1. ## Simple Calculator

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;

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);
return 0;
}
else if (c == 2) {sub(a - b);
return 0;
}
else if (c == 3) {mul(a * b);
return 0;
}
else if (c == 4) {div(a / b);
return 0;
}
else if (c >= 5) {
cout << "Option doesn't excist!" << endl;
return 1;
}
else if (c <= 0) {
cout << "Option doesn't excist!" << endl;
return 1;
}
}```

2. 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;

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);`

3. Originally Posted by manasij7479
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);`
Ok thanks I'll do that then

4. Last two if statements could be combined into one.
Break out as much code from the if statements as you can. For example, store the result in a variable somewhere and print the result AFTER the if statement.
Improving your indentation and code style would not be a bad thing either.

5. BTW, I didn't try to compile but I think the
Code:
`using  namespace std;`
is unnecessary in your current implementation.

6. Originally Posted by walla
BTW, I didn't try to compile but I think the
Code:
`using  namespace std;`
is unnecessary in your current implementation.
No it isn't .... unless you want to do the same thing by different 'using' statements...or prefixing std at dozens of places.

7. Originally Posted by manasij7479
No it isn't .... unless you want to do the same thing by different 'using' statements...or prefixing std at dozens of places.
He didn't use anything from std

8. Incorrect. cout and cin lies in the std namespace. So does endl.

9. Interesting..
thanks