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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
#include<iostream>
#include<string>
void addn (int a, int b);
void subn (int a, int b);
void muln (int a, int b);
void divn (int a, int b);
using namespace std;
int main()
{
int n,a,b; // These didn't need to be global
cout<<"Welcome to iCalc 1.0 !"<<endl;
loop:
do {
cout<<"Please enter a number to choose operation (Enter 0 to exit):"<<endl<<endl;
cout<<"1. addnition"<<endl;
cout<<"2. Subtraction"<<endl;
cout<<"3. Multiplication"<<endl;
cout<<"4. Division"<<endl;
cout<<"5. About"<<endl<<endl;
cout<<"NUMBER:\t";
cin>>n;
} while (n!=0); // Indenting, this should be aligned with the do
if (n>5){
cout<<"Please enter a number from 1-5 OR enter 0 to exit";
goto loop;
} // Indenting, it should be aligned with the if
else
{ // added to make the 'else' scope clearer
switch (n) {
case 1:
cout<<"Please enter 1st number to addn:\t";
cin>>a;
cout<<endl<<"Please enter 2nd number to addn: \t";
cin>>b;
addn (a, b);
break;
case 2:
cout<<"Please enter 1st number to subtract:\t";
cin>>a;
cout<<endl<<"Please enter 2nd number to subtract: \t";
cin>>b;
subn (a, b);
break;
case 3:
cout<<"Please enter 1st number to multiply:\t";
cin>>a;
cout<<endl<<"Please enter 2nd number to multiply: \t";
cin>>b;
muln (a, b);
break;
case 4:
cout<<"Please enter 1st number to divide:\t";
cin>>a;
cout<<endl<<"Please enter 2nd number to divide: \t";
cin>>b;
divn (a, b);
break;
case 5:
cout<<"VERSION:\t1.0\n\n";
break;
default:
cout<<"Please enter a number from 1-5 OR enter 0 to exit";
goto loop;
} // I changed the indenting of this, it closes the switch
} // I added this to make the 'else' scope clearer
system ("pause");
return 0;
}
// Function definitions are no longer in main
void addn (int a, int b)
{
int ans = a+b; // you can make ans a local variable.
cout<<ans;
// goto loop; You cannot goto something in another function
}
void subn (int a, int b)
{
int ans = a-b; // The variable on the left is what will get the value, a-b = ans doesn't work.
cout<<ans;
// goto loop;
}
void muln (int a, int b)
{
int ans = a*b;
cout<<ans;
// goto loop;
}
void divn (int a, int b)
{
int ans = a/b;
cout<<ans;
// goto loop;
}
|