Thread: loop freak out when char entered

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    465

    loop freak out when char entered

    I just want to use a loop sort of like this, but if you entered a char it goes wild. How do I account for characters being entered as well?

    Code:
    	while(monster!=1||2||3||4){
    	cout<<"1-man\n";
    	cout<<"2-goblin\n";
    	cin>>monster;
    	}
    My computer is awesome.

  2. #2

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    I don't really see the logic behind this line of code. I've never seen a loop used like that.
    Code:
    while (cout << "Enter a number: " && !(cin >> input))
    Could you please explain this line? I know what it does, but it doesn't make sense.
    My computer is awesome.

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    What logic? The code indicates to output the question each time the user's input is invalid.

    Kuphryn

  5. #5
    Registered User
    Join Date
    Mar 2005
    Location
    New Zealand
    Posts
    20
    Quote Originally Posted by cerin
    I just want to use a loop sort of like this, but if you entered a char it goes wild. How do I account for characters being entered as well?

    Code:
    	while(monster!=1||2||3||4){
    	cout<<"1-man\n";
    	cout<<"2-goblin\n";
    	cin>>monster;
    	}
    What your code is really checking is:
    Code:
    	while(monster!=(BOOL) TRUE){
    	cout<<"1-man\n";
    	cout<<"2-goblin\n";
    	cin>>monster;
    	}
    Perhaps you should repahse that if statement...

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Actually it's:
    Code:
    while( monster != 1 || (non-zero is true, so true) )
    However, you're correct that the truth test needs to be redone. (There is no if statement however, but I know what you meant. )

    In case you, the origional poster, don't know what we're getting at: You cannot test one variable against multiple values with a single statement like that. You have to repeatedly compare the variable to each new value. Therefore, for your example to be correct, it would need to be:
    Code:
    while( monster != 1 && monster != 2 && monster != 3 && monster != 4 )
    {
        ...stuff...
    }
    Or, you could write it like so:
    Code:
    while( monster < 1 || monster > 4 )
    {
        ...do stuff...
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    *this
    Join Date
    Mar 2005
    Posts
    498
    ^ DeMorgans law, simply...boolean algebra.

    Code:
    not (1 or 2 or 3 or 4)
    after distributing, "or" becomes its opposite "and"

    Code:
    (not 1) and (not 2) and (not 3) and (not 4)
    using your example yeilds
    Code:
    ((monster != 1) && (monster != 2) && (monster != 3) && (monster != 4 ))

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    How do I account for characters being entered as well?
    You can't use the >> operator, i.e cin>>monster. That requires that you know that the input is the same type as monster. If the user may enter a different type than monster, then you need to read in the input as a string with getline():
    Code:
    char input[30]
    cin.getline(input, 30);
    Then, you can use atoi() in <cstdlib> to convert the string to a number(alpha to int). atoi() returns 0 if there isn't a leading integer in the string, otherwise it returns the integer. So, you can check for 0 which is a signal for bad input before continuing.

    I don't really see the logic behind this line of code. I've never seen a loop used like that.
    Code:
    while (cout << "Enter a number: " && !(cin >> input))
    I've never seen it before, but after doing some reading, I think I understand it.

    The <<operator returns a reference to the stream cout, a reference is a pointer which stores an address, and an address is a non-zero integer, so it evaluates to true. By the same logic, the >>operator returns a reference to the stream cin, which is an address, and an address is non-zero, so it evaluates to true. Since !true is false, the result is:

    true && false

    which evaluates to false. So, it seems like the while loop will never execute.

    However, the !operator is overloaded for stream objects, and it is programmed to check the status of the stream. If an input operation did not read the characters that were expected, then an error flag is set for the stream, and any subsequent attempts to read from the stream will fail. The !operator checks for that error flag, and returns true if it finds it, meaning there is an error. But, as long as the input matches the type of the input variable, no error flag gets set, and the !operator returns false--signaling it couldn't find an error flag, which results in the conditional:

    true && false

    That evaluates to false, and the while loop with the error message doesn't execute. But, if the wrong type of input is entered, then an error flag gets set for the cin stream, and the !operator finds it and returns true, which results in the conditional:

    true && true

    That evaluates to true, which causes the while loop error statements to execute. Those statements display an error message, reset the error flag, and remove the bad input from the stream.
    Last edited by 7stud; 04-02-2005 at 04:59 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  2. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  3. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM
  4. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM
  5. errors in class(urgent )
    By ayesha in forum C++ Programming
    Replies: 1
    Last Post: 11-10-2001, 10:14 PM