473,657 Members | 2,376 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sorry, another newbie question on loops!

pdring
15 New Member
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 1256
sagnik`
5 New Member
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 New Member
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
6572
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
3044
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 counter within the loop declaration, for loops have always seemed to me to be about doing something a certain number of times, and not about iterating over an object. The reason for this distinction comes from the fact that I read a lot how...
10
3965
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 than 'while'? C: for(i=0; i<length; i++) python: while i < length:
5
2265
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
1798
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.5 5.5 Bob, Jolene The first time through the do while loop it works fine but the second
4
1454
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 it and bring back video. Basically a home brew DVB-s application. Being a non/new programmer (apart from some basic too many years ago) I decided to teach my self Python as from what i have read it's an easy and
2
1920
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 words, and its in the form of:
33
7162
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 array, so im inputting the dimensions of those myself. The problem is that the output array (C=A*B) has as many rows as A and as many columns as B. I would think of initialising C with:
8
7247
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 <Boolean ExpressionThen Exit For Next If Not <Boolean ExpressionThen Exit For
0
8399
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8312
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8827
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8504
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8606
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6169
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4159
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1622
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.