Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Computer Engineering Expert

For this assignment, you must create a simple calculator program. In a normal calculator, you give the first number (the X value), then the operation (+,-,*,/), then the second number (the Yvalue) and finally you hit the equal sign. In this calculator, there is no equal sign. You enter the first number (the X value), then the second number (the Y value), and lastly the operation (+,-,*,/). The result is given as soon as you enter the operation.(That style of calculation is called "Reverse Polish" and is the style used by the original HP calculator. For engineers, it is more efficient and does not require parentheses in long expressions.)

To implement this program, declare two integer variables for the X and Y values. Starting with this assignment, coding styles will be enforced, including Hungarian prefixes. So you should name your variables iValue1 and iValue2 or iXoperand and iYoperand or similar names that are descriptive and include a type prefix. For the operation, you are to read in a single character. The character type is a char. Name this variable chOperator.You will need to use if or if else statements to perform only the operation that is requested. A literal character is not a string, an d uses single quotes as in the following example:if (chOperand =='+') In the code, make sure that you include a prompt for each value. For reasons that you will see later, make sure that you prompt for the first and second values separately. Make sure that your program works before moving on to the next part. It should be able to perform the following dialog: Enter the first value as an integer: 12 Enter the second value as an integer: 5

Enter the operation you want to perform:- The result is 7 For the

next part, make the following changes. In the two prompts, change the words "first"

to "initial" and the word "second" to "next". When you compute the result of the operation,

assign the result to the first variable (e.g. iXoperand = iXoperand + iYoperand).

Make sure that your program still works. Then add the following two lines. Between the cin

line that reads the initial variable and the cout line that prompts for the next variable, insert the line:

do {Then,

right before the ending " return 0; " line, insert the line:} while (iXoperand != 0);

For the name iXoperand, use whatever name you used for your first variable which is the one

that also got the result of the operation.Now try running your program. The program should continue to get new values and operations to apply to the previous result. It will stop when the result is 0. Enter the initial value as an integer: 5 Enter the next value as an integer: 7 Enter the operation you want to perform: + The result is 12

Enter the next value as an integer: 3 Enter the operation you want to perform: -

The result is 9 Enter the next value as an integer: 10 Enter the operation you want to perform: + The result is 19 Enter the next value as an integer: 19 Enter the operation you want to perform: - The result is 0 Press any key to continue . . . Making your program robust For the divide operation, you should add another condition to make sure that the second or Y operand is not zero. Do not perform divide if the divisor is 0. The cin operation is fragile. If you enter something other than an integer, you can get odd results. If your program starts to loop forever, type Ctrl -

C to stop it. To prevent this behavior,you can add the following code. After a cin to read an integer, add this code: if (cin.fail()) { cout <<"No integer found . Using"<< iYoperand << endl; cin.clear(); cin.ignore(10000, '\n');}

Remember from lecture that using { and } creates a block of code within the condition instead of just one line.Also, not the use of single quotes instead of double quotes. (Double quotes signify a literal string while single quotes signify a literal character.) If reading the integer failed, you can either proceed with whatever the previous value of iYoperand might have been, or you can avoid doing anything else in the loop. There are two ways to prevent the rest of the code from being executed. One way is to make the rest of the code

be the else case from this if: else { ... }all the way to just before the while at the end of the loop. The other way

to have the same effect is the use a special loop "continue" statement which

says: skip the rest of the loop but keep looping. if (cin.fail()){ cout << "No integer

found. Try again."<< endl;cin.clear();cin.ignore(10000,'\n');continue;}

The cin to read a character will read any character so the problem here is different. If you

type more than one character, the input will be messed up. In this case you need an extra line to clear any bad input. In this case, just add the following two lines just before the while statement (inside the loop, but at the end of the loop code).cin.clear();cin.ignore(10000,'\n'); 

Computer Engineering, Engineering

  • Category:- Computer Engineering
  • Reference No.:- M9450177

Have any Question?


Related Questions in Computer Engineering

In a large university 6 of students live in dormitories a

In a large university, 6% of students live in dormitories. A random sample of 14 students is selected. What is the probability that the sample contains more than five students who do not live in the dormitories?

How does having an understanding of spreadsheet software

How does having an understanding of spreadsheet software help you succeed in your personal or professional life?

System analysis project can you answer the 4 questions at

System Analysis project: can you answer the 4 questions at the task section. Personal Trainer, Inc. owns and operates fitness centers in a dozen Midwestern cities. The centers have done well, and the company is planning ...

What are the uses and value of statistical significance

What are the uses and value of statistical significance testing and the p-value as demonstrated in the various analytical techniques (t-tests, ANOVA, chi-square, etc.). What's the fundamental reason we apply these techni ...

The average cell phone bill is 78 with a standard deviation

The average cell phone bill is 78 with a standard deviation of 10. What is the minimum percentage of cell phone bills that will be between 54 and 102?

Whats the difference between a bigfile tablespace and a

What's the difference between a Bigfile tablespace and a Smallfile tablespace? Explain which you would use for your database and why.

Suppose you make 30 annual investments in a fund that pays

Suppose you make 30 annual investments in a fund that pays 6% compounded annually. If your first deposit is $7,500 and each successive deposit is 6% greater than the preceding deposit, how much will be in the fund immedi ...

Can someone help me with this java problem and explain the

Can someone help me with this Java problem and explain the parts please! Write a method "totalDays" that takes three integer arguments (number of weeks, number of days, number of hours) and returns the total real number ...

Maureen has preferences for two goods to be consistent with

Maureen has preferences for two goods to be consistent with the utility function. The price of good 1 is $4 each, and the price of good 2 is $12 each. For what incomes will good 1 be normal?

Question using a web browserand a search engine the terms

Question: Using a web browserand a search engine the terms "citibank backup tapes lost." You will find many results. Select one article and identify what that article considers a ,short coming in citibank's planning. Wha ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As