Click here to Skip to main content
15,923,006 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Ok I want to loop an if statement so that there are only two choices as it were. I have as follows:

cout << "Please enter A to use a or P to use p:"; cin >>; apChoice; cout <<; endl;
    
if ((apChoice == 'A') || (apChoice == 'a'))
{
  void aChoice();    
}
else if ((pChoice) == 'P' || (pChoice == 'p'))
{
  void pChoice();
}
else
{
  cout <<; "Wrong choice please choose again.";
}


this bottom else statement I want it so that if someone enters something other than A or P it asks the question again. I just can't seem to get it to work.
Posted
Updated 13-Nov-10 7:39am
v3
Comments
Abhinav S 14-Nov-10 1:15am    
You would probably be better off using a switch case statment.

Hi,

The simplest infinite loop:
for (;;)
{
   // your code 
}
That's what you requested but now your user is stuck in that block :(
Maybe you should add an exit command :)
cheers,
AR

Edit: removed 'as posted' after reading Ash's answer
 
Share this answer
 
v2
There's a few things wrong with your code, unless I've really misunderstood what you're up to (which is possible...).

The first one is that when you write:

void aChoice();

or

void pChoice();


you're prototyping the functions, not calling them. If you really want to call them you need to knock the void off the front.

I'd assume you mean aChoice in the second conditional statement as well. While the code you've got may compile (not sure of the rules for converting function pointers to arbitrary pointers) what it looks like you're doing is comparing the address of the function pChoice with a couple of characters. It might be worth cranking up your compiler warnings to the top level (/W4 /WX on Visual C++) as that might give you some more guidance.

Now, having sorted that out... What it looks like you want to do is loop around while the user hasn't entered what you expected him or her to, otherwise you want to execute one of the functions and bail out of the program. If you don't want to bail out then you don't want to do what I'm going to suggest!!

So how do you loop until the input is what you expect? I'd be tempted to use a while loop (a do/while might actually work better, I'm just a bit adverse to them for no logical reason I can put my finger on). The conditional for the while loop is similar to the two conditions you check later:

char aChoice = ' ';

while( ( aChoice != 'A' ) && ( aChoice != 'a' ) && ( aChoice != 'P' ) && ( aChoice != 'p' ) )
{
    // Code as before in here...
}


Now while this code is okay, it's a bit of a bugger to read. My eyes when crosseyed while reading it. You might want to look at hiding the conditionals in other functions to make it easier to read. Another technique you could look at might be a jump table but that's probably overkill for this sort of problem.

Cheers,

Ash
 
Share this answer
 
Comments
Henry Minute 14-Nov-10 14:10pm    
Good answer. :)
To do what you want, you need to put your above code into a method, and have the method call itself when want to loop. This is called recursion.

I'd question the need for this practically, you'd be much better off with a do/while or while loop in all probability.
 
Share this answer
 
I think that the method which mentioned by previous member is so difficult dear because the code has just one loop with if statement and it's not complex enough to require recursion state...

I think that the loop statement of while or do/while is the suitable case in order to solve this code with all required probabilities.
 
Share this answer
 
v2
A set of statements could be executed multiple times either by using recursion or iteration (for. do/while and while loops). Iteration is faster in most cases. Recursion would cause a function to be called again and again. As function calls use up your stack you would eventually end up with a stack overflow if the process continues too long. Iteration handles larger number of repeats better than recursion.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900