Functions

Hi so i need a little help with my program, because i need to put two numbers and choose my operand and after that it will ask a question to return back to the main menu press y for yes and n for no. when i press y it returns to the main menu and ill input another two numbers and select my operand but when i press y it finishes the program.

Here is my code:

#include <iostream>
#include <stdlib.h>
using namespace std;
void printMessage()
{
system ("cls");
system ("color f0");
int a,b,d;
char c;
cout << "\tPlease enter a number: ", cin >> a;
cout << "\t[+, -, /, %]: ", cin >> c;
cout << "\tPlease enter second number: ", cin >> b;
if (c=='+')
{
int sum;
sum = a + b;
cout << "\tExpression: " << a << " + " << b << " = " << sum;
}
if (c=='-')
{
int diff;
diff = a - b;
cout << "\tExpression: " << a << " - " << b << " = " << diff;
}
cout<<endl;
cout <<"\tdo you want to return to the main menu? [y/n]"; cin>> d;
if (d=='y')
{
printMessage();
}
else if (d=='n')
{
cout << endl;
}




}


int addition (int num1, int num2)
{
system ("color A0");
int sum;
sum = num1 + num2;

return sum;
}
int subtraction (int num1, int num2)
{
system ("color B0");
int sub;
sub = num1 - num2;

return sub;
}
int mod (int num1, int num2)
{
system ("color e0");
int mod;
mod = num1 % num2;

return mod;
}
int division (int num1, int num2)
{
system ("color b0");
int div;
div = num1 / num2;

return div;
}
int multiplication (int num1, int num2)
{
system ("color c0");
int prod;
prod = num1 * num2;

return prod;
}
int main ()
{
system ("color f0");
int a,b;
char c,d;
cout << "\tPlease enter a number: ", cin >> a;
cout << "\t[+, -, /, %]: ", cin >> c;
cout << "\tPlease enter second number: ", cin >> b;
system ("cls");
switch (c)
{
case '+':
cout << "\tExpression: " << a << " " << c << " " << b << " = " << addition (a,b); break;
case '-':
cout << "\tExpression: " << a << " " << c << " " << b << " = " << subtraction (a,b); break;
case '%':
cout << "\tExpression: " << a << " " << c << " " << b << " = " << mod (a,b); break;
case '/':
cout << "\tExpression: " << a << " " << c << " " << b << " = " << division (a,b); break;
case '*':
cout << "\tExpression: " << a << " " << c << " " << b << " = " << multiplication (a,b); break;

}

cout << endl;
cout << "\tFirst number:" << a << "\tSecond number:" << b;
cout << endl;

cout << "Do you want to go back to the Main Menu? [y/n]: ", cin >> d;

if (d=='y')
{
printMessage();
}
else if (d=='n')
{
cout << endl;
}





return 0;
}
Last edited on
char d = 'y';
do
{
...
cin >> d;
} while(d == 'y');

the variable d must be type char.the declaration of variable d in your programm is integer.you must think that integers have priority against characters..so when you declared variable d int you programm transformed the character 'y' to integer.
Last edited on
Topic archived. No new replies allowed.