-7

How do I run a loop based on the user input?

Example:

I ask how many students does the user want to calculate the grade.

if the user enters 2 students, then I will ask the user to input grades for exams, homework, quiz etc...

After the program calculate the first student grade, how do I run the loop again for the second student?

I tried using a while loop but it just goes to infinite loop.

What I did was:

cout << "number of student you want to calculate grade for" << endl; 

cin >> student; 

while (student) { 
... 
... 
... 
... 
} 

when I run this it goes to infinite loop.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • [Here's a helpful link for you](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), that should help you write your program. – Sam Varshavchik Nov 01 '17 at 02:34

1 Answers1

-2

you need to make sure you decrement student inside the loop as you check if there are any students left to get information for.

while(student > 0){ // check if any students left
    // your code to get student information
    student -= 1; // decrement students
} 
hahahakebab
  • 328
  • 7
  • 22