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

    Unhappy My program doesn't accept my input values true, although they are

    I am making a program which is going to print out a head image according to the input values entered by the user. Here is the code:

    Code:
     #include <iostream>
        #include <string>
    
        using namespace std;
    
        void Sides() 
        // draws the sides to take proportion
        {
    	      cout << "|                 |" << endl;
        }
        void Hair()
        // draws a bald hair
        {
    	     cout << "__________________" << endl;
        }
        void Hair2()
        // draws a parted hair
        {
    	    cout << "|||||||||/////////" << endl;
        }
        void Eyes()
        //draws angry eyes
        {
    	    cout<< "|  ---       ---  |" << endl;
        }
        void Nose()
        //draws a normal nose
        {
    	    cout<< "|        o        |" << endl;
        }
        void Mouth()
        //draws an angry mouth
        {
    	    cout<< "|      ____       |" << endl;
        }
        void Mouth2()
        //draws a happy mouth
        {
    	    cout<< "|    |_____|      " << endl;
        } 
        void Head1()
        //draws a face with a bald head and an angry mouth
        {
    	    Hair();
    	    Sides();
    	    Eyes();
    	    Sides();
    	    Nose();
    	    Sides();
    	    Mouth();
        }
        void Head2()
        //draws a face with parted hair and an angry mouth
        {
    	    Hair2();
    	    Sides();
    	    Eyes();
    	    Sides();
    	    Nose();
    	    Sides();
    	    Mouth();
        }
        void Head3()
        //draws a face with a bald head and a happy mouth
        {
    	    Hair();
    	    Sides();
    	    Eyes();
    	    Sides();
    	    Nose();
    	    Sides();
    	    Mouth2();
        }
        void Head4()
        //draws a face with parted hair and a happy mouth
        {
    	    Hair2();
    	    Sides();
    	    Eyes();
    	    Sides();
    	    Nose();
    	    Sides();
    	    Mouth2();
        }
    
        int main ()
        { 
    	    string headstyle, mouthstyle;
    
    	    cout << "plase enter which way you want to print out the head, with parted hair or bald." <<      endl;
    	
    	
    	    if (cin >> headstyle != "bald" || cin >> headstyle != "parted hair" ) 
    	    {
    		     	cout << "wrong input" << endl;
    	    }
    
    	    else 
    	    {
    		    cout << "plase enter which way you want to print out the motuth, happy or angry." << endl; 
    		
    		    if (cin >> mouthstyle != "happy" || cin >> mouthstyle != "angry")
    		    {
    			    cout << "wrong input" << endl;
    		    }
    		    else if (headstyle == "bald" && mouthstyle == "angry")
    		    {
    			    Head1();
    		    }
    		    else if (headstyle == "parted hair" && mouthstyle == "angry")
    		    {
    			    Head2();
    		    }
    		    else if (headstyle == "bald" && mouthstyle == "happy")
    		    {
    		        Head3();
    		    }
    		    else if (headstyle == "parted hair" && mouthstyle == "happy")
    		    {
    			    Head4();
    		    }
    	
    	
    	
    	    } 
    	
    	    cin.ignore();
    	    cin.get();
    	    return 0;
        }
    at first i tried to work program like:

    Code:
      cout << "plase enter which way you want to print out the head, with parted hair or bald." << endl;
    	         cin >> headstyle;
    	
    	         if (headstyle != "bald" || headstyle != "parted hair" )

    but it also had given the same mistake. The program compiles BUT; even if I put in the values bald or parted hair the program prints out "wrong input" then exits. If you could help, I'd be really grateful.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: My program doesn't accept my input values true, although they are

    Code:
    if (cin >> headstyle != "bald" || cin >> headstyle != "parted hair" ) 
    	    {
    		     	cout << "wrong input" << endl;
    	    }
    As you found, you can't combine stream extraction with condition testing! - as the return value of cin >> .... is the cin object if the extraction was good. See http://www.cplusplus.com/reference/i...perator%3E%3E/

    Code:
    cout << "plase enter which way you want to print out the head, with parted hair or bald." << endl;
    	         cin >> headstyle;
    	
    	         if (headstyle != "bald" || headstyle != "parted hair" )
    This is the way to do it - but you have a simple logic error. When the conditional test is being evaluated, the left hand side of || (logical or) is evaluated first and if this evaluates to true then the or is evaluated to true and the right hand side isn't checked. If the left hand side evaluates to false then the right hand side of the or is checked. If this evaluates to true then then the or test is true, but if this also evaluates to false then the or is false.

    So if you enter 'bald'
    The lhs evaluates to false, the rhs evaluates to true so the or condition is true and the program prints 'wrong input'

    If you enter 'parted hair'
    The lhs evaluates to true so the or condition is true and the program prints 'wrong input'

    What you need is logical and instead of logical or.
    Code:
    cout << "plase enter which way you want to print out the head, with parted hair or bald." << endl;
    	         cin >> headstyle;
    	
    	         if (headstyle != "bald" && headstyle != "parted hair" )
    For a logical and, the condition is only evaluated as true if both sides of the and are evaluated to true. If either evaluates to false then the wrong and condition evaluates to false. For logical and, the rhs is evaluated only if the lhs is evaluated to true. If the lhs evaluates to false then the whole condition is false and the rhs is not evaluated.

    So now if you enter 'bald'
    The lhs evaluates to false so the whole logical and condition is false.

    If you enter 'parted hair'
    The lhs evaluates to true and the rhs evaluates to false so the whole logical and condition is false.

    If you enter say 'short'
    The lhs evaluates to true and the rhs now also evaluates to true so the whole logical and condition is true and so the message is printed.

    For info about logical conditions see http://www.learncpp.com/cpp-tutorial...cal-operators/
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Nov 2014
    Posts
    2

    Re: My program doesn't accept my input values true, although they are

    Oh, I must have completely get confused with the not equal symbol. Thank you so much, saved my day!

Tags for this Thread

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