473,401 Members | 2,127 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,401 software developers and data experts.

How to stop a loop before it gets to the last one

16
This loop goes through all the lines in the file but there are only six lines and it has seven outputs
Oct 12 '10 #1
9 1806
Oralloy
988 Expert 512MB
Well, try using a loop counter in addition to your eof condition:
Expand|Select|Wrap|Line Numbers
  1. int count = 1;
  2. while ( (count < 7) && !cin.eof() )
  3. {
  4.   count ++;
  5. . . .
Oct 12 '10 #2
nuken
16
that would work but there could be more or less than 7 in the file.
Oct 12 '10 #3
Oralloy
988 Expert 512MB
So what is the alternate terminiation condition?

If you know how to detect the state, you can either detect at the top of the file, or you can insert this code following line 25 of your example:
Expand|Select|Wrap|Line Numbers
  1.      if (cin.eof())
  2.         break;
Luck!
Oct 12 '10 #4
nuken
16
Parse error on line 55: break statements are only allowed within switch statements;
breaking out of a loop is not supported.

I added that and it gave me this error.
Oct 12 '10 #5
Oralloy
988 Expert 512MB
You're using C#, aren't you?

Ok, so you need to build a guard around the while loop at line 26. You can do it the easy way and write:
Expand|Select|Wrap|Line Numbers
  1.         while ( (!cin.eof()) && (count <= scores_in_section) ) 
Or you can guard the whole thing with an "if" statement:
Expand|Select|Wrap|Line Numbers
  1.         if (!cin.eof())
  2.         {
  3.           while ( count <= scores_in_section ) 
  4.           {
  5.           . . .
  6.           }
  7.         }
  8.  
Oct 12 '10 #6
nuken
16
Actually im using C++, but heres the whole code because i think the problem is the first if statement is set to 0at the end and thats why it outputs that seventh one:
Oct 12 '10 #7
Oralloy
988 Expert 512MB
After line 37, try inserting
Expand|Select|Wrap|Line Numbers
  1. if (!cin.eof()) {
And after line 106 insert
Expand|Select|Wrap|Line Numbers
  1. }
That should effectively mask out the section logic when EOF occurs on input.
Oct 12 '10 #8
nuken
16
Dude thankyou soo much!!! You are awesome
Oct 12 '10 #9
Oralloy
988 Expert 512MB
No worries, nuken. Just be sure to help others when you can.

Luck!
Oct 12 '10 #10

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

Similar topics

2
by: Steve Henderson | last post by:
I have really surprised myself and written a script that actually works! <grin>. As you will be able to tell, I'm really, really new to this scripting stuff... My script does a task and then sleeps...
0
by: volumstein | last post by:
I have a make-table query in a FOR loop that outputs to 'table1.' 'table1' is then referenced by 'Form1.' here's the code: For j = 1 To rsTable.RecordCount SerialNumRef.Value =...
3
by: Rudy | last post by:
I am writing a program in VB.NET and as I was debugging a problem I noticed my For loop doesn't want to loop! I originally had a upper bound which was an expression and it wasn't working. WHen I...
3
by: Ivan V via DotNetMonster.com | last post by:
Hi All: I would like to know if I can loop my selection in a combobox. For instance, I got 8 items in a combobox, and when I selected it using the up and down arrow, it will stop in the first...
19
by: vamshi | last post by:
Hi all, This is a question about the efficiency of the code. a :- int i; for( i = 0; i < 20; i++ ) printf("%d",i); b:- int i = 10;
6
by: Christo | last post by:
I have this script for showing news on a page, but i want it to only show the last 10 records, as in the 10 records that were added to the database last. the script shows the entries in descending...
4
anfetienne
by: anfetienne | last post by:
hi im back again.......i have a code to create strings and save it within a text file to pass variables to flash. im using the string format below. ...
0
dbrewerton
by: dbrewerton | last post by:
Hello folks. I'm kinda stumped here so I could use a second pair of eyes. I'm trying to increment the x_device_id variable to the next ID in the database. MySQL is complaining about my incrementing...
3
by: Sam Zuhbi | last post by:
Hi, I was wondering how do I make the next record button on my access 2007 form stop at the last record, I want to make sure that if it does not create a new record if it reaches the last record...
3
by: dowlingm815 | last post by:
I am receiving a Do While Syntax Error - Loop without Do - Can't See it. I would appreciate any fresh eyes? Mary Private Sub Create_tblNJDOC() On Error GoTo Err_Hndlr
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.