Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

[C++] If else infinite loop?

loop

  • Please log in to reply
20 replies to this topic

#1 SterAllures

SterAllures

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 188 posts

Posted 05 April 2009 - 09:33 AM

Hiya All,

I'm trying some things out with C++ did it a long time ago and now I'm picking it up again. I'm just trying to make a few things to understand all the loops again.

But now I got this code..
You can choose 3 answers until you got the right one, but the thing is I made a kind of pin(not sure how you call it) but when you enter the wrong answer is goes back to that pin(that pin is at the start of the program).

The thing is, if people answer but a number above 3 it'll say sorry the value must be between 1 and 3.

But when I enter a letter it says the same but gives an infinite loop and doesn't come out of it.

I want that if people answer a character or string that they have to guess ago.

I hope someone could help me with this

Here's the code:
//Menu Chooser


#include <iostream>
using namespace std;

int main()
{
    
    cout << " Is CodeCall A Great Site\n\n";
    cout << "1 -- Yeah Totally!\n";
    cout << "2 -- Not Sure..\n";
    cout << "3 -- No, it's an awful site!\n";
    
    TryAgain: //this is a kind of pin where you can come back to later in the code
             
             
    int choice;
    cout << "Choice: ";
    cin >> choice;  //get here the input from the user
    
              if (choice == 1)
                {
                cout << "You're Totally Right!!\n\n";
                }
                
                else if (choice ==2)
                {
                cout << "I'm Sorry But If You Knew The Forum YOu Wouldn't Have Doubts \n\n";
                goto TryAgain;     //Here the loop will be transported back to the ping 'TryAgain'
                }
                
                else if (choice == 3)
                {
                cout << "Are You Serious?! I'm Sorry That Can't Be Correct...\n\n";
                goto TryAgain; 
                }  
                
                else //this only works when you enter a wrong number. with a char it'll give an infinite loop, why?
                {
                cout << "Sorry you must answer a number from 1 to 3\n";
                goto TryAgain;    
                }
                          

        system("pause"); //Pause the program otherwise the program will shut down
        return 0;

}
                   
    

I Appreciate the help!
//SterAllures

Edited by SterAllures, 05 April 2009 - 11:00 PM.

  • 0
4d 65 6c 76 69 6e 0d 0a
"If happiness was the national currency, what kind of work would make you rich?"

#2 Phoenixz

Phoenixz

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 222 posts

Posted 05 April 2009 - 09:56 AM

try using a switch and case, that should work fine for it :}
  • 0
Posted Image

#3 SterAllures

SterAllures

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 188 posts

Posted 05 April 2009 - 09:58 AM

try using a switch and case, that should work fine for it :}


Ya I Had that before but I don't know how to re ask the question if it isn't 1?
SHould I than use goto TryAgain instead of break?
  • 0
4d 65 6c 76 69 6e 0d 0a
"If happiness was the national currency, what kind of work would make you rich?"

#4 SterAllures

SterAllures

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 188 posts

Posted 05 April 2009 - 10:07 AM

Oke I Followed your advice with the switch and case and I got this:

//Menu Chooser
//demostrates switch statement

#include <iostream>
using namespace std;

int main()
{
    
    cout << " Is CodeCall A Great Site\n\n";
    cout << "1 -- Yeah Totally!\n";
    cout << "2 -- Not Sure..\n";
    cout << "3 -- No, it's an awful site!\n";
    
    TryAgain: //this is a kind of pin where you can come back to later in the code
             
             
    int choice;
    cout << "Choice: ";
    cin >> choice;  //get here the input from the user
    
              switch (choice)
                {
                case 1:
                cout << "You're Totally Right!!\n\n";
                break;
                
                case 2:
                cout << "I'm Sorry But If You Knew The Forum You Wouldn't Have Doubts b\n\n";
                goto TryAgain;     //Here the loop will be transported back to the ping 'TryAgain'
                
                
               case 3:
                cout << "Are You Serious?! I'm Sorry That Can't Be Correct...\n\n";
                goto TryAgain; 
                
                
                default:
                cout << "Sorry you must answer a number from 1 to 3\n";
                goto TryAgain;    
                }
                          

        system("pause"); //Pause the program otherwise the program will shut down
        return 0;

}

But it still gives an infinite loop when I answer a char or string :( any other advice :D?

Edited by SterAllures, 05 April 2009 - 11:00 PM.

  • 0
4d 65 6c 76 69 6e 0d 0a
"If happiness was the national currency, what kind of work would make you rich?"

#5 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 05 April 2009 - 04:10 PM

Why are you using a goto? I would use a while loop, with a bool to indicate invalid input.
//Menu Chooser
//demostrates switch statement

#include <iostream>
using namespace std;

int main()
{
    bool valid=false;
    cout << " Is CodeCall A Great Side\n\n";
    cout << "1 -- Yeah Totally!\n";
    cout << "2 -- Not Sure..\n";
    cout << "3 -- No, it's an awful site!\n";
    
    //TryAgain: //this is a kind of pin where you can come back to later in the code
    while (!valid) 
    {         
       valid=true;       
       int choice;
       cout << "Choice: ";
       cin >> choice;  //get here the input from the user
    
              switch (choice)
                {
                case 1:
                cout << "You're Totally Right!!\n\n";
                break;
                
                case 2:
                cout << "I'm Sorry But If You Knew The Forum You Wouldn't Have Doubts b\n\n";
                valid=false;     //Here the loop will be transported back to the ping 'TryAgain'
                
                
               case 3:
                cout << "Are You Serious?! I'm Sorry That Can't Be Correct...\n\n";
                valid=false; 
                
                
                default:
                cout << "Sorry you must answer a number from 1 to 3\n";
                valid=false;    
                }
        }                  

        system("pause"); //Pause the program otherwise the program will shut down
        return 0;

}

  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#6 SterAllures

SterAllures

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 188 posts

Posted 05 April 2009 - 11:22 PM

Ow Never thought of that with a boolean really nice style.

But when I try the program it still gives an infinite loop when I enter a char or string.

I think the problem is that I declare choice as an int. I think that is why it gives an infinite loop the moment you enter a char.

Is there a way to say when a string or char is insert you have to choose a number between 1 and 3 and only give the message once....


P.S. I made a huge mistake in the source code. the question is spelled as Side instead of Site :P:P! Srry people!
  • 0
4d 65 6c 76 69 6e 0d 0a
"If happiness was the national currency, what kind of work would make you rich?"

#7 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 06 April 2009 - 03:10 AM

char fixes it. You were also missing a couple break statements.
//Menu Chooser
//demostrates switch statement

#include <iostream>
using namespace std;

int main()
{
  bool valid=false;
  cout << " Is CodeCall A Great Side\n\n";
  cout << "1 -- Yeah Totally!\n";
  cout << "2 -- Not Sure..\n";
  cout << "3 -- No, it's an awful site!\n";
  
  //TryAgain: //this is a kind of pin where you can come back to later in the code
  while (!valid) 
  {         
    valid=true;       
    char choice;
    cout << "Choice: ";
    cin >> choice;  //get here the input from the user
    
    switch (choice)
    {
      case '1':
        cout << "You're Totally Right!!\n\n";
        break;
      
      case '2':
        cout << "I'm Sorry But If You Knew The Forum You Wouldn't Have Doubts b\n\n";
        valid=false;     //Here the loop will be transported back to the ping 'TryAgain'
        break;
      
      case '3':
        cout << "Are You Serious?! I'm Sorry That Can't Be Correct...\n\n";
        valid=false; 
        break;
      
      default:
        cout << "Sorry you must answer a number from 1 to 3\n";
        valid=false;    
    }
  }                  
  
  system("pause"); //Pause the program otherwise the program will shut down
  return 0;

}

  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#8 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 06 April 2009 - 03:11 AM

char fixes it. You were also missing a couple break statements.
//Menu Chooser
//demostrates switch statement

#include <iostream>
using namespace std;

int main()
{
  bool valid=false;
  cout << " Is CodeCall A Great Side\n\n";
  cout << "1 -- Yeah Totally!\n";
  cout << "2 -- Not Sure..\n";
  cout << "3 -- No, it's an awful site!\n";
  
  //TryAgain: //this is a kind of pin where you can come back to later in the code
  while (!valid) 
  {         
    valid=true;       
    char choice;
    cout << "Choice: ";
    cin >> choice;  //get here the input from the user
    
    switch (choice)
    {
      case '1':
        cout << "You're Totally Right!!\n\n";
        break;
      
      case '2':
        cout << "I'm Sorry But If You Knew The Forum You Wouldn't Have Doubts b\n\n";
        valid=false;     //Here the loop will be transported back to the ping 'TryAgain'
        break;
      
      case '3':
        cout << "Are You Serious?! I'm Sorry That Can't Be Correct...\n\n";
        valid=false; 
        break;
      
      default:
        cout << "Sorry you must answer a number from 1 to 3\n";
        valid=false;    
    }
  }                  
  
  system("pause"); //Pause the program otherwise the program will shut down
  return 0;

}

  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#9 SterAllures

SterAllures

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 188 posts

Posted 06 April 2009 - 04:09 AM

Yaa! It works now!

but I find it weird I declare a char while I want an integer answer.

And also another downside with declaring a char if I enter a String(or more than 1 character it gives the notification of "Sorry you must answer a number from 1 to 3"
but it gives the notification as many times, as there are character.

so when I would enter 'hello' It will give the notification 5 times.
first it'll say 'Sorry you must answer a number from 1 to 3'
and after that it says the following for times this:
Choice : Sorry you must answer a number from 1 to 3

How to solve that problem :P
  • 0
4d 65 6c 76 69 6e 0d 0a
"If happiness was the national currency, what kind of work would make you rich?"

#10 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 06 April 2009 - 05:34 AM

1 is both a character and a number. You can use member methods of cin to grab input as it is entered, rather than waiting for the enter key to be pressed. (getch() comes to mind).
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#11 SterAllures

SterAllures

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 188 posts

Posted 06 April 2009 - 07:17 AM

mmm Oke I'll try that later and let you know if I worked it out ;). thanks for your help!
  • 0
4d 65 6c 76 69 6e 0d 0a
"If happiness was the national currency, what kind of work would make you rich?"

#12 SterAllures

SterAllures

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 188 posts

Posted 06 April 2009 - 07:52 AM

1 is both a character and a number. You can use member methods of cin to grab input as it is entered, rather than waiting for the enter key to be pressed. (getch() comes to mind).


I think I got the answer!!!! thanks to your getch() that's used alot in C and in C++ most people use cin.get() so I tried that look at this!

//Menu Chooser
//demostrates switch statement

#include <iostream>
using namespace std;

int main()
{
  bool valid=false;
  cout << " Is CodeCall A Great Site\n\n";
  cout << "1 -- Yeah Totally!\n";
  cout << "2 -- Not Sure..\n";
  cout << "3 -- No, it's an awful site!\n";
  
  //TryAgain: //this is a kind of pin where you can come back to later in the code
  while (!valid) 
  {         
    valid=true;       
    char choice;
    cout << "Choice: ";
    cin >> choice;  //get here the input from the user
    [B][I][COLOR="Red"]cin.ignore(255, '\n');[/COLOR][/I][/B]
    


    switch (choice)
    {
      case '1':
        cout << "You're Totally Right!!\n\n";
        break;
      
      case '2':
        cout << "I'm Sorry But If You Knew The Forum You Wouldn't Have Doubts b\n\n";
        valid=false;     //Here the loop will be transported back to the ping 'TryAgain'
        break;
      
      case '3':
        cout << "Are You Serious?! I'm Sorry That Can't Be Correct...\n\n";
        valid=false; 
        break;
      
      default:
        cout << "Sorry you must answer a number from 1 to 3\n";
            //cin.get(choice, 1);
        valid=false;    
    }
  }                  
  
  system("pause"); //Pause the program otherwise the program will shut down
  return 0;

}


Is there a possible way to let the program read only the first entry? so when you would enter 10 numbers it would only read the first entry?

cuase when I now enter alot of values with a 3 in between it will give the answer 'Are You Serious?! I'm Sorry That Can't Be Correct..'

any way to solve that
  • 0
4d 65 6c 76 69 6e 0d 0a
"If happiness was the national currency, what kind of work would make you rich?"





Also tagged with one or more of these keywords: loop

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download