C++ App Help!

Status
Not open for further replies.

eXtensible

Baseband Member
Messages
37
Location
Earth
Hi eXtensible here,
I've been working on a C++ app that replaces the standard Calculator app with a simple but more advanced Scientific Calculator, i've gotten as far as basic math functions but that's where the problem lies, I have funcitons for +ing, -ing, *ing, and /ing, but the problem is no matter what function i select when i run it, It does nothing but addition.

Here's the Source Code:
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
int a;
cout << "What would you like to do ? " << endl << "HINT: 1 = add, 2 = subtract, 3 = multiply, 4 = divide " << endl;

cin >> a;

if (a = 1);
{
double a;
cout << "Enter the first number: ";
cin >> a;
double b;
cout << "Enter the second number: ";
cin >> b;
double c;
c = a + b;
cout << "Calculating: " << c << endl;
system("PAUSE");
return 0;
}

if (a = 2);
{
double a;
cout << "Enter the first number: ";
cin >> a;
double b;
cout << "Enter the second number: ";
cin >> b;
double c;
c = a - b;
cout << "Calculating: " << c << endl;
system("PAUSE");
return 0;
}

if (a = 3);
{
double a;
cout << "Enter the first number: ";
cin >> a;
double b;
cout << "Enter the second number: ";
cin >> b;
double c;
c = a * b;
cout << "Calculating: " << c << endl;
system("PAUSE");
return 0;
}

if (a = 4)
{
double a;
cout << "Enter the first number: ";
cin >> a;
double b;
cout << "Enter the second number: ";
cin >> b;
double c;
c = a / b;
cout << "Calculating: " << c << endl;
system("PAUSE");
return 0;
}


system("PAUSE");
return EXIT_SUCCESS;
}
Please Help Me!
 
A common beginner C programmer error. " if (a = 1);" will always be TRUE because it is an assigment (a=1) instead of a comparison (a==1). You'd probably be better off using a SWITCH statement instead of all those IF statements. See here.
 
Strollin,
I replaced the IF statements with SWITCH statements, I also used (a==1) instead of (a=1), but i still got the same error, but Thanks anyway. Any other advice from anyone, is greatly appreciated.
Thanks,
eXtensible
 
If you replaced the IF statements with A switch statement, then you would not need the (a==1).

switch ( a )
{
case 1:
<add>
break;
case 2:
<subtract>
break;
case 3:
<multiply>
break;
default:
<error>
;
}
 
A common beginner C programmer error. " if (a = 1);" will always be TRUE because it is an assigment (a=1) instead of a comparison (a==1). You'd probably be better off using a SWITCH statement instead of all those IF statements. See here.

I second this. Also, you have semicolons terminating your if conditions meaning that if the condition is true do nothing then always do the code in the braces. Mysteriously, you have corrected this error for case 4.
 
Status
Not open for further replies.
Back
Top Bottom