C++ Calculator Help plz

This is a discussion on C++ Calculator Help plz within the C++ Programming forums, part of the General Programming Boards category; Hi guys. I just learned C++, and to see if I have learned anything, I decided to code a calculator ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    3

    C++ Calculator Help plz

    Hi guys.

    I just learned C++, and to see if I have learned anything, I decided to code a calculator program all by myself. I am using xcode on Mac OS X 10.5, and I have coded this so far,

    Code:
    #include <iostream>
    using namespace std;
     
    int main() //Calculator program!!!!
    {
    	int add, subtract, multiply, divide, add1, add2, addAnswer, subtract1, subtract2, subtractAnswer, multiply1, multiply2, multiplyAnswer, divide1, divide2, divideAnswer;
    	cout << "Please choose the operation!";
    	if (cin >> add)
    	{
    	
    	cout << "Enter first addend!";
    	cin >> add1;
    	
    	cout << "Enter second addend!";
    	cin >> add2;
    	
    	addAnswer = (add1 + add2);
    	cout << "Answer is " << addAnswer << "!";
    	}
    	
    	if (cin >> subtract)
    	{
    	
    	cout << "Enter first minuend!";
    	cin >> subtract1;
    	
    	cout << "Enter second subtrahend!";
    	cin >> subtract2;
    	
    	subtractAnswer = (subtract1 - subtract2);
    	cout << "Answer is " << subtractAnswer << "!";
    	}
    	
    	if (cin >> multiply)
    	{
    	
    	cout << "Enter first multiple!";
    	cin >> multiply1;
    	
    	cout << "Enter second multiple!";
    	cin >> multiply2;
    	
    	multiplyAnswer = (multiply1 * multiply2);
    	cout << "Answer is " << multiplyAnswer << "!";
    	}
    	
    	if (cin >> divide)
    	{
    	
    	cout << "Enter first dividend!";
    	cin >> divide1;
    	
    	cout << "Enter second divisor!";
    	cin >> divide2;
    	
    	divideAnswer = (divide1 / divide2);
    	cout << "Answer is " << divideAnswer << "!";
    	}
    return 0;
    }
    I did cmd apple r to get to the console menu, then built and go. It says

    "Please choose the operation!"
    then I say add or something, then it says

    "The Debugger has exited with status 0."

    Can you find a problem? I need to fix it so I can learn from my mistake. This is for my learning purposes! Thx

  2. #2
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    Code:
    	int add, ... ;
    	if (cin >> add)
    This doesn't do what you think it does. Look up how to use std::cin.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    3
    I've seen this before. if I didnt add,

    Code:
    using namespace std
    then I didn't have to use

    Code:
    std::cin
    just

    Code:
    cin
    What is the difference? I'm new at this....

  4. #4
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    Exit status 0 means that it did exactly what you wanted with no errors and just exited the program. It is so fast though that you cant see the output before it exits the program. I am not sure how a console application in Mac will act when it is finishes, but if it is anything like a Windows based Console app, it will do what you ask it to and then just quit. You are going to have to put something to make it pause so that you can see the output. Try a cin right at the very end asking the user if they want to quit? This will create a pause situtation till you hit a key, then you can see what was printed on the screen.


    Right before your return 0; Put a char temp >> cin statement. This will let you see the output till you hit any key, then it will quit the program with your return 0;.
    The keyboard is the standard device used to cause computer errors!

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    3
    But in my code, if you type in "add", then another cin should pop up, the "Enter First Addend"

    I think their is something is wrong with the "if" line.

    I dunno

  6. #6
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    lol. I just skimmed through your code... yes, you are right.

    You need something like

    string userResponce;
    cin >> userResponce;

    if (userResponce == "add")
    ............

    and so forth.
    The keyboard is the standard device used to cause computer errors!

  7. #7
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    Quote Originally Posted by Caldark View Post
    I think their is something is wrong with the "if" line.
    You're right. When
    Code:
    cin >> add
    fails to read an integer, such as when you type "add" at the prompt, the expression returns false, and the blocks do not get executed. This is why your program does nothing.
    Try reading your input into a string variable.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can any1 plz make this assignment
    By jean in forum C Programming
    Replies: 17
    Last Post: 05-13-2009, 10:19 PM
  2. [Request] Need Help Plz
    By TylerD in forum Tech Board
    Replies: 4
    Last Post: 01-03-2009, 09:54 AM
  3. Anyone plz help me
    By Rose_Flowers in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 09-17-2003, 01:01 PM
  4. plz help me fix this problem
    By Joe100 in forum C++ Programming
    Replies: 8
    Last Post: 07-13-2003, 02:25 PM
  5. help plz plz
    By nsssn73 in forum C++ Programming
    Replies: 2
    Last Post: 06-03-2002, 09:44 AM

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