Thread: loop not initializing

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    6

    loop not initializing

    okay so i am working on this very basic game to help cement some programming information i going over into my brain. i can't figure out what is wrong with my loop.

    the code gets to the while loop and the gets stuck.. anyone know whats going on?

    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
        int main(int argc, char *argv[]){
                 
        int GameEnd =0;
        int Option;
        int RockysPow =3;
        int ApollosPow =10;
                
                cout<<"Welcome to Rockys Punch Out Adventure!\n"
                   "created by sean wilson.\n";
                   
                   system("PAUSE");
                   while (GameEnd != 1);{        
                         cout<<"would your like to..\n"; 
                         cout<< "1: train to fight\n"; 
                          cout<< "2: take on Apollo Creed\n";
                          
                          cin>> Option;
                          
                          if (Option == 1){
                          RockysPow++;
                          cout<<RockysPow;
                          }
                          
                           else {if (Option == 2){cout<<"option 2\n";}
                           else {cout<< Option <<" is not a valid entry.";}
                           
                   }
        }
            system("PAUSE");
            return EXIT_SUCCESS;
        }
    thanks in advance!

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Quote Originally Posted by bbeltwilson View Post
    Code:
    //...
                   while (GameEnd != 1);{        
                         cout<<"would your like to..\n"; 
    //...
    for things like while loops and if statements, the code they will execute is either the next (single) statement or code block. by code block, it is a group of statements surrounded by curly braces. in your case, the while loop will do (as with all while loops,) either the next statement or code block after your while loop, which in your code is only a semi colon ";", a single statement. a while loop is different from a regular statement, so remove the semicolon, so that it will execute the code block (stuff surrounded by the curly braces) on each iteration which is what you want.

    edit: here is example code to further illustrate what i have described
    Code:
    // the code that is executed when the while condition is true is either the next single statement or code block,
    // which is the "myVariable=someValue;" statement only in this case
    while ( myVariable != someValue )
        myVariable = someValue;
    anotherStatement;
    
    // the code that is executed when the while condition is true is either the next single statement or code block,
    // which is the block of statements surrounded by curly braces { and } in this case
    while ( myVariable != someValue )
    {
        myVariable = someValue;
        printf(...);
        myOtherVar = anotherValue;
    }
    myVariable++ ;
    Last edited by nadroj; 12-28-2008 at 09:38 PM.

  3. #3
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    Code:
    while (GameEnd != 1);
    The syntax for while loops and do loops is
    Code:
    while(condition){}
    and
    Code:
    do{}while(condition);
    Notice the semicolons. I think every new programmer makes this mistake at least once.

    edit:
    I still have to complain about your indentation.
    Last edited by NeonBlack; 12-28-2008 at 09:35 PM.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  4. #4
    Registered User
    Join Date
    Dec 2008
    Posts
    6
    Quote Originally Posted by NeonBlack View Post
    Code:
    while (GameEnd != 1);
    The syntax for while loops and do loops is
    Code:
    while(condition){}
    and
    Code:
    do{}while(condition);
    Notice the semicolons. I think every new programmer makes this mistake at least once.

    edit:
    I still have to complain about your indentation.

    sorry im working on it iv been at this whole programming thing for 2 days.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 10:44 AM
  2. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 12:38 PM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  4. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM
  5. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM