【发布时间】:2023-03-16 14:19:01
【问题描述】:
我正在尝试编写一个控制台程序,它从输入中获取一个数字并将其放入一个数组中。只要用户没有点击“空格”按钮,读入就会持续。我已经尝试了几件事,但我的程序不会将输入与“空格”的 ASCII 码进行比较。提前致谢!
#include <iostream>
using namespace std;
int main()
{
int fabcd[25],number_of_items = 0;
cout << "The read in loop lasts while you don't hit space:\n";
while((char)cin.get() != 32)
{
cin >> fabcd[number_of_items];
number_of_items++;
}
return 0;
}
【问题讨论】:
-
它会得到一个整数输入,并与数字 32 进行比较
-
也许 cin.get() 不会以这种方式或其他方式工作
-
不,它把它当作一个字符,但仍然无法工作 xD
-
流默认为空格跳过行为。参见例如stackoverflow.com/questions/6774825/…
-
等等!这是什么?
cin >> fabcd[number_of_items]
标签: c++ while-loop compare ascii