Originally Posted by
Loke302
So this is something I made out of everything I've learned sofar.
I post it here so I can get pointers on what to improve on.
Code:
#include <iostream>
using namespace std;
void add(float x){
cout << "Result: " << x << endl;
};
void sub(float x){
cout << "Result: " << x << endl;
};
void mul(float x){
cout << "Result: " << x << endl;
};
void div(float x){
cout << "Result: " << x << endl;
};
int main()
{
float a;
float b;
int c;
cout << "Loke302 Simple Calculator!\n" << endl;
cout << "Enter a number: ";
cin >> a;
cout << "Enter a number: ";
cin >> b;
cout << "[1] To Add\n[2] To Subtract\n[3] To Multiply\n[4] To Divide" << endl;
cin >> c;
if (c == 1) {add(a + b);}
else if (c == 2) {sub(a - b);}
else if (c == 3) {mul(a * b);}
else if (c == 4) {div(a / b);}
else if (c >= 5) {
cout << "Option doesn't excist!";
}
else if (c <= 0) {
cout << "Option doesn't excist!";
}
return 0;
}
You misunderstood what functions should be used for and how.
I'm showing you an example of how your add should ideally be.
Code:
double add(double x, double y)
{
return (x+y);
}
You could show the result by:
Code:
cout<<"Result: "<<add(a,b);