Annoying problem...

This is a discussion on Annoying problem... within the C++ Programming forums, part of the General Programming Boards category; It's just a little nooby problem. In my book it said to try and design a program that would take ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    20

    [RESOLVED] Annoying problem...

    It's just a little nooby problem. In my book it said to try and design a program that would take two numbers and add or subtract them (With input from the user). Anyway this is what I tried to do:

    Code:
    void main()
    {
    	int no1, no2;
    	char plusminus;
    	
    	cout << "\n- Please enter 2 integers to be added or subtracted -";
    	cout << "\n\nNumber 1: ";
    	cin >> no1;
    	
    	while(plusminus != '+' || plusminus != '-')
    		{
    			cout << "\n\nWould you like to add or subtract? (+/-): ";
    			cin >> plusminus;
    		
    			switch(plusminus)
    			{
    				case '+':
    					plusminus = '+';
    				break;
    				case '-':
    					plusminus = '-';
    				break;
    				default:
    					cout << "\n\nERROR: That is not an option.\n";
    			}
    		}
    	
    	cout << "\n\nNumber 2: ";
    	cin >> no2;
    	
    	if(plusminus == '-')
    		cout << endl << no1 << " " << plusminus << " " << no2 << " = " << no1 - no2;
    	else
    		cout << endl << no1 << " " << plusminus << " " << no2 << " = " << no1 + no2;
    }
    The problem is when you try and select whether it's plus or minus it just loops even if you select + or -...

    Thanks very much if you can help.

    -Marlon
    Last edited by StickyGoo; 04-14-2006 at 01:41 PM.

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    while(plusminus != '+' || plusminus != '-')
    should be
    Code:
    while(plusminus != '+' && plusminus != '-')
    In your version it would have to be a + and a - at the same time to skip the loop
    Kurt

  3. #3
    Registered User
    Join Date
    Apr 2006
    Location
    Singapore
    Posts
    2
    Heres another solution using if else instead of the while loop to avoid the confusion.

    Code:
    int main()
    {
        using namespace std;
        
    	int no1, no2;
    	char plusminus;
    	
    	cout << "\n- Please enter 2 integers to be added or subtracted -";
    	cout << "\n\nNumber 1: ";
    	cin >> no1;
    	cout << "\n\nWould you like to add or subtract? (+/-): ";
    	cin >> plusminus;
    	cout << "\n\nNumber 2: ";
    	cin >> no2;
    	
    	if(plusminus == '+')
    	             cout<<no1+no2;
                  
                  else
                  if(plusminus == '-'){
                               cout<<no1+no2;
                               }                           
                               else
                               cout<<"Input error";
                           
                                                    
                  return 0;
    
    }

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    Also make sure you initialize your variables before you use them. In this case, you use plusminus before it is initialized. Set it to any character you want other than '+' or '-' so that the loop will be entered the first time.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    20
    Thank you all very much for your help!

    ZuK:

    Your solution worked (Thank your very much ). But the reason I did the '||' instead of the '&&' is because I read in the book that '&&' would mean 'and'.

    It says if(A&&B) means If A and B are true.

    It also says if(A||B) means If A or B are true.

    Is it a mistake or am I looking at that sentence from a strange view. Thanks anyway though

    Jreak:


    I always seem to do things the hard way I knew there had to be a simpler solution... Thank you very much for showing it to me

    Daved:

    Thank you for the advice, I'll try to do that from now on

  6. #6
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by StickyGoo
    Is it a mistake or am I looking at that sentence from a strange view. Thanks anyway though
    Yes you do but it is a very common mistake if you use negations.
    mybe an easier to read solution would be
    Code:
    while( ! (plusminus== '+' || plusminus == '-'))
    Kurt

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    20
    Quote Originally Posted by ZuK
    Code:
    while( ! (plusminus== '+' || plusminus == '-'))
    So does that mean....

    While Plusminus is not equal to '+' And It's also not equal to '-' Then....

  8. #8
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Yes it does. Both of my versions do.

    Code:
    ( not A ) and ( not B )
    is the same as
    Code:
    not ( A or B )
    Kurt

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    20
    Brilliant. Thank you very much for your help, I really appreciate it.

    Have a good easter too!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  3. Annoying Message Box Problem
    By Necrofear in forum C++ Programming
    Replies: 14
    Last Post: 07-02-2006, 11:25 AM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  5. Replies: 5
    Last Post: 11-07-2005, 10:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21