Can't figure out what is wrong with this

This is a discussion on Can't figure out what is wrong with this within the C++ Programming forums, part of the General Programming Boards category; I am trying to design an algorithm for a simple calculator function. But everytime i enter in a symbol to ...

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

    Can't figure out what is wrong with this

    I am trying to design an algorithm for a simple calculator function. But everytime i enter in a symbol to have it perform an operation it always adds. Also, the while loop never terminates even when i say no.

    #include <iostream>
    using namespace std;
    Code:
    int main ()
    {
    	// Variable Declarations
    	int num1, num2, num3, num4;
    	char letter, symbol;
        
        // Prompt 
    	cout << "This Program will add, subtract, multiply, and divide" 
    		 << "the two numbers you enter." << endl;
        cout << endl;
    
    	while (letter = 'y')
    	{ 
    		cout << "Please enter two numbers: ";
    		cin >> num1 >> num2;
    		cout << endl;
    
    		cout << "Please input the operation you would like to use: ";
    		cin >> symbol;
    		cout << endl;
    
    		if (symbol = '+')
    		 {
    			 num3 = num1 + num2;
    		     cout << "The sum is: " << num3 << endl;
    		 }
            else if (symbol = '-')
    		 {  
    		     num3 = num1 - num2;
    		     cout << "The difference is: " << num3 << endl;
    		 }
    		else if (symbol = '*')
    		 {   
    		     num3 = num1 * num2;
    		     cout << "The product is: " << num3 << endl;
    		  }
    		else if (symbol = '/')
    		  {  
    		     num3 = num1 / num2;
    		     num4 = num1 % num2;
    			 cout << "The quiotient is: " << num3 << "The remainder is: " << num4 << endl;
    		  }
    		else
    			cout << "Invalid symbol." << endl;
    	
    		cout << "Would you like to run the program again [y/n] ";
    		cin >> letter;
    		cout << endl;
    	}
            cout << "End of Program" << endl;
    	return 0;
    
    }

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    '==' is the comparison operator.
    '=' is the assignment operator.

    Common mistake.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    137
    Also, where are you assigning letter to 'y'? (Besides in the while loop, where you obviously don't want to. That is the reason your while loop will not terminate.)

  4. #4
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    with regards the previous post and the code you supplied, i am
    assuming that you want to loop over while the user enters 'y'.
    you have:

    Code:
    while (letter = 'y')
    which as already pointed out, should be changed to:

    Code:
    while (letter == 'y')
    thats fine, but then when you start your program, what value
    is in the variable y? when you declare a variable, it already has
    some junk value in it (left over from something else). you will
    need to initialise the variable letter as follows:

    Code:
    char letter = 'y', symbol;
    otherwise, even if you make the changes suggested, the program
    still wont work. just trying to explain something that is easily
    overlooked by beginners
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help! I cannot figure out what I'm doing wrong here!
    By chsindian595 in forum C Programming
    Replies: 2
    Last Post: 08-02-2008, 04:18 AM
  2. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 02:04 PM
  3. please help, i can't figure out what's wrong
    By Leeman_s in forum C++ Programming
    Replies: 1
    Last Post: 06-05-2002, 10:13 PM
  4. What is wrong here?????
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 05-23-2002, 11:51 AM
  5. What did i do wrong? Code inside
    By The Rookie in forum C Programming
    Replies: 2
    Last Post: 05-18-2002, 09:14 AM

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