Using while loop for if statement.

This is a discussion on Using while loop for if statement. within the C++ Programming forums, part of the General Programming Boards category; I'm having a bit of trouble here trying to make this if structure loop if an unknown entry is made. ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    4

    Using while loop for if statement.

    I'm having a bit of trouble here trying to make this if structure loop if an unknown entry is made. When I enter the unknown in the gui (else statement) it will loop the else statement, but not the beginning (if statement) My variable is predeclared local
    Code:
    int g2le = 0;
    Much appreciated.

    Code:
    while ( g2le == 0 ){
        if ( g2s == 1 ){
             cout<< g;
             g2le++;
             cin.get();
             }
        else if ( g2s == 2) {
             cout<< h;
             g2le++;
             cin.get();
             }
        else if ( g2s == 3){
             cout<< i;
             g2le++;
             cin.get();
             }
        else {
             cout<< j;
             cin.get();
             }
             }
             cin.get();
    }
    Program initiated successfully

    This is the start of the program. We are using cout to display this message. Ple
    ase press enter to continue after each code block has been executed.

    This part of the program will use 'int' to declare variables with user input. Pl
    ease, enter a number.
    3
    The number you entered will be displayed right now: 3 See?

    So now we seen an example of how the code will enter a value through cin, and re
    place the value of the variable with your input. This is a local variable assign
    ment. It can constantly change as I will show you in a few moments.

    Please enter another value for a.
    4
    The variable 'a' is now: 4 instead the previous number you entered

    Now we will move onto assigning two variables at once for an 'int'. Please enter
    two numbers into the prompt. Place a space between each number please.
    2 3
    The numbers you entered are: 2 and 3.

    Now we will take these numbers and begin using math to solve different types of
    problems.

    2 x 3 equals: 6
    2 + 3 equals: 5
    2 - 3 equals: -1
    2 / 3 equals: 0.666667

    Now I will demonstrate how the 'if' statement works well with passwords or other
    various program functions.

    Lets pay a little game here, lets imagine you are in a role playing game and you
    have several options to choose from. Lets call them:

    1.Attack
    2.Defense
    3.Magic

    Go on, choose one by using numerical values
    4
    !Error! Unknown Command!!Error! Unknown Command!
    !Error! Unknown Command!
    !Error! Unknown Command!
    !Error! Unknown Command!
    !Error! Unknown Command!
    The program is not to describe anything to anyone but to help myself become more involved into coding.
    Last edited by Galens; 10-02-2008 at 11:17 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well, if g2s is not 1, 2, or 3, how does g2le change (to get out of the loop) or g2s change (to pick a different choice in the if-elseif-elseif-else chain)?

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    4
    I want the last else if to repeat the external loop, not internal. The other if, else if I want it to break the loop and continue to the next code block.

    I'm a little confused here. I tried this and I don't understand why its not working. On top of the else if repeating its own code block, the 1,2,3 values won't pause even though I'm using the cin.get();

    Code:
    while ( g2le < 1 ){
        if ( g2s == 1 ){
             cout<< g;
             g2le++;
             cin.get();
             }
        else if ( g2s == 2) {
             cout<< h;
             g2le++;
             cin.get();
             }
        else if ( g2s == 3){
             cout<< i;
             g2le++;
             cin.get();
             }
        else if ( g2s != 1,2,3){
             cout<< j;
             g2le--;
             cin.get();
             }
             }
             cin.get();
    }

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    4
    So far I got it fixed where it doesn't exit the gui upon any value entered. I'm using break statements after the cin.get(); in each internal block to prevent this. I tried continue inside the else block thinking it would continue the loop...guess not. I had to switch back from:

    Code:
    while ( g2le < 1 )
    To:

    Code:
    while (g2le == 1)
    The transition back prevented the program from exiting on all value inputs. I also changed back from the last block (else if) back to (else) I had previously tried

    Code:
    else if (g2s != 1,2,3)
    Code:
    else if (g2s != 1 || 2 || 3)
    Here is where I am so far:

    Code:
    while ( g2le == 1 ){
              int g2le = 1;
        if ( g2s == 1 ){
             cout<< g;
             g2le = g2le++;
             cin.get();
             break;
             }
        else if ( g2s == 2) {
             cout<< h;
             g2le = g2le++;
             cin.get();
             break;
             }
        else if ( g2s == 3){
             cout<< i;
             g2le = g2le++;
             cin.get();
             break;
             }
        else {
             cout<< j;
             g2le = g2le--;
             cin.get();
             continue;
             }
             }
             cin.get();
    }
    Maybe someone can shed some light for me here. I'm stumped and still cannot resolve this issue. Much appreciated

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by Galens View Post
    I'm having a bit of trouble here trying to make this if structure loop if an unknown entry is made
    'if' statements don't loop. 'while', 'do..while' and 'for' statements can loop.
    So far I got it fixed where it doesn't exit the gui upon any value entered. I'm using break statements after the cin.get(); in each internal block to prevent this. I tried continue inside the else block thinking it would continue the loop...guess not.
    A 'continue' statement does exactly what it implies. It jumps immediately to the next iteration of the loop.
    The transition back prevented the program from exiting on all value inputs. I also changed back from the last block (else if) back to (else) I had previously tried

    Code:
    else if (g2s != 1,2,3)
    Code:
    else if (g2s != 1 || 2 || 3)
    The syntax you are after is:
    Code:
    else if (g2s != 1 && g2s != 2 && g2s != 3)
    Or to put it a shorter way:
    Code:
    else if (g2s < 1 || g2s > 3)
    Here is where I am so far:
    Code:
    ...
             g2le = g2le++;
    ...
    That line is undefined behaviour. You can't modify g2le twice in the same statement. You want just:
    Code:
    g2le++;
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    4
    I did it!!!!!

    I noticed a common sense error I made before the while. I wasn't asking for input inside the while, which was preventing any new values to be entered after the cin>> g2s; It was retaining the value 4(unknown command) and repeating the while with the that value. I hope I explained that correctly....

    Fixed:

    Code:
    while ( g2le == 1 ){
        cout<<"1.Attack\n2.Defense\n3.Magic\n";
        cin>> g2s;    
        if ( g2s == 1 ){
             cout<< g;
             g2le++;
             cin.get();
             break;
             }
        else if ( g2s == 2) {
             cout<< h;
             g2le++;
             cin.get();
             break;
             }
        else if ( g2s == 3){
             cout<< i;
             g2le++;
             cin.get();
             break;
             }
        else { 
             continue;
             }
             }
             cin.get();
    }
    Thanks all who helped!
    Last edited by Galens; 10-03-2008 at 04:20 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Visual Studio Express / Windows SDK?
    By cyberfish in forum C++ Programming
    Replies: 23
    Last Post: 01-22-2009, 02:13 AM
  2. infinite while loop inside a switch statement.
    By tummala_005 in forum C Programming
    Replies: 6
    Last Post: 12-15-2008, 05:46 PM
  3. Switch statement = infinite loop
    By Lucid003 in forum C++ Programming
    Replies: 10
    Last Post: 10-10-2005, 01:46 AM
  4. for loop or while loop
    By slamit93 in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2002, 05:13 AM
  5. explain this loop statement?
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 05-05-2002, 03:46 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21