By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,406 Members | 954 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,406 IT Pros & Developers. It's quick & easy.

Gonzo, help me please

P: 35
I'm suppose to write a program that will ask the user to enter two integers. Then the program will ask the user for one of four letters a to add the numbers, s to subtract the numbers, m to multiply the numbers, or d to divide the two numbers.
If the user enters any character other than the 4 listed, keep asking them over and over until they enter a correct letter. Display on the monitor the two numbers entered the math operation used and the answer of the operation:

What's your aim name? PM it
Nov 6 '06 #1
Share this Question
Share on Google+
11 Replies


sicarie
Expert Mod 2.5K+
P: 4,677
Is there supposed to be any error-checking for the numbers? Or just for the letters?

All you need to do is cin two numbers, then enter a while loop. The while loop will have a condition that a char i == 'a' or i == 's' or i == 'm' or i == 'd'. While you are in that loop, cin i.

Then, you can either use if statements or a switch statement to do the correct operation.
Nov 6 '06 #2

P: 35
Is there supposed to be any error-checking for the numbers? Or just for the letters?

All you need to do is cin two numbers, then enter a while loop. The while loop will have a condition that a char i == 'a' or i == 's' or i == 'm' or i == 'd'. While you are in that loop, cin i.

Then, you can either use if statements or a switch statement to do the correct operation.
Just the letters
Nov 8 '06 #3

sicarie
Expert Mod 2.5K+
P: 4,677
Well, then you got it.
Nov 8 '06 #4

P: 35
Expand|Select|Wrap|Line Numbers
  1. == 'a' or i == 's' or i == 'm' or i == 'd'
  2.  
That's all the code ???
Nov 8 '06 #5

sicarie
Expert Mod 2.5K+
P: 4,677
Expand|Select|Wrap|Line Numbers
  1. == 'a' or i == 's' or i == 'm' or i == 'd'
  2.  
That's all the code ???
Absolutely not - I'm not writing the program, you are.

That is the general pseudocode or algorithm to follow. If you break that down and code it, you'll get it. (Though not that little bit there, the whole paragraph was the pseudocode, you just zeroed in on what looked the most like code).

Feel free to post any compiler errors you run into or any sort of declarational statements, but it wouldn't be a learning experience if you didn't have to do a little thinking ;)
Nov 8 '06 #6

P: 51
hi :im khajeddin.
you should write this program with "switch" structure.and also make a loop that ask the user over and oner again to enter the correct character.here it is what you want:
(then please answer my question which is in the Discussion page of C/C++)


while(1)
{
switch(ch) {
case'a' :
result=number1+numder2;
break;
case's' :
result=number1-number2;
break;
case'm' :
result=number1*number2;
break;
case'd' :
result=number1/number2;
break;
default:
printf("please enter the correct character");
break;
} /*end of while*/
Nov 8 '06 #7

sicarie
Expert Mod 2.5K+
P: 4,677
hi :im khajeddin.
you should write this program with "switch" structure.and also make a loop that ask the user over and oner again to enter the correct character.here it is what you want:
(then please answer my question which is in the Discussion page of C/C++)


while(1)
{
switch(ch) {
case'a' :
result=number1+numder2;
break;
case's' :
result=number1-number2;
break;
case'm' :
result=number1*number2;
break;
case'd' :
result=number1/number2;
break;
default:
printf("please enter the correct character");
break;
} /*end of while*/
Man, i always forget about switch statements - good idea.
Nov 8 '06 #8

P: 35
Man, i always forget about switch statements - good idea.
What should loop look like for the user over and oner again to enter the correct character
Nov 9 '06 #9

sivadhas2006
100+
P: 142
Hi,

This may satisfy your need.

Expand|Select|Wrap|Line Numbers
  1.  
  2. int DoOperation(int a_nFirst, int a_nSecond, char &a_cOperation)
  3. {
  4.    int
  5.       nResult = 0;
  6.  
  7.    switch(a_cOperation) 
  8.    {
  9.       case'a' :
  10.       {
  11.          nResult = a_nFirst + a_nSecond;
  12.          break;
  13.       }
  14.  
  15.       case's' :
  16.       {
  17.          nResult = a_nFirst - a_nSecond;
  18.          break;
  19.       }
  20.  
  21.       case'm' :
  22.       {
  23.          nResult = a_nFirst * a_nSecond;
  24.          break;
  25.       }
  26.  
  27.       case'd' :
  28.       {
  29.          nResult = a_nFirst / a_nSecond;
  30.          break;
  31.       }
  32.  
  33.       case'q' :
  34.       {         
  35.          // To quit the recursive.
  36.          break;
  37.       }
  38.  
  39.       default:
  40.       {
  41.          cout << "Please enter the correct operator(a -> add, s -> sub, m -> mul, d -> div, q -> quit : ";
  42.          cin >> a_cOperation;
  43.  
  44.          // Recursive function to do the operation again.
  45.          nResult = DoOperation(a_nFirst, a_nSecond, a_cOperation);
  46.          break;
  47.       }      
  48.    }
  49.    return nResult;
  50. }
  51.  
  52. void main()
  53. {
  54.    int      
  55.       nFirst = 0,      
  56.       nSecond = 0,
  57.       nResult = 0;
  58.    char
  59.       cOperation = '\0';
  60.  
  61.    // Get the first number.
  62.    cout << "Enter the first number : ";
  63.    cin >> nFirst;
  64.  
  65.    // Get the second number.
  66.    cout << "Enter the second number : ";
  67.    cin >> nSecond;
  68.  
  69.    // Get the operator.
  70.    cout << "Enter the operator(a -> add, s -> sub, m -> mul, d -> div, q -> quit : ";
  71.    cin >> cOperation;
  72.  
  73.    // Do the operation with the given information.
  74.    nResult = DoOperation(nFirst, nSecond, cOperation);
  75.  
  76.    // Print the result.
  77.    cout << "The result is : " << nFirst << " " << cOperation << " " << nSecond << " = " << nResult;
  78.  
  79. }
  80.  
  81.  
Regards,
M.Sivadhas.
Nov 9 '06 #10

P: 35
Expand|Select|Wrap|Line Numbers
  1.  
  2. { while(1)
  3. {
  4. switch(ch) {
  5. case'a' :
  6. result=number1+numder2;
  7. break;
  8. case's' :
  9. result=number1-number2;
  10. break;
  11. case'm' :
  12. result=number1*number2;
  13. break;
  14. case'd' :
  15. result=number1/number2;
  16. break;
  17. default:
  18. printf("please enter the correct character");
  19. break;
  20. } /*end of while*/
  21. int DoOperation(int a_nFirst, int a_nSecond, char &a_cOperation)
  22. {
  23.    int
  24.       nResult = 0;
  25.  
  26.    switch(a_cOperation) 
  27.    {
  28.       case'a' :
  29.       {
  30.          nResult = a_nFirst + a_nSecond;
  31.          break;
  32.       }
  33.  
  34.       case's' :
  35.       {
  36.          nResult = a_nFirst - a_nSecond;
  37.          break;
  38.       }
  39.  
  40.       case'm' :
  41.       {
  42.          nResult = a_nFirst * a_nSecond;
  43.          break;
  44.       }
  45.  
  46.       case'd' :
  47.       {
  48.          nResult = a_nFirst / a_nSecond;
  49.          break;
  50.       }
  51.  
  52.       case'q' :
  53.       {         
  54.          // To quit the recursive.
  55.          break;
  56.       }
  57.  
  58.       default:
  59.       {
  60.          cout << "Please enter the correct operator(a -> add, s -> sub, m -> mul, d -> div, q -> quit : ";
  61.          cin >> a_cOperation;
  62.  
  63.          // Recursive function to do the operation again.
  64.          nResult = DoOperation(a_nFirst, a_nSecond, a_cOperation);
  65.          break;
  66.       }      
  67.    }
  68.    return nResult;
  69. }
  70.  
  71. void main()
  72. {
  73.    int      
  74.       nFirst = 0,      
  75.       nSecond = 0,
  76.       nResult = 0;
  77.    char
  78.       cOperation = '\0';
  79.  
  80.    // Get the first number.
  81.    cout << "Enter the first number : ";
  82.    cin >> nFirst;
  83.  
  84.    // Get the second number.
  85.    cout << "Enter the second number : ";
  86.    cin >> nSecond;
  87.  
  88.    // Get the operator.
  89.    cout << "Enter the operator(a -> add, s -> sub, m -> mul, d -> div, q -> quit : ";
  90.    cin >> cOperation;
  91.  
  92.    // Do the operation with the given information.
  93.    nResult = DoOperation(nFirst, nSecond, cOperation);
  94.  
  95.    // Print the result.
  96.    cout << "The result is : " << nFirst << " " << cOperation << " " << nSecond << " = " << nResult;
  97.  
  98. }
  99.  
I got the following errors
-fatal error C1004: unexpected end of file found
Error executing cl.exe.
-error C2447: missing function header (old-style formal list?)
Nov 10 '06 #11

sivadhas2006
100+
P: 142
Expand|Select|Wrap|Line Numbers
  1.  
  2. { while(1)
  3. {
  4. switch(ch) {
  5. case'a' :
  6. result=number1+numder2;
  7. break;
  8. case's' :
  9. result=number1-number2;
  10. break;
  11. case'm' :
  12. result=number1*number2;
  13. break;
  14. case'd' :
  15. result=number1/number2;
  16. break;
  17. default:
  18. printf("please enter the correct character");
  19. break;
  20. } /*end of while*/
  21. int DoOperation(int a_nFirst, int a_nSecond, char &a_cOperation)
  22. {
  23.    int
  24.       nResult = 0;
  25.  
  26.    switch(a_cOperation) 
  27.    {
  28.       case'a' :
  29.       {
  30.          nResult = a_nFirst + a_nSecond;
  31.          break;
  32.       }
  33.  
  34.       case's' :
  35.       {
  36.          nResult = a_nFirst - a_nSecond;
  37.          break;
  38.       }
  39.  
  40.       case'm' :
  41.       {
  42.          nResult = a_nFirst * a_nSecond;
  43.          break;
  44.       }
  45.  
  46.       case'd' :
  47.       {
  48.          nResult = a_nFirst / a_nSecond;
  49.          break;
  50.       }
  51.  
  52.       case'q' :
  53.       {         
  54.          // To quit the recursive.
  55.          break;
  56.       }
  57.  
  58.       default:
  59.       {
  60.          cout << "Please enter the correct operator(a -> add, s -> sub, m -> mul, d -> div, q -> quit : ";
  61.          cin >> a_cOperation;
  62.  
  63.          // Recursive function to do the operation again.
  64.          nResult = DoOperation(a_nFirst, a_nSecond, a_cOperation);
  65.          break;
  66.       }      
  67.    }
  68.    return nResult;
  69. }
  70.  
  71. void main()
  72. {
  73.    int      
  74.       nFirst = 0,      
  75.       nSecond = 0,
  76.       nResult = 0;
  77.    char
  78.       cOperation = '\0';
  79.  
  80.    // Get the first number.
  81.    cout << "Enter the first number : ";
  82.    cin >> nFirst;
  83.  
  84.    // Get the second number.
  85.    cout << "Enter the second number : ";
  86.    cin >> nSecond;
  87.  
  88.    // Get the operator.
  89.    cout << "Enter the operator(a -> add, s -> sub, m -> mul, d -> div, q -> quit : ";
  90.    cin >> cOperation;
  91.  
  92.    // Do the operation with the given information.
  93.    nResult = DoOperation(nFirst, nSecond, cOperation);
  94.  
  95.    // Print the result.
  96.    cout << "The result is : " << nFirst << " " << cOperation << " " << nSecond << " = " << nResult;
  97.  
  98. }
  99.  
I got the following errors
-fatal error C1004: unexpected end of file found
Error executing cl.exe.
-error C2447: missing function header (old-style formal list?)
Hi,

Remove your while loop.

Expand|Select|Wrap|Line Numbers
  1. { while(1)
  2. {
  3. switch(ch) {
  4. case'a' :
  5. result=number1+numder2;
  6. break;
  7. case's' :
  8. result=number1-number2;
  9. break;
  10. case'm' :
  11. result=number1*number2;
  12. break;
  13. case'd' :
  14. result=number1/number2;
  15. break;
  16. default:
  17. printf("please enter the correct character");
  18. break;
  19. } /*end of while*/
  20.  
  21.  
Regards,
M.Sivadhas.
Nov 13 '06 #12

Post your reply

Sign in to post your reply or Sign up for a free account.