10 Replies - 1697 Views - Last Post: 18 October 2011 - 06:09 AM Rate Topic: -----

#1 Eowyn27  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 134
  • Joined: 17-October 11

how to continue a loop until the user enters the correct command

Posted 17 October 2011 - 12:14 PM

I'm just wondering what's the basic format for if-else if or do-while or any looping that you know of on how to continue a loop until the user enters the correct command?

That is, if the statement asks the user to input a positive number, and user enters a positive number, the statement ,whatever it may be, executes but if the user enters a negative number, the statement repeats the command of asking the user for a positive number until the command works.

Do you know what I mean? XD Sorry if this is obvious or has been asked before. I don't know how to do it :(

Is This A Good Question/Topic? 0
  • +

Replies To: how to continue a loop until the user enters the correct command

#2 DarenR  Icon User is online

  • D.I.C Lover

Reputation: 433
  • View blog
  • Posts: 2,999
  • Joined: 12-January 10

Re: how to continue a loop until the user enters the correct command

Posted 17 October 2011 - 12:17 PM

psuedo code

while(userinput != your answer)
keep asking question
else
your output
Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is online

  • member icon


Reputation: 3992
  • View blog
  • Posts: 12,320
  • Joined: 25-December 09

Re: how to continue a loop until the user enters the correct command

Posted 17 October 2011 - 12:27 PM

You can start by studying this link: Control Structures.

Jim
Was This Post Helpful? 0
  • +
  • -

#4 Eowyn27  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 134
  • Joined: 17-October 11

Re: how to continue a loop until the user enters the correct command

Posted 17 October 2011 - 12:34 PM

View Postjimblumberg, on 17 October 2011 - 12:27 PM, said:

You can start by studying this link: Control Structures.

Jim



So basically, I can use a do-while loop right?

do (true user input) while (input!=false input) hmmm
Was This Post Helpful? 0
  • +
  • -

#5 Eowyn27  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 134
  • Joined: 17-October 11

Re: how to continue a loop until the user enters the correct command

Posted 17 October 2011 - 12:44 PM

do
{
cout <<"Enter a correct month: "; 
cin >> month; 
} 
while (month !< 1 && month !>12); 


Why doesn't this code work?

When I compile the program, it says: "expected '>' before '!' token and expected primary-expression before '<' token....

What does that mean?


Thanks a bunch guys!!!
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is online

  • member icon


Reputation: 3992
  • View blog
  • Posts: 12,320
  • Joined: 25-December 09

Re: how to continue a loop until the user enters the correct command

Posted 17 October 2011 - 01:02 PM

There are no "!<" or "!>" operators. See this link for the available operators.

Jim

This post has been edited by jimblumberg: 17 October 2011 - 01:03 PM

Was This Post Helpful? 0
  • +
  • -

#7 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 218
  • View blog
  • Posts: 591
  • Joined: 04-August 09

Re: how to continue a loop until the user enters the correct command

Posted 17 October 2011 - 01:03 PM

Instead of !< it's >=. Similarly for !> its <=.
Was This Post Helpful? 2
  • +
  • -

#8 Eowyn27  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 134
  • Joined: 17-October 11

Re: how to continue a loop until the user enters the correct command

Posted 17 October 2011 - 01:27 PM

View Postsimeesta, on 17 October 2011 - 01:03 PM, said:

Instead of !< it's >=. Similarly for !> its <=.



Thank you!! I just need to figure out why my program doesn't work for a different reason haha xD
Was This Post Helpful? 0
  • +
  • -

#9 Eowyn27  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 134
  • Joined: 17-October 11

Re: how to continue a loop until the user enters the correct command

Posted 17 October 2011 - 07:20 PM

Out of curiosity, if you I wanted to do the same using if-else statements, how would I do it?
Was This Post Helpful? 0
  • +
  • -

#10 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 218
  • View blog
  • Posts: 591
  • Joined: 04-August 09

Re: how to continue a loop until the user enters the correct command

Posted 17 October 2011 - 11:51 PM

You can't, because
int x;
cin >> x;
if(badInput())
{
   cin >> x;//what if its bad again
    // you have to make another if statement to check this 
    //      ...
     // goes on forever because you don't know how many bad entries are going to be entered
            
}
else
{
   //...
}





You could use recursion instead of a loop:
void foo()
{
  int x;
  cin >>x;
  if(badinput())
     return foo();
  else
     return x;
}



Another REALLY BAD IDEA would be to use goto. There are lots of posts about why goto is bad. If you're interested in finding out why, do a quick search, but don't use goto.
Was This Post Helpful? 0
  • +
  • -

#11 Gungnir  Icon User is offline

  • Your Imaginary Friend

Reputation: 152
  • View blog
  • Posts: 527
  • Joined: 21-May 11

Re: how to continue a loop until the user enters the correct command

Posted 18 October 2011 - 06:09 AM

View PostEowyn27, on 17 October 2011 - 07:20 PM, said:

Out of curiosity, if you I wanted to do the same using if-else statements, how would I do it?


If-else statements don't actually loop. They check if a condition holds true (not to be confused with the boolean constant true) and then executes the code within. If it doesn't hold true, it checks the next else if statement and so on and so forth. If no conditions stated hold true, it executes the code within the else statement. However, at no point does it actually loop.

However, if you wanted an if statement contained within a loop (iteration of conditional) that would be perfectly fine.


Also, another thing that I noticed is an error in your pseudo-code logic.

do (true user input) while (input!=false input)

should be something along the lines of:

do{get user input} while(input!=answer)

In your example, it would loop until the the user got the answer wrong. If that requires further elaboration let me know and I'll be glad to clarify.

This post has been edited by Gungnir: 18 October 2011 - 06:26 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1