GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 10-Mar-2004, 04:09
tommy69 tommy69 is offline
Junior Member
 
Join Date: Mar 2004
Posts: 40
tommy69 is on a distinguished road

coding a sentinel controlled loop


This one is driving me nuts:

I need to code a loop, like a While loop and if a user types -5, the loop will will stop looping. The loop will contain statements which input and output the integers depending on what integer the user types.

If either a 2 or a 99 is typed, the integer simply won't be printed. All other integers will be printed. Once the user types in -1, the message, "End of Program" will print.

For example;

The printf statement will be like "Please enter an integer". If I type in a 8, the message displays, "The integer you have selected is 8". If I type in -5, "End of Program" is printed. If I type in 2, nothing is printed and myabe the message could go back to "Please enter in another integer" or maybe it's better to go back to the original message of "Please enter an integer". Does this make sense? I have tried to code this many and many of times without any success. Any help would greatly be appreciated. Thanks much,

Tom
  #2  
Old 10-Mar-2004, 07:40
pan pan is offline
New Member
 
Join Date: Mar 2004
Location: Greece
Posts: 16
pan will become famous soon enough

coding a loop


Hello Tom,

>I need to code a loop, like a While loop and if a user types -5, the loop will will stop looping.

This can be done with the "break;" statement, which allows you to exit a loop you are from any point within that loop. For instance, you may use an if statement and when -5 is encountered the loop breaks. However, you may also keep looping until a -5 is encountered by diong something like this:

CPP / C++ / C Code:

int input = 0;             //declare an input
while ( input != -5 ){  //keep looping until a -5 is encountered
   cin >> input;          //get the input
}


>If either a 2 or a 99 is typed, the integer simply won't be printed.

This can be controlled through an if statement, with an AND ("&&"):

CPP / C++ / C Code:

int input = 0;
while ( input != -5 ){
     cin >> input;
     if ( input != 2 && input != 99 )
          cout << "You entered the number " << input << '\n';
}


If this information is not helpful, post what code you do have and I can get back to you.

Best of,
Pan Thomakos
  #3  
Old 10-Mar-2004, 14:52
tommy69 tommy69 is offline
Junior Member
 
Join Date: Mar 2004
Posts: 40
tommy69 is on a distinguished road
Thumbs up

Thanks for the reply. You were a big help :-) 8-)
 
 

Recent GIDBlogProblems with the Navy (Enlisted) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 23:50.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.