This has been a nightmare. I'm coding this in C++. I'm supposed to enter a letter, and it's supposed to do the following depending on the letter. a is for addition, s is for subtraction, m is for multiplication, and d is for division. Any other letters is supposed to display an error message. Anyway, when I enter a, not only get the addition of the two numbers, I also get the quotient. When I enter s, I get the subtraction AND the quotient. When I enter m, I get the multiplication AND the difference. And when I enter d, I get the quotient AND the difference. What is going on? Also, when I enter a number other than a, s, m, or d, not only do I get, "Invalid letter" (Which I want), I also get this. Here is what I get. Please help me. I've included the code. Thank you!
/*
Enter letter:G
Invalid letter
Difference:4011944
Quotient:501494
Press any key to continue . . .
*/
_______________________________
//declare variables
int sum;
int difference;
int product;
int quotient;
2007-03-07
14:21:22
·
4 answers
·
asked by
Christi
4
in
Computers & Internet
➔ Programming & Design
int number;
char letter = ' ';
int number1;
int number2;
//enter input items
cout << "Enter letter:";
cin >> letter;
letter = tolower(letter);
if (letter !='a' && letter !='s' && letter !='m' && letter !='d')
cout << "Invalid letter" << endl;
else
{
number1 = getNumber();
number2 = getNumber();
}
//
if (letter == 'a')
{
sum = number1 + number2;
cout << "Sum:" << sum << endl;
}
else
if(letter == 's' && number1 >= number2)
{
difference = number1 - number2;
cout << "Difference:" << difference << endl;
}
else
{
difference = number2 - number1;
cout << "Difference:" << difference << endl;
}
if (letter == 'm')
{
product = number1 * number2;
cout << "Product:" << product << endl;
}
else
if(letter == 'd' && number1 > number2)
2007-03-07
14:23:00 ·
update #1
{
quotient = number1/number2;
cout << "Quotient:" << quotient << endl;
}
else
{
quotient = number2/number1;
cout << "Quotient:" << quotient << endl;
}
//end ifs
system ("pause");
return 0;
} //end of main function
int getNumber()
{
int number;
cout << "Enter a number:";
cin >> number;
return number;
}
2007-03-07
14:23:42 ·
update #2