CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Nov 2008
    Posts
    4

    What's wrong with this??

    So i'm making a stupid little game( i'm a beginner)

    I want to make it so that if the user inputs something wrong it will just loop back around, so they can enter it again.

    Code:
    int whereToGo;
           
         
         do {
             cin >> whereToGo;
             if(whereToGo == 1); {
             cout << " You go outside and you are stopped by a guard.\n";
             cout << " He asks you to go on a quest for him";
             }
             else { 
                  if(whereToGo == 2);
                  cout << " You have chosen to stay and look around\n";
                  cout << " You look around the castle is made of marble\n";
                  cout << " There are wealthy people all around you\n";
                  }
                  }  while (whereToGo < 3);
    any suggestions are welcome thanks

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: What's wrong with this??

    if(whereToGo == 1);

    Your braces don't look right in your else and second if statement either.
    Last edited by GCDEF; December 4th, 2008 at 02:23 PM.

  3. #3
    Join Date
    Nov 2008
    Posts
    4

    Re: What's wrong with this??

    Grr i tried to fix it but i still don't get it :P

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: What's wrong with this??

    Quote Originally Posted by jesus45 View Post
    Grr i tried to fix it but i still don't get it :P
    What did you try? I assume you got the point that your if statements shouldn't be terminated with a semi-colon?

  5. #5
    Join Date
    Nov 2008
    Posts
    4

    Re: What's wrong with this??

    this is what i have now.

    Code:
     int whereToGo;
           
         
         do {
             cin >> whereToGo;
             if(whereToGo == 1); 
             cout << " You go outside and you are stopped by a guard.\n";
             cout << " He asks you to go on a quest for him";
             }
             else  
                  if(whereToGo == 2);
                  cout << " You have chosen to stay and look around\n";
                  cout << " You look around the castle is made of marble\n";
                  cout << " There are wealthy people all around you\n";
                  }  while (whereToGo < 3);
    Still doing something wrong though.

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: What's wrong with this??

    Quote Originally Posted by jesus45 View Post
    this is what i have now.

    Code:
     int whereToGo;
           
         
         do {
             cin >> whereToGo;
             if(whereToGo == 1); 
             cout << " You go outside and you are stopped by a guard.\n";
             cout << " He asks you to go on a quest for him";
             }
             else  
                  if(whereToGo == 2);
                  cout << " You have chosen to stay and look around\n";
                  cout << " You look around the castle is made of marble\n";
                  cout << " There are wealthy people all around you\n";
                  }  while (whereToGo < 3);
    Still doing something wrong though.
    Again - sigh - don't put a semi-colon after your if statement.

    Anything you want to execute if the if statement is true should be enclosed in a block using braces.
    Code:
    if(whereToGo == 1)
    {  
             cout << " You go outside and you are stopped by a guard.\n";
             cout << " He asks you to go on a quest for him";
    }

  7. #7
    Join Date
    Jan 2007
    Posts
    143

    Wink Re: What's wrong with this??

    int whereToGo;


    while ((whereToGo !=1) || (whereToGo!=2))
    {

    cout<<"Enter I/p"<<endl;
    cin >> whereToGo;

    if(whereToGo == 1)
    {
    cout << " You go outside and you are stopped by a guard.\n";
    cout << " He asks you to go on a quest for him";
    break;
    }
    else {
    if(whereToGo == 2)
    {
    cout << " You have chosen to stay and look around\n";
    cout << " You look around the castle is made of marble\n";
    cout << " There are wealthy people all around you\n";
    break;
    }
    }


    }

  8. #8
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: What's wrong with this??

    ((whereToGo !=1) || (whereToGo!=2))

    Think about that. See if you can come up with a number where that is never true.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured