Remember from lecture that using

Assignment Help Basic Computer Science
Reference no: EM13160051

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'); 

Reference no: EM13160051

Questions Cloud

Explain how to record the patent : At May 5, 2011, MacDougal's common stock was quoted at $34 per share, and the patent had a carrying value of $55,000 on Masset's books. MacDougal should record the patent at.
Calculate the three given overhead variances : Manufacturing overhead for the period is budgeted at $135,000, of which 20 percent is fixed. The 17,200 hours worked during the period resulted in production of 8,500 units. Manufacturing overhead cost incurred was $136,500.Calculate the following..
Propose a biochemical pathway for flower pigmentation : Two plants with white flowers, each from true-breeding strains,were crossed. All the F1 plants had red flowers. Whenthese F1 plants were intercrossed, they produced anF2 consisting of 92 plants with red flowers and 69 withwhite flowers.
Differences and similarities between the two operating syste : What are some distinct differences and similarities between the two operating systems? Why do you think it is important to know about both of these operating systems
Remember from lecture that using : 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 ..
How are entries made under each type of accounting : Write a 700- to 1,050-word paper in APA format comparing and contrasting the different types of accounting, namely accrual, cash flow, and fund accounting.
Evaluate arthur schopenhauer : Explain and evaluate Arthur Schopenhauer's view that people are rarely rational in their actions, is he correct?
Determine participation in the year-s net income : Determine Hassell's and Lawson's participation in the year's net income of $288,000 under each of the independent assumptions above.
Phenomena and the noumena : Explain the distinction between the phenomena and the noumena.

Reviews

Write a Review

Basic Computer Science Questions & Answers

  How much laser be tipped before light no longer continues

If a laser is to be pointed into the end of the fibre, but is "tipped" to one side by θ degrees, how much can the laser be "tipped" before the light no longer continues down the fibre?

  Internet changes consumer and supplier relationships

Considering a focus in area of E-Commerce (EC), how would you think Internet changes consumer and supplier relationships?

  Aspects of negotiation will point out it is proceeding well

What aspects of negotiation will point out it is proceeding well or poorly? What will tell you that it is time to arrange further meeting? What signs will you utilize to decide when change in negotiators is necessary?

  Explain how to reasoned access to company resources

Given home computers and access to company resources, employees will work extra hours at home and thereby increase company profits." Explain how well reasoned etc.

  Significant questions software installed by cable personnel

Interesting post on the computer discussion site (Slashdot) raises some significant questions about software installed by cable personnel.

  Explain what is file type-from where file types come

What is "file type?" Where do file types come from? Write down some of the information that file types give (tip: think about copying and pasting, also importing/exporting between files)?

  The progress report you will describe

The progress report you will describe your progress and analysis of the unfinished solution. At this time you should be able to take stock of your skills and abilities and match them against the project requirements.

  Explain ethics of developing of system

To help counter terrorism, many countries are planning and development of computer systems that track large number of citizens and their actions. Clearly this has privacy, implications. Discuss the ethics of developing this type of system.

  Turing machine that decreases positive binary number by one

Write a Turing machine that decreases a positive binary number by one? By writing turing machine an instruction set in the form of (w,x,y,z,a) where w is current state,

  Determine degree of statical indeterminacy

Is it statically determinate? If it is not statically determinate, determine its degree of statical indeterminacy? b) Determine the member forces roughly by assuming that diagonal members can't carry compressive loads.

  Weighted probabilities for x86 processors

Create a procedure that receives a value N between 0 to 100. When the procedure is called, there should be a probability of N/100 that it clears the Zero flag. Write a program that asks the user to enter a probability value 0 to 100.

  Java program to input values into array

Write a Java program to input the following values into an array named prices: 10.95, 16.32, 12.15, 8.22, 15.98, 26.22, 13.54, 6.45, 17.59.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd