Help with loops and functions

Ctrackstar126

Senior member
Jul 14, 2005
988
0
76
Ok i can get this to function but Im wondering about how I can loop it all. This is an example of the outpout that needs to be asked:

Enter the ID for student 1: 1232
Enter the 1st test grade for student 1: 80
Enter the 2nd test grade for student 1: 65
Enter the 3rd test grade for student 1: 90
Enter the ID for student 2: 9821

Enter the 1st test grade for student 2: 30
Enter the 2nd test grade for student 2: -1
Invalid score! Enter a score between 0 and 100.
Enter the 2nd test grade for student 2: 22
Enter the 3rd test grade for student 2: 12


this goes all the way up to 35 students. My problem is with how to place the loops. this is what I got so far for coding. (im a NOOB). I just wanted to make sure I was going the right route and maybe just need to position it a little different. I am using an a, b, c, d array to hold the ID and the test scores. Thanks guys :)

for(int i=0;i<SIZE;i++){


do{
cout<<"Enter the ID for student "<<y<<" : ";
cin>>a;
if(a<1000||a>=9999){
cout<<"Invalid ID! Please enter a number between 1000 and 9999"<<endl;
y=y;
}
do{ cout<<"Enter the 1st test grade for student "<<y<<" : ";
cin>>b;
if(b<0||b>=100){
cout<<"Invalid score! Enter a score between 0 and 100."<<endl;
y=y;
}
y++;
}while(y>=1&&y<=35);
}
 

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
When you pasted the code into your message, italics screwed up your code a bit, so here is the way it's supposed to be (use Attach Code in message post window):

What does the 'y=y;' line intend to do?

If I were you I'd put each of the conditionals of the if and while statements in its own set of parentheses like this:

while ((y>=1) && (y<=35))

Just to avoid any unpredictable behaviors. It is easier to read, fool-proof, and won't make your program any bigger. I could have made a mistake in indenting it but is there a missing end brace somewhere in the program?
 

Ctrackstar126

Senior member
Jul 14, 2005
988
0
76
the y=y is supposed to make sure that the y++ doesn't go into effect. but now that i think of it thats not going to help because my arrays are still going to increment. Because after I then have to do a whole bunch of calculations with the arrays.
Im a hardware guy, but this stuff is fun to try to figure out.

Another question can u put loops within loops like you can with if and else statements.
 

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
Originally posted by: Ctrackstar126
the y=y is supposed to make sure that the y++ doesn't go into effect.

Actually, while syntaxically correct, the y=y statement just doesn't do or prevent anything on its own. It's setting y to 42 when it's already 42, for example.

Also, checking whether y is inclusively within the bounds 1-35 is redundant if y already starts off at 1. (Just initialize y=1 and check if y<35 at end of while loop.)

but now that i think of it thats not going to help because my arrays are still going to increment. Because after I then have to do a whole bunch of calculations with the arrays.
Im a hardware guy, but this stuff is fun to try to figure out.

Is the a array allocated to encompass the possible i values you are addressing it with?

Another question can u put loops within loops like you can with if and else statements.

Sure, that is called nesting.
 

Ctrackstar126

Senior member
Jul 14, 2005
988
0
76


Is the a array allocated to encompass the possible i values you are addressing it with?

Yea. heres the thing i don't understand. What exactly is the i there for. If i have four arrays do i use the i to allocate with each array like b, c and d.