We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,304 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

C++ Calculator

I am trying to make a calculator in C++ but I keep running into errors. Please help me.

Here is the code:

#include <iostream>
#include <cmath>
#include <string>

using namespace std;

int main()

{ //what the variables are
double calculation;
double sign;
double number1;
double number2;


    //the questions
cout<<"Enter a Number"<< endl;
cin>> number1;

cout<<"Enter Another Number"<< endl;
cin>> number2;

cout<<"Enter what you would like to do with these numbers"<< endl;
cout<<"For example * to multiply, / to divide, - to subtract, and + to add"<< endl;
cin>> sign;

    //the calculations and answer
    if  {sign=*} 
            calculation=number1*number2;
                cout<<number1<<"*"<<number2"="<<calculation<<endl;

    if  {sign=/}
            calculation=number1/number2;
                cout<<number1<<"/"<<number2"="<<calculation<<endl;

    if  {sign=-} 
            calculation=number1-number2;
                cout<<number1<<"-"<<number2"="<<calculation<<endl;

    else    {sign=+}   
                calculation=number1+number2;
                    cout<<number1<<"+"<<number2"="<<calculation<<endl;


return 0;
}
4
Contributors
4
Replies
1 Day
Discussion Span
7 Months Ago
Last Updated
5
Views
alfredgg
Newbie Poster
2 posts since Sep 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

declare sign as a char and not a double cause it makes no sense to have sign defined as double when its a character you know? try that and it should work but say sign == '*' and ect for the other operators

Rzink92
Newbie Poster
8 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Your compiler should choke on that input. The diagnostics may or may not be useful, you have not indicated.
A proper if construct looks something like:

if (condition) {
   // code for this condition
} else if (some_other_condition) {
   // code here
} else {
   // catch-all case in the if-else tree
}

In your case you want your conditions to look like variable == value or, more specifically, sign == '*'

L7Sqr
Practically a Posting Shark
851 posts since Feb 2011
Reputation Points: 253
Solved Threads: 155
Skill Endorsements: 7

See The Tutorial At W3School....

HP2-N29, HP2-K31

itsolexpert
Newbie Poster
2 posts since Sep 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Thank you all for your advice. I finally got it to work!

alfredgg
Newbie Poster
2 posts since Sep 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0735 seconds using 2.75MB