对于循环提示重复5次

时间:2014-01-29 03:18:54

标签: for-loop cout cin

我正在尝试编写一个简单的命令行学生数据库程序,当提示在for循环中开始循环时,它会在要求下一个学生的名字时重复5次。这有什么不对?这就是我所拥有的。

输出:

输入课堂上的学生人数:5 输入学生姓名:John Smith 输入学生姓名:输入学生姓名:输入学生姓名:输入学生姓名:


代码:

for (int i = 0; i < totalStudents; i++) {
    std::cout << "Enter student name: ";
    std::cin.get(studentName[i]);
    std::cin.ignore();
}

1 个答案:

答案 0 :(得分:0)

此行:std::cout << "Enter student name: ";位于for循环内,因此显示5次。这就是你想要做的。只需输入名称5次。