473,327 Members | 1,896 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Gonzo, help me please

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
11 1598
sicarie
4,677 Expert Mod 4TB
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
05l8kr
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
4,677 Expert Mod 4TB
Well, then you got it.
Nov 8 '06 #4
05l8kr
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
4,677 Expert Mod 4TB
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
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
4,677 Expert Mod 4TB
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
05l8kr
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
142 100+
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
05l8kr
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
142 100+
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

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

Similar topics

6
by: Google Mike | last post by:
This is big news! http://story.news.yahoo.com/news?tmpl=story&ncid=1817&e=8&u=/zd/20050225/tc_zd/146644&sid=96120751...
1
by: Numberwhun | last post by:
Hello everyone! I am trying to learn java and have run into kind of a snag. Here is the code that I have so far: ------ <begin_code> ---------- import javax.swing.*; import...
1
by: HolaGoogle | last post by:
Hi all, Please help me with the following..it's realy urgent and i tried everything i could and i can't get it work properly!! Thanks in advance. Here's what i'm trying to accomplish: in my...
0
by: s_erez | last post by:
Hi, This is a realy tricky one. I have an ASP.NET application where some pages are reading data from a DB and presenting reports. In order for the user to wait while the page is reading data from...
4
by: dave | last post by:
Hi guys I display one page in popup window...that fetches some data from sql and perfom some calculation (tht approx 10 secs) and display result.... I am trying to display "Please wait ..."message...
2
by: rked | last post by:
I get nameSPAN1 is undefined when I place cursor in comments box.. <%@ LANGUAGE="VBScript" %> <% DIM ipAddress ipAddress=Request.Servervariables("REMOTE_HOST") %> <html> <head> <meta...
7
by: x muzuo | last post by:
Hi guys, I have got a prob of javascript form validation which just doesnt work with my ASP code. Can any one help me out please. Here is the code: {////<<head> <title>IIBO Submit Page</title>...
4
by: pshindle | last post by:
DB2 Team - I just downloaded and unzipped the new Fixpack 9 for DB2 ESE V8 for Windows (FP9_WR21350_ESE.exe). I then burned the unzipped Fixpack files to a CD. I proceded to install this...
1
PEB
by: PEB | last post by:
POSTING GUIDELINES Please follow these guidelines when posting questions Post your question in a relevant forum Do NOT PM questions to individual experts - This is not fair on them and...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.