- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
#include <iostream>
#include<cmath>
using namespace std;
int main(){
int grades[10];
cout << "Enter list: ";
int count = 0;
int current = 0;
while (current >= 0){
cin >> current;
grades[count] = current;
count += 1;
}
cout << grades[0];
}
在输入由空格分隔的数字列表(总共少于 10 个) 后,应输出数组中的第一个整数,但不输出任何内容。理想情况下,它应该输出整个数组,但我不明白为什么它不只输出数组的第一个值。我怀疑这与 while (current >= 0) 有关。如果是这样,那么我想知道如何检查流中是否没有更多输入。
最佳答案
您的代码中的数组,如 int grades[10]
无法在标准 C++ 中调整大小。
相反,使用标准容器 - 例如 std::vector
- 设计为在运行时调整大小
#include <vector>
#include <iostream>
int main()
{
std::vector<int> grades(0); // our container, initially sized to zero
grades.reserve(10); // optional, if we can guess ten values are typical
std::cout << "Enter list of grades separated by spaces: ";
int input;
while ((std::cin >> input) && input > 0) // exit loop on read error or input zero or less
{
grades.push_back(input); // adds value to container, resizing
}
std::cout << grades.size() << " values have been entered\n";
// now we demonstrate a couple of options for outputting all the values
for (int i = 0; i < grades.size(); ++i) // output the values
{
std::cout << ' ' << grades[i];
}
std::cout << '\n';
for (const auto &val : grades) // another way to output the values (C++11 and later)
{
std::cout << ' ' << val;
}
std::cout << '\n';
}
关于c++ - 如何在不知道最初输入多少值的情况下将整数存储到数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58055874/
我有一个使用 Sinch SDK 和 CallKit 实现的 VOIP 应用程序。一切正常,除了设备插入耳机时。在后一种情况下,当通话开始时,音频仍通过设备的主扬声器路由。如果我在通话期间拔下并重
我需要将大量复杂(20 多个属性)对象过滤成多个子列表。要创建子列表,我有一个过滤器规范列表。要求是:a) 不允许一个项目成为两个子列表的一部分,并且 b) 必须能够在处理完成后获取所有未分割的项目。
我有一个简单的 .share-toggle 切换链接。当您点击它时,Facebook 评论插件应该会显示。它最初应该被 Conceal 问题是,如果我使用 css Conceal 它,然后单击切换按钮
我正在尝试制作一个功能类似于 android 市场的图库,您可以在其中滚动(左/右)以查看免费或付费应用程序等...还可以在布局中上下滚动。 到目前为止,我只是让它加载了两个布局,它们都有一个简单的“
大家好,我是网络开发的新手,最近刚刚完成我对网络设计的第一次尝试。我偶然发现了一个我找不到解决方案的问题。在 webkit-browsers 中,我的一个 ul-lists(导航)最初呈现在错误的位置
我遇到了这个问题。我在 ISP 上使用 Umbraco CMS 启动了一个 ASP.NET 网站。(这只是一个非常基本的信息网站。没什么特别的。) 但是,当我想要访问该网站时,第一个页面加载需要很长时
在我的 iPhone 应用程序中,我使用 UITabBarController 布置了三个选项卡。第一个选项卡(在应用启动时加载)使用本地数据加载,速度非常快。 虽然第二个选项卡从网络下载 XML 文
我有这样的风格: #cytoscape-container { width: 100%; height: 100%; margin: 0 aut
我在这里看到了几篇关于 SO 的帖子,但它们在功能和结构上过于具体,而我正在寻找的是我或任何人都可以在任何地方使用的更通用的东西。 我只需要有一个按钮,单击该按钮可以在 3 类之间循环。但如果出现必须
我在 http://www.raven.dima.neoturbine.net/ 有一个网页我正在努力。顶部导航在 IE 8、Firefox 3.6 和适用于 Android 的 Dolphin 浏览
我想将主机的用户/组与 docker 机器同步,以使(开发人员)能够编辑容器内部或外部的文件。有一些这样的想法:Handling Permissions with Docker Volumes这会创建
向通知列表/栏发布推送通知时,.contentText 和 .number 最初不显示(.ticker、.icon 和 .contentTitle 显示正常)。但是,在发布另一个通知(具有不同的 ID
我实现了以下 MKMapView 方法,该方法在添加注释后运行。我在 Interface Builder 中将我的 MKMapView map (parishMap) 设置为“显示用户位置”,并且在加
我是一名优秀的程序员,十分优秀!