473,326 Members | 2,128 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

sorry, another newbie question on loops!

pdring
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
2 1242
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
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

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

Similar topics

15
by: JustSomeGuy | last post by:
I have a need to make an applicaiton that uses a variable number of nested for loops. for now I'm using a fixed number: for (z=0; z < Z; ++z) for (y=0; y < Y; ++y) for (x=0; x < X; ++x)
17
by: John Salerno | last post by:
I'm reading Text Processing in Python right now and I came across a comment that is helping me to see for loops in a new light. I think because I'm used to the C-style for loop where you create a...
10
by: Putty | last post by:
In C and C++ and Java, the 'for' statement is a shortcut to make very concise loops. In python, 'for' iterates over elements in a sequence. Is there a way to do this in python that's more concise...
5
by: sigloiv | last post by:
I've very much a beginner to VB.NET, and I'm running Visual Basic 2005 Express. Anyways, all I'd like to do is run three concurrent Do loops at the same time. Is this at all possible?
19
by: Zach Heath | last post by:
I'm just starting C and haven't done programming for a few years...could you guys please take a look at this? Thanks for your time! I have an input file that looks like: 1.5 2.5 Bob, Joe...
4
by: Grayham | last post by:
Hi all I am new to this group so 'Hello All' I have a PC which is running linux and in it have installed a digital satellite card. I would like to write some software to access the card, tune...
2
by: Zethex | last post by:
At the moment i'm doing a piece of work for school and I'm stuck at the moment. I have a list of words, for example: Sentence = I have another list which I need to use to replace certain...
33
by: Adam Chapman | last post by:
Hi, Im trying to migrate from programming in Matlab over to C. Im trying to make a simple function to multiply one matrix by the other. I've realised that C can't determine the size of a 2d...
8
by: Nathan Sokalski | last post by:
I have several nested For loops, as follows: For a As Integer = 0 To 255 For b As Integer = 0 To 255 For c As Integer = 0 To 255 If <Boolean ExpressionThen <My CodeElse Exit For Next If Not...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.