如何不断要求用户输入一个int保持的次数?

(对c ++来说是新手,不错)

我希望能够要求用户输入与名单中所说的一样多的学生姓名。因此,基本上,我想使用整数来回答问题。

(是的,我在python上看到了类似的问题,但我不知道如何将其应用于c ++。)

我知道它涉及while循环。我知道如何增加。我已经建立了一个while循环,我所需要的只是正确的条件。

//Num_Students equals the number of students the user has.
//Students_List equals the student number that I am asking for. Such as
//Student 1,and so on 
//s1 is a string that holds the name of the student. 

while (Num_Students =) {
  Students_List = 1;
  cout << "Enter the full name of student " << Students_List << " >";
  cin >> s1;
  Students_List++;
}

我希望输出像这样:

输入学生1的全名>哔声

输入学生2的全名> bop

输入学生3的全名> boop

以此类推。

huage24 回答:如何不断要求用户输入一个int保持的次数?

您可以选择。您可以检查EOF,但在这里不起作用。您还可以预定义程序将退出的符号。与之相同,输入exit将终止您的程序。您可以检查以下程序以获取帮助。

string s1("");
int Students_List = 1;
while (s1.compare("exit")) {
  cout << "Enter the full name of student " << Students_List << " >";
  cin >> s1;
  Students_List++;
}

我使用了string::compare

,
int Students_List = 1;
string s1;

while (Num_Students) {
    cout << "Enter the full name of student " << Students_List << " >";
    getline(cin,s1);
    Students_List++;
    Num_Students--;
}

其中Num_Students初始化为学生人数。 std::getline从输入流读取直到行尾(包括任何空格)。请注意,Students_List必须在循环之前进行初始化,否则每次执行循环时都会将其重置为1。

本文链接:https://www.f2er.com/3162256.html

大家都在问