Need help with simple program please

 DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

1. Registered User
Join Date
Mar 2007
Posts
4

## Need help with simple program please

I'm trying to write a simple calculator program and it mostly runs ok except the multiplication and division part of the calculations. It just returns the first number entered when I try and do these. Does anyone have any idea what I have done wrong or need to add to this program?
Here's the code:

#include <iostream>

using namespace std;

int main()
{
float num1;
float num2;
float total;

cout << "Enter an equation i.e.(1+1) : " << endl;
cin >> num1 >> num2;

if(num1 + num2)
{
total = num1 + num2;
cout << "The total is " << total << endl;
}
else if (num1 - num2)
{
total = num1 - num2;
cout << "The total is " << total << endl;
}
else if (num1 * num2)
{
total = num1 * num2;
cout << "The total is " << total << endl;
}
else if (num1 / num2)
{
total = num1 / num2;
cout << "The total is " << total << endl;
}

return 0;
}

Thanks for the help! :)

2. Registered User
Join Date
Jan 2005
Location
UK
Posts
604
Hi,
by the looks of it I have to wonder why you say that the program works mostly ok. ;-)
The way you intend to work your program you should read in a string and then parse it for numbers and operators.
Code:
```if(num1 + num2 != 0)
{
total = num1 + num2;
cout << "The total is " << total << endl;
}
else if (num1 - num2 != 0)
{
total = num1 - num2;
cout << "The total is " << total << endl;
}
else if (num1 * num2 != 0)
{
total = num1 * num2;
cout << "The total is " << total << endl;
}
else if (num1 / num2 != 0)
{
total = num1 / num2;
cout << "The total is " << total << endl;
}```
which is probably not what you intended.
Once you extracted your operator from the input string you want to use something like
Code:
```string mathOperator;
// ... assign the operator
// ...
if(mathOperator == "+")
{
result = num1 + num2;
}
else if(mathOperator == "-")
{
result = num1 - num2;
}
....```
Hope that get's you on the right track?!

Cheers,

D

3. Registered User
Join Date
Mar 2007
Posts
4
I have made changes to it and I keep getting these stupid errors. I don't know if there is something weird going on with my visual studio or what but here is the code I have now:

string mathOperator;
string +;
string -;
string *;
string /;
float num1;
float num2;
float total;

cout << "Enter an equation i.e.(1+1) : " << endl;
cin >> num1 >> mathOperator >> num2;

if (mathOperator == "+")
{
total = num1 + num2;
cout << "The total is " << total << endl;
}
else if (mathOperator == "-")
{
total = num1 - num2;
cout << "The total is " << total << endl;
}
else if (mathOperator == "*")
{
total = num1 * num2;
cout << "The total is " << total << endl;
}
else if (mathOperator == "/")
{
total = num1 / num2;
cout << "The total is " << total << endl;
}

And here is the errors it is giving me.

error C2143: syntax error : missing ';' before '+'
1>.\Source4.cpp(9) : error C2059: syntax error : ';'
1>.\Source4.cpp(10) : error C2143: syntax error : missing ';' before '-'
1>.\Source4.cpp(10) : error C2059: syntax error : ';'
1>.\Source4.cpp(11) : error C2059: syntax error : ';'
1>.\Source4.cpp(12) : error C2143: syntax error : missing ';' before '/'
1>.\Source4.cpp(12) : error C2143: syntax error : missing ';' before '/'

The errors are all about the math operators, even if I change them from a string it still gives me errors...any ideas???

4. Registered User
Join Date
Jan 2005
Location
UK
Posts
604
No mate, there's something weird going on with your code: ;-)
Code:
```string +; // syntax error: what you probably want is sth like
const string PLUS = "+";
string -; // see above
string *; // see above
string /; // see above```
BTW: I don't think you need these lines at all.
D

5. Registered User
Join Date
Mar 2007
Posts
4
I didn't have these lines to begin with but someone else mentioned that I need them since the multiplication and division weren't working. I don't understand why the + and - would work and not the * and /. Doesn't make sense to me but as you can probably tell I am pretty new to this.

6. Registered User
Join Date
Mar 2007
Posts
4
OK I got it working now. Thanks for the help! :) :WAVE:

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 FAQ Latest Articles Java .NET XML Database Enterprise