English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

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

4 answers

Try this:

int main()
{
int number;
char letter = ' ';
int number1;
int number2;

int sum, difference, product;
float quotient;

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')
{
if(number1 >= number2)
{
difference = number1 - number2;
cout << "Difference:" << difference << endl;
}
else
{
difference = number2 - number1;
cout << "Difference:" << difference << endl;
}
}
else if (letter == 'm')
{
product = number1 * number2;
cout << "Product:" << product << endl;
}
else if(letter == 'd')
{
if(number1 > number2)
{
quotient = number1/number2;
cout << "Quotient:" << quotient << endl;
}
else
{
quotient = number2/number1;
cout << "Quotient:" << quotient << endl;
}
}
system("pause");
return 0;
}

int getNumber()
{
int number;
cout << "Enter a number:";
cin >> number;
return number;
}


Using switch statement is a better way to do this but I don't want to change your code at the last moment, so fixing your code only.

Hope this helps!

2007-03-07 14:44:20 · answer #1 · answered by Rainmaker 2 · 0 0

Hey,

You are making this too complicated. I haven't programmed in C++ for a while but I'll take a shot at it. You should be using a simple switch.

=========================
//declare vars
int num1;
int num2;
char operation = '';

while( isCharValid(operation) == false ){
cout << "Enter letter of operation." << endl;
cin >> operation;
if( isCharValid(operation) != true ){
cout << "Invalid operator." << endl;
}
}

cout << "Enter number one";
cin >> num1;
cout << "Enter number two";
cin >> num2;
//It would be a good idea to add validation to this but i dont //know the validation function to check for an int

switch( operation ){
case 'a':
cout << "Result is " << num1 + num2;
break;
case 's':
cout << "Result is " << num1 - num2;
break;
case 'm':
cout << "Result is " << num1 * num2;
break;
case 'd':
cout << "Result is " << num1 / num2;
break;
default:
cout << "Invalid operation";
}
return 0;

bool isCharValid( char c ){
if( c != 'a' && c!='s' && c!='m' && c!='d' ){
return false;
}
else{
return true;
}
}
/////////////////////////////////////////////////////////////

That program should work. Let me say though that you should be validating the numbers as valid integers. Also you could have stored the results of the operations in a float to save lines of code (where I printed out the results, you could combine that into one line). Anyways, hopefully this helps you get started.

2007-03-07 22:38:48 · answer #2 · answered by Nick O 2 · 0 0

TRY THIS

if (letter == 'a')
{
sum = number1 + number2;
cout << "Sum:" << sum << endl;
}
else
{
if(letter == 's')
{
if number1>=number2)
{
difference = number1 - number2;
cout << "Difference:" << difference << endl;
}
else
{
difference = number2 - number1;
cout << "Difference:" << difference << endl;
}
}
else
{
if (letter == 'm')
{
product = number1 * number2;
cout << "Product:" << product << endl;
}
else
{
if(letter == 'd')
{
if (number1>number2)
{
quotient = number1/number2;
cout << "Quotient:" << quotient << endl;
}
else
{
quotient = number2/number1;
cout << "Quotient:" << quotient << endl;}
}
}
}
}

2007-03-07 22:39:06 · answer #3 · answered by engineerpat 2 · 0 0

i don't program in C, but the symptom sounds like you need to add an instruction to end the program after a calculation is performed.

pseudocode:

input "press a, s, or q"
if input =a goto aroutine
if input =s goto sroutine
goto end

a routine{
add numbers
end}

s routine
...

also, does C++ have a case command, you used alot of else clauses

2007-03-07 22:29:01 · answer #4 · answered by BigJohnny 4 · 0 0

fedest.com, questions and answers