nested while loop

good day, im having trouble converting for loop to while loop. i did the "input number of student having 3 quizes each and get the average" in for loop. the problem is that I'm trying it in while loop. i followed the format of while like this:

while(condition)
{
while(condition)
{
statement(s);
}
statement(s);
}

here's my code for while loop:

#include<iostream>

using namespace std;

int main()

{
int student,quiz;
int x=1;
int y=1;

cout<<"enter student num: ";
cin>>student;

while(x<=student)
{
cout<<"student " <<x <<":";
while(y<=3)
{
cout<<"\nQuiz" <<y <<":";
cin>>quiz;
y++;
}
x++;
}
}

im trying to input something here just to test it but, when i try to input 2 or more number of students it just stops at 1 student and end the program. i followed the format of nested while and i cant figure out what went wrong. any help would be appreciated.
Last edited on
The problem is that you don't reset your y value after each loop.
thnx for the reply. i did put the reset in the outer loop but now it includes 0 in counting the quizes.
Did you reset y to zero or to one?
oh.. i set it to 0, i see my mistake and thanks for the help. but, why do i have to put a reset on y? i have my initialization on the top which y=1. so, inner while does not read the initialization on the top and when using nested while loop i have to always reset it? i have no problem doing this on for loop. thnx again
First, you set your y value to 1. Then the outer while loop executes, but it doesn't automatically reset things. That's just how while loops work. A for loop works differently. All for loops can be written as while loops, but usually the for version is shorter.
Topic archived. No new replies allowed.