Thread: comparison problem

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    2

    comparison problem

    Hi, I am new to C++ so this may seem like a stupid problem, but it has me stumped.


    What I'm supposed to do here, is compare the value in mysign to +. Add if it is +, subtract if it is -, and display error message if it is neither.
    this is what I have so far:

    Code:
    #include <iostream>
    using namespace std;
    
    main()
    {	
    
    	int first, second, result=0; 
    	char mysign;
    
    	cout<<"Please input an integer. \n";
    	cin>> first;
    
    	cout<<"Please input an integer once again. \n";
    	cin>> second;
    
    	cout<<"Please input + or - \n";
    	cin>> mysign;
    
        if (+ == mysign) ; {
    		cout<< first+second;
    		cin>> result;
    	}
    	else if(- == mysign) ; {
    		cout<< first-second;
    		cin>> result;
    	}
    	else ;{
    		cout<< "Cannot perform an operation.\n";
    	}
    
    	cout<<"The variable stored in result is:" << result << endl;
    
    	cin.get();
    	return 0;
    }
    There are probably many things wrong here, please help me correct them. I'm lost.

  2. #2
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    mysign is a char. chars are reffered to with single quotes.

    + <---That's an operator

    '+' <-----That's a character

    When your program looks at mysign, make sure you're using the right one.
    There is a difference between tedious and difficult.

  3. #3
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    You need to put " " around the symbols. Right now you are saying "minus from itself mysign" you want it to be "-" == mysign so that it is comparing the 2.

  4. #4
    1479
    Join Date
    Aug 2003
    Posts
    253
    Code:
    if ('+' == mysign) ; {
    Remove the ; from that line and from
    Code:
    if ('-' == mysign) ; {
    Knowledge is power and I want it all

    -0RealityFusion0-

  5. #5
    I Write C++ Apps, Sue Me.
    Join Date
    Feb 2006
    Location
    In My Computer
    Posts
    44
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {	
    
    	int first, second, result=0; 
    	char mysign;
    
    	cout<<"Please input an integer. \n";
    	cin>> first;
    	cin.ignore();
    
    	cout<<"Please input an integer once again. \n";
    	cin>> second;
    	cin.ignore();
    
    	cout<<"Please input + or - \n";
    	cin>> mysign;
    	cin.ignore();
    
        if (mysign=='+')  {
            result = first + second;
    	}
        if(mysign=='-')  {
            result = first - second;
    	}
    
    	cout<<"The variable stored in result is: " << result << endl;
    
    	cin.get();
    	return 0;
    }
    Here, I cleaned up the code for you.

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    2
    Thank you all very much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. Problem with comparison
    By black187 in forum C Programming
    Replies: 4
    Last Post: 05-11-2006, 04:14 AM