Hi again, I'm back, with another question about my calculator program (which I got to work, yay!)
Why does the following program not work (when the one after it does):
#include <iostream>
using namespace std;
float x;
float y;
char z;
int add()
{
cout << x << " " << z << " " << y << " = " << x + y << endl;
}
int subtract()
{
cout << x << " " << z << " " << y << " = " << x - y << endl;
}
int divide()
{
cout << x << " " << z << " " << y << " = " << x / y << endl;
}
int multiply()
{
cout << x << " " << z << " " << y << " = " << x * y << endl;
}
int main()
{
cout << "Please enter an expression: " << endl;
cin >> x;
cin >> z;
cin >> y;
if(z == '+')
{
int add();
}
if(z == '/')
{
int divide();
}
if(z == '-')
{
int subtract();
}
if(z == '*')
{
int multiply();
}
cin.clear();
cin.ignore(255, '\n');
cin.get();
//^^ for Dev C++ compiler users, so the program stays open
return 0;
}
when this one does:
#include <iostream>
using namespace std;
float x;
float y;
char z;
int main()
{
cout << "Please enter an expression: " << endl;
cin >> x;
cin >> z;
cin >> y;
if(z == '+')
{
cout << x << " " << z << " " << y << " = " << x + y << endl;
}
if(z == '/')
{
cout << x << " " << z << " " << y << " = " << x / y << endl;
}
if(z == '-')
{
cout << x << " " << z << " " << y << " = " << x - y << endl;
}
if(z == '*')
{
cout << x << " " << z << " " << y << " = " << x * y << endl;
}
cin.clear();
cin.ignore(255, '\n');
cin.get();
//^^ for Dev C++ compiler users, so the program stays open
return 0;
}
The only difference between the two is that the first used function calls to pre-stated functions, and the second has the entire function stated in the if statement, without use of function calls. The first compiles okay, but when you actually try to use the program... well nothing happens. Anyone know why?