You are defining "operacio" as an integer VARIABLE here
Then you are using "operacio" as a function, here
so, from what i see with your program, you need to take out that whole section, which is this one
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
int operacio{
if (operacio==1){
int suma;
}
if (operacio==2){
int suma;
}
if (operacio==3){
int suma;
}
if (operacio==4){
int suma;
}
}
|
and modify your int main() { ... } function to look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
int main()
{
cout << "Selecciona el calcul a fer: \n 1. Suma \n 2. Resta \n 3. Multiplicacio \n 4. Divisio \n ";
cin >> operacio;
switch (operacio) {
case 1:
suma();
break;
case 2:
resta();
break;
case 3:
multiplicacio();
break;
case 4:
divisio();
break;
default:
cout << "You selected an inappropriate option"<<endl;
}
return 0;
}
|
Also please note that this calculator will only be able to perform operations on whole numbers. Anything with a decimal will be trunicated.
Also, you have a LOT of repetition in your functions, which the idea of functions is to save yourself from repetition. Since you have integers a and b declared in the global scope, you could you could simply do
1 2 3 4
|
cout <<"Inserta un numero ";
cin >> a;
cout <<"Inserta un segon numero ";
cin >> b;
|
one time in the main function, since your asking for the same data in each function. If you use the code that I showed you above with the switch statement, you'd simply place the code directly above, above your switch statement. However, considering the overall simplicity of these functions, it would be more optimal and organizational to have a one-does-all function. Lets take in to consideration the code below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
using namespace std;
int operations(int opr, int integer1, int integer2) {
switch (opr){
case 0: // break statements not required, because we are returning from the function
return integer1+integer2;
case 1:
return integer1-integer2;
case 2:
return integer1/integer2;
case 3:
return integer1*integer2;
default:
return integer1+integer2;
}
}
int main() {
int choice = 0;
int num1 = 0;
int num2 = 0;
cout << "Please choose a function:"<<endl<<" 1. Add"<<endl<<" 2. Subtract"<<endl<<" 3. Divide"<<endl<<" 4. Multiply"<<endl<<endl<<"Your choice: ";
cin >> choice;
if((choice>0) && (choice<5)) {
cout << endl << "Enter your first number: ";
cin >> num1;
cout << endl << "Enter your second number: ";
cin >> num2;
cout << endl << "The Result Is: " << operations(choice-1, num1, num2) << endl;
}else{
cout << endl <<"You chose an invalid options. Goodbye.";
}
return 0;
}
|
In this code we use one function to perform all operations we require. A function prototype is not required either because we are defining it before it's called upon. I believe the code to be pretty straigthtforward, however, if you have any questions please ask.