This line: std::cout << "Enter student name: ";
is inside the for
loop so it displays 5 times. This is what you want to do. Just enter the name 5 times.
For Loop Prompt Repeating 5 Times
Question
I am trying to write a simple command line student database program and when the prompt begins the loop in the for loop it repeats 5 times when asking for the next student's name. What is wrong here? This is what I have.
Output:
Enter number of students in class: 5 Enter student name: John Smith Enter student name: Enter student name: Enter student name: Enter student name:
Code:
for (int i = 0; i < totalStudents; i++) {
std::cout << "Enter student name: ";
std::cin.get(studentName[i]);
std::cin.ignore();
}
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow