I am attempting to make a simple calculator. When I first build the addition part the program compiled and worked fine. but when I put the subtraction part in I got the following errors:
(expected primary-expression before "else")
(expected ";" before "else")
both errors are on line 43.
What I am wanting to do is get this part working, then I should be able to add the multiplication and division parts without incident. And at some point add a memory function and all the other bells and whistles.
// Calculator - Lets see if I can allow you to chose the operation and then
// choose to do another operation after the first one.
#include <cstdlib>
#include <cstdio>
#include <iostream>
usingnamespace std;
int main(int nNumberofArgs, char* pszArgs[])
{
char choice;
do
{
// This block of code makes the first bit of text easier to read on screen.
char operation;
char a, s, m, d;
cout << "Do you want to: \n"
<< "A - Add\n"
<< "S - Subtract\n"
<< "M - Multiply\n"
<< "D - Divide\n"
<< endl;
cin >> operation;
if (operation == a);
{double x, y, z;
cout << "Enter the first number you want to Add: "
<< endl;
cin >> x;
cout << "Now enter the seccond number: "
<< endl;
cin >> y;
z=(x+y);
cout << x
<< " + "
<< y
<< " = "
<< z
<< endl;
}
elseif (operation == s)
{double d, e, f;
cout << "Enter the fist number you want to subtract: "
<< endl;
cin >> d;
cout << "Enter the seccond number you want to subtract: "
<< endl;
cin >> e;
f =(d-e);
cin >> e;
cout << d
<< " - "
<< e
<< " = "
<< f
<< endl;
}
cout << "Do you want to continue? (y/n)"
<<endl;
cin >> choice;
}
while(choice != 'n');
}
Thanks I was thinking the == would take that into account. I have the addition and subtraction working now. I had to remove line 53, don't know why I put that in there.
Thanks again.
Once I get it dont this way I think I will figure out how to do the same program using (switch).
// Calculator - Lets see if I can allow you to chose the operation and then
// choose to do another operation after the first one.
#include <cstdlib>
#include <cstdio>
#include <iostream>
usingnamespace std;
int main(int nNumberofArgs, char* pszArgs[])
{
char choice;
do
{
// This block of code makes the first bit of text easier to read on screen.
char operation;
char a, s, m, d;
cout << "Do you want to: \n"
<< "A - Add\n"
<< "S - Subtract\n"
<< "M - Multiply\n"
<< "D - Divide\n"
<< endl;
cin >> operation;
if (operation == 'a')
{double x, y, z;
cout << "Enter the first number you want to Add: "
<< endl;
cin >> x;
cout << "Now enter the seccond number: "
<< endl;
cin >> y;
z=(x+y);
cout << x
<< " + "
<< y
<< " = "
<< z
<< endl;
}
elseif (operation == 's')
{
double d, e, f;
cout << "Enter the fist number you want to subtract: "
<< endl;
cin >> d;
cout << "Enter the seccond number you want to subtract: "
<< endl;
cin >> e;
f =(d-e);
cout << d
<< " - "
<< e
<< " = "
<< f
<< endl;
}
elseif (operation == 'm')
{
double f, g, h;
cout << "What is the first number you want to multiply? "
<< endl;
cin >> f;
cout << "what is the seccond number you want to multiply? "
<< endl;
cin >> g;
h = (f*g);
cout << f
<< " * "
<< g
<< " = "
<< h
<< endl;
}
elseif (operation == 'd')
{
double i, j, k;
cout << "What is your Dividend? "
<< endl;
cin >> i;
cout << "What is your Divisor? "
<< endl;
cin >> j;
k = (i/j);
cout << i
<< " / "
<< j
<< " = "
<< k
<<endl;
}
cout << "Do you want to continue? (y/n)"
<<endl;
cin >> choice;
}
while(choice != 'n');
}