954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Would you like to try again? Press Y, Press N. (Repeating program function)

Hi,

Would anybody know a way to repeat a program, for example the user has reached the end and a prompt is shown on screen Would you like to try again? Press y, n. I have managed to do this using goto command but this as ive read is a no go zone unless really needed. I have also found bool to be of some use.

Also i have 3 if statements working to filter through a user value that has to be between 3 and 31 and odd.

cout << "Please enter an odd value between 3 and 31: " << endl;
cin >> rate;
myWaveForm.getSampleValue( rate );

cout << endl;

//upto here we have a value that the user has entered

if ( rate % 2 != 1 ){
else rate = rate; }

if ( rate <3 ){
if ( rate > 31 );}
else rate = rate;

Is there a way of simplifying this?

johnnyjohn20
Newbie Poster
22 posts since Nov 2007
Reputation Points: 37
Solved Threads: 0
 

>>Would anybody know a way to repeat a program
use a loop

while( not done )
{
    // display menu
    // get input
    // if 'Q' entered then exit the loop
    // do something
}


Not sure what you are trying to do that that code you posted. But rate = rate; doesn't do anything, so you might as well just replace it with a simple semicolon.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Is that 'while' you put there a loop? i initally thought of a counter (being a beginer to this i only know counters as loops)

johnnyjohn20
Newbie Poster
22 posts since Nov 2007
Reputation Points: 37
Solved Threads: 0
 

read your text book. while is just one of several types of loops. See this tutorial

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

wish i was good at this! humph!!

johnnyjohn20
Newbie Poster
22 posts since Nov 2007
Reputation Points: 37
Solved Threads: 0
 
wish i was good at this! humph!!


You will be. Hooray!!!

Just takes practice...

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You