我想获取结构数组的名称和分数对。结构中的一个字段是c字符串,我不确定如何在该字段中以字符串形式存储输入。当我运行以下代码时,“分数”字段会正确存储,但是在我输入了scores [counter] .name的字符串后,控制台窗口将不再接受任何输入。光标只是闪烁而没有任何反应。有人可以告诉我怎么回事吗?谢谢!
struct highscore
{
int score;
char name[charSize];
};
void initializeData(highscore scores[], int size)
{
for (int counter = 0; counter < size; counter++)
{
cout << "Enter the name for score #" << (counter + 1) << ": ";
cin >> scores[counter].score;
cout << "Enter the score for score #" << (counter + 1) << ": ";
do {
cin >> scores[counter].name;
} while (scores[counter].name != '\0');
}
}
答案 0 :(得分:0)
处理字符串(一长串字符)甚至带空格的字符串的最简单方法就是使用C ++中的以下库。
#include <bits/stdc++.h>
然后只声明一个字符串变量。
struct highscore
{
int score;
String name;
};
然后在循环中
for (int counter = 0; counter < size; counter++)
{
cout << "Enter the name for score #" << (counter + 1) << ": ";
cin >> scores[counter].name;
cout << "Enter the score for score #" << (counter + 1) << ": ";
cin >> scores[counter].score;
}