C++ 中的嵌套循环和用户输入
Nested loops in C++ and user input
这里是编程的新手,我有一项任务需要实现以下目标:
- 求总人数
- 得到他们每个人的名字
- 允许用户为每个人输入最多 5 个分数
- 如果给定的人的分数少于 5,输入 -100 将停止它
到目前为止我已经写了这个:
#include <iostream>
using namespace std;
int main() {
string personName;
int totalPerson, personScoreCounter;
double personGrade, personGradeTotal;
cout << "Input total amount of people: ";
cin >> totalPerson;
for (int person = 1; person <= totalPerson; person++)
{
cout << "Input name for person " << person << ": ";
getline(cin, personName);
cin.ignore();
while ( (personGrade != -100) && (personScoreCounter <= 5) )
{
cout << "Input up to 5 scores for " << personName << " (-100 to end): ";
cin >> personGrade;
if (personGrade >= 0 && personGrade <= 100) // valid range of scores
{
personGradeTotal += personGrade;
personScoreCounter++;
}
else
{
cout << "Input only scores from 0-100" << endl;
}
cout << "Input up to 5 scores for " << personName << " (-100 to end): ";
cin >> personGrade;
}
}
// calculate averages and other stuff in here.
return 0;
}
得到他们的名字后,只有while循环中的最后一个cout
先执行,然后从顶部开始,依此类推,直到for
循环结束,这取决于totalPerson
。我知道我在这里遗漏了一些东西,可能是操作顺序以及我执行循环的方式,但我就是看不到它。你们中任何有语言经验的人都可以给我任何关于这里发生的事情以及我如何解决它的指示吗?谢谢。
在您的 while
组中,您只想使用 cout
行一次(开头看起来不错)。
您的第一个检查应该是 ==-100
或类似的,因为现在,如果您输入 -100
.
,您将收到 "Input only scores from 0 to 100" 消息
你应该在每次使用 cin >> VARIABLE
后保持一个 cin.ignore();
调用,因为那样你将放弃 EoL 字符。
示例代码:
#include <iostream>
using namespace std;
int main() {
int totalPerson;
cout << "Input total number of people: ";
cin >> totalPerson;
cin.ignore();
for (int person = 1; person <= totalPerson; person++)
{
int personScoreCounter=0;
double personGrade = -1, personGradeTotal=0;
string personName;
cout << "Input name for person " << person << ": ";
std::getline(cin, personName);
while ( (personGrade != -100) && (personScoreCounter < 5) )
{
cout << "Input up to 5 scores for " << personName << " (-100 to end): ";
cin >> personGrade;
cin.ignore();
if (personGrade == -100) {
break;
} else if (personGrade >= 0 && personGrade <= 100) {
personGradeTotal += personGrade;
personScoreCounter++;
} else {
cout << "Input only scores from 0-100" << endl;
}
}
// calculate averages and other stuff in here.
double avg = personGradeTotal / personScoreCounter;
cout << "Avg = " << avg << endl;
}
return 0;
}
您的一些变量还需要移动内部 for
循环。
此外,我将 personScoreCounter
的限制更改为 [0:4] 而不是 [1:5] - 这样您可以更轻松地使用它进行平均。
您也可以尝试 cin.getline() 而不是 getline(std::cin , ... ):
int max_length = 30;
std::cin.getline(personName, max_length, '\n'); // \n is option termination.
这也允许在输入中使用空格。
http://www.cplusplus.com/reference/istream/istream/getline/
这里是编程的新手,我有一项任务需要实现以下目标:
- 求总人数
- 得到他们每个人的名字
- 允许用户为每个人输入最多 5 个分数
- 如果给定的人的分数少于 5,输入 -100 将停止它
到目前为止我已经写了这个:
#include <iostream>
using namespace std;
int main() {
string personName;
int totalPerson, personScoreCounter;
double personGrade, personGradeTotal;
cout << "Input total amount of people: ";
cin >> totalPerson;
for (int person = 1; person <= totalPerson; person++)
{
cout << "Input name for person " << person << ": ";
getline(cin, personName);
cin.ignore();
while ( (personGrade != -100) && (personScoreCounter <= 5) )
{
cout << "Input up to 5 scores for " << personName << " (-100 to end): ";
cin >> personGrade;
if (personGrade >= 0 && personGrade <= 100) // valid range of scores
{
personGradeTotal += personGrade;
personScoreCounter++;
}
else
{
cout << "Input only scores from 0-100" << endl;
}
cout << "Input up to 5 scores for " << personName << " (-100 to end): ";
cin >> personGrade;
}
}
// calculate averages and other stuff in here.
return 0;
}
得到他们的名字后,只有while循环中的最后一个cout
先执行,然后从顶部开始,依此类推,直到for
循环结束,这取决于totalPerson
。我知道我在这里遗漏了一些东西,可能是操作顺序以及我执行循环的方式,但我就是看不到它。你们中任何有语言经验的人都可以给我任何关于这里发生的事情以及我如何解决它的指示吗?谢谢。
在您的 while
组中,您只想使用 cout
行一次(开头看起来不错)。
您的第一个检查应该是 ==-100
或类似的,因为现在,如果您输入 -100
.
你应该在每次使用 cin >> VARIABLE
后保持一个 cin.ignore();
调用,因为那样你将放弃 EoL 字符。
示例代码:
#include <iostream>
using namespace std;
int main() {
int totalPerson;
cout << "Input total number of people: ";
cin >> totalPerson;
cin.ignore();
for (int person = 1; person <= totalPerson; person++)
{
int personScoreCounter=0;
double personGrade = -1, personGradeTotal=0;
string personName;
cout << "Input name for person " << person << ": ";
std::getline(cin, personName);
while ( (personGrade != -100) && (personScoreCounter < 5) )
{
cout << "Input up to 5 scores for " << personName << " (-100 to end): ";
cin >> personGrade;
cin.ignore();
if (personGrade == -100) {
break;
} else if (personGrade >= 0 && personGrade <= 100) {
personGradeTotal += personGrade;
personScoreCounter++;
} else {
cout << "Input only scores from 0-100" << endl;
}
}
// calculate averages and other stuff in here.
double avg = personGradeTotal / personScoreCounter;
cout << "Avg = " << avg << endl;
}
return 0;
}
您的一些变量还需要移动内部 for
循环。
此外,我将 personScoreCounter
的限制更改为 [0:4] 而不是 [1:5] - 这样您可以更轻松地使用它进行平均。
您也可以尝试 cin.getline() 而不是 getline(std::cin , ... ):
int max_length = 30;
std::cin.getline(personName, max_length, '\n'); // \n is option termination.
这也允许在输入中使用空格。 http://www.cplusplus.com/reference/istream/istream/getline/