Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Topics Methods would you please provide the answer to this question using while loop. I have attached the previous answer that have used switch .

Topics

Methods

would you please provide the answer to this question using while loop. I have attached the previous answer that have used switch . but that is not what the Teacher outlined.

Description

Write a program that will simulate a simple calculator. The program will input from the user, the operation to be performed, the first decimal number and the second decimal on which to perform the operation. Then, it will perform the specified operation on the two decimal numbers and will display the result. The program will do this repeatedly and the user will be able to perform different operations on a different pair of numbers. The program will end when the user enters x when asked for an operation. The program will allow the following values for operation:

+ (add operation)

- (subtract operation)

* (multiply operation)

/ (divide operation)

Testing

For turning in the assignment, perform the following test run using the input values shown.

Test Run

(The user input is shown in bold)

Enter an operation

+

Enter the first number

2.2

Enter the second number

1.1

The result is 3.3

Enter an operation

-

Enter the first number

2.2

Enter the second number

1.1

The result is 1.1

Enter an operation

*

Enter the first number

2.2

Enter the second number

1.1

The result is 2.42

Enter an operation

/

Enter the first number

2.2

Enter the second number

1.1

The result is 2

Enter an operation

x

Requirements

The program will provide the normal entry function main and an additional function calculate as described below

Function main

The function main will do the following repeatedly:

It will prompt the user to enter the desired operation.

It will prompt the user to enter the first operand.

It will prompt the user to enter the second operand.

It will call the function calculate and pass it the operation and the two operands as parameters.

On return from calculate, the function main will receive the result in a variable using an assignment statement.

It will display the result.

Function calculate

The function calculate will receive three parameters and will return a double value.

It will receive the following three parameters.

A char parameter op for receiving the operation value. The possible values for this parameter are: +, -, *, /.

A double parameter n1 for receiving the first operand.

A double parameter n2 for receiving the second operand.

It will return a double value.

Sample Code

Always put prototypes of your functions at the beginning of the file before writing any function. Then you can write the functions anywhere before or after main. Otherwise, you will have to put functions before they are called or you will get a compiler error. A function prototype can be written by writing the function header

and ending it with a semi-colon as below.

//function prototype

double calculate (char op, double n1, double n2);

int main ( )

{

char oper;

double num1, num2, result;

//input oper from the user

while (oper == '+' || oper == '-' || oper == '*' || oper == '/' )

{

//input num1 and num2 from the user

//call the function calculate to perform the desired operation and receive the result

result = calculate (oper, num1, num2);

//display the result

//update for the loop

//input oper from user

}

return 0; }

double calculate (char op, double n1, double n2)

{

double res;

switch (op)

{

case '+':

res = n1 + n2;

break;

case '-':

res = n1 + n2;

break;

}

return res;

}

Answer That I have is used switch not while loop. I want the anser to be exactly what the teacher asks for. would You kindly use while loop for this question.

Assignment #: 30

Student Name: shirshah sahel

Class: comsc-110

Enter an operation (+,-,/,*)

+

Enter the first number

1

Enter the second number

2

Result:3

Enter an operation (+,-,/,*)

-

Enter the first number

7

Enter the second number

6

Result:1

Enter an operation (+,-,/,*)

*

Enter the first number

8

Enter the second number

9

Result:72

Enter an operation (+,-,/,*)

/

Enter the first number

100

Enter the second number

5

Result:20

#include

using namespace std;

int main()

{

double num1,num2,result;

char option;

cout<<"Enter an operation (+,-,/,*) ";

cin>>option;

cout<<"Enter the first number ";

cin>>num1;

cout<<"Enter the second number ";

cin>>num2;

switch(option)

{

case '+' :

result = num1+num2;

break;

case '-' :

result = num1-num2;

break;

case '*' :

result = num1*num2;

break;

case '/' :

result = num1/num2;

break;

default :

cout<<"Invalid operator ";

}

cout<<"Result:"<

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David M Kroenke, David J Auer

6th Edition

0132742926, 978-0132742924

More Books

Students also viewed these Databases questions

Question

=+. What steps from RMF resonate most with you?

Answered: 1 week ago

Question

What sector and functional experience are we looking for?

Answered: 1 week ago

Question

What is the specific piece of work?

Answered: 1 week ago