By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,637 Members | 1,945 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,637 IT Pros & Developers. It's quick & easy.

sorry, another newbie question on loops!

pdring
P: 15
Hi guys, sorry but yet another newbie struggling with the basics!
I have a small program that i am working on in wich 1 want to enter exam scores for each of 5 students in turn untill i enter a set number to stop the loop and carry on with the program, the loop i have come up with is bellow but it keeps going and ignores my set number, can anyone shed any light on this problem loop??

[HTML]while ( score[count]!= -999)
{

for (count = 0;count <5; count++)
{
printf ("\nPlease enter the exam score for student number %d: ", count+1);
scanf ("%d", &score[count]);


}
}[/HTML]

thanks for any help,
Phil (Cornwall)
Jan 5 '08 #1
Share this Question
Share on Google+
2 Replies


P: 5
Hi guys, sorry but yet another newbie struggling with the basics!
I have a small program that i am working on in wich 1 want to enter exam scores for each of 5 students in turn untill i enter a set number to stop the loop and carry on with the program, the loop i have come up with is bellow but it keeps going and ignores my set number, can anyone shed any light on this problem loop??

[HTML]while ( score[count]!= -999)
{

for (count = 0;count <5; count++)
{
printf ("\nPlease enter the exam score for student number %d: ", count+1);
scanf ("%d", &score[count]);


}
}[/HTML]

thanks for any help,
Phil (Cornwall)
There are many possibilities why your code is not working. See, in the for loop, each time when the loop ends and the score[count] goes for checking in the outer while loop, the value of count is 5. But in reality you havent entered any value in score[5], so the loop keep on running.

It would be certainly better if you can use do-while loop in this kind of exit checking situation. Here is an example of it, you can work it out in a different way of course -

Expand|Select|Wrap|Line Numbers
  1.  
  2. do {
  3.  
  4. printf ("\nPlease enter the exam score for student number %d:    ", count+1);
  5. scanf ("%d", &score[count]); }while (count<5&&score[count]!=-999);
  6.  
  7.  
Jan 5 '08 #2

pdring
P: 15
There are many possibilities why your code is not working. See, in the for loop, each time when the loop ends and the score[count] goes for checking in the outer while loop, the value of count is 5. But in reality you havent entered any value in score[5], so the loop keep on running.

It would be certainly better if you can use do-while loop in this kind of exit checking situation. Here is an example of it, you can work it out in a different way of course -

Expand|Select|Wrap|Line Numbers
  1.  
  2. do {
  3.  
  4. printf ("\nPlease enter the exam score for student number %d:    ", count+1);
  5. scanf ("%d", &score[count]); }while (count<5&&score[count]!=-999);
  6.  
  7.  
Thanks for your help. it has solved my problem,
Phil
Jan 5 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.