Question

Simple calculator program. The program should ask the user for a number, an operator, and then...

Simple calculator program. The program should ask the user for a number, an operator, and then a second number. The operator should be either '+', '-', 'x', or '/'. The program should then report the answer to the problem. Use a switch statement, not an if statement. Note that "operator" is a reserved word in C++, so you cannot use it as a variable name. Use double variables and setprecision(2).

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include <iomanip> // std::setprecision

using namespace std;

int main()
{
// Declaring variables
double num1, num2;
char operatr, ch;
  
//Setting the number to two decimal places
std::cout << std::setprecision(2) << std::fixed;

// Getting the first number from the user
cout << "Enter a first number:";
cin >> num1;
while (true)
{


// getting the operator
cout << "Enter Operator :";
cin >> operatr;

// Based on operator corresponding case will get executed
switch (operatr)
{
case '+':
{
// Getting the Second number
cout << "Enter a Second Number :";
cin >> num2;

// Displaying the result
cout << num1 << " + " << num2 << " is :" << (num1 + num2) << endl;
break;
}
case '-':
{
// Getting the Second number
cout << "Enter a Second Number :";
cin >> num2;
cout << num1 << " - " << num2 << " is :" << (num1 - num2) << endl;
break;
}
case '*':
{
// Getting the Second number
cout << "Enter a Second Number :";
cin >> num2;
// Displaying the result
cout << num1 << " * " << num2 << " is :" << (num1 * num2) << endl;
break;
}
case '/':
{

do
{
// Getting the Second number
cout << "Enter a Second Number :";
cin >> num2;
if (num2 == 0)
{
cout << "** Invalid.Should'nt be Zero **" << endl;
}


} while (num2 == 0);
// Displaying the result
cout << num1 << " / " << num2 << " is :" << (num1 / num2) << endl;
break;
}
default:
{
cout << "** Invalid Operator **" << endl;
continue;
}
}
cout << "\nDo you Want to continue(Y/N):";
cin >> ch;

if (ch == 'y' || ch == 'Y')
{
cout << "Enter a first number:";
cin >> num1;
continue;
}
else
break;
}


return 0;
}

_______________________

Output:

CAProgram Files (x86) Dev-CppMinGW64 bin\OperatorAndTwoNumbers.exe nter Operator :$ nter OperatoF + Enter a first number:45 *

________________Thank You

Add a comment
Know the answer?
Add Answer to:
Simple calculator program. The program should ask the user for a number, an operator, and then...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please solve in java Write a program that serves as a simple calculator. The program should...

    Please solve in java Write a program that serves as a simple calculator. The program should ask the user for a number, an operator, and then a second number. The operator should be either '+', '-', 'x', or '/'. The program should then report the answer to the problem. Use a switch statement, not an if statement. You'll need to read the operator as a String and compare it using Strings, as you did in 4.1. Turn in your source...

  • This program should ask the user for the maximum number to print out to and then...

    This program should ask the user for the maximum number to print out to and then print each evennumber starting from 0 to the maximum. You may want to use % operator which gives you the remainder. Some results of the operator is as follows: • 0 % 2 = 0 • 1 % 2 = 1 • 2 % 2 = 0 • 3 % 2 = 1 • 4 % 2 = 0

  • Java program should ask the user for the maximum number to print out to and then...

    Java program should ask the user for the maximum number to print out to and then print each evennumber starting from 0 to the maximum. You may want to use % operator which gives you the remainder. Some results of the operator is as follows: • 0 % 2 = 0 • 1 % 2 = 1 • 2 % 2 = 0 • 3 % 2 = 1 • 4 % 2 = 0

  • This program will ask the user to enter a number of players, then ask the user...

    This program will ask the user to enter a number of players, then ask the user for each player's name and score. Once all of the players have been entered, the program will display a bar chart with each of their scores scaled as a percentage of the maximum score entered. See below for details on exactly how this should work - an example transcript of how the program should work is shown below (remember that values entered by the...

  • Write a Java program which allows the user to perform simple tasks on a calculator. A...

    Write a Java program which allows the user to perform simple tasks on a calculator. A series of methods allows the user to select an operation to perform and then enter operands. The first method displays a menu, giving the user the choice of typing in any one of the following: +, -, *, /, or % representing the usual arithmetic operators (Each operation should be done on numbers negative(-) to positive(+), positive(+) to negative(-), negative(-) to negative(-), and positive(+)...

  • Digit to WordsWrite a C program that asks the user for a two-digit number, then prints...

    Digit to WordsWrite a C program that asks the user for a two-digit number, then prints the English wordfor the number. Your program should loop until the user wants to quit.Example:Enter a two-digit number: 45You entered the number forty-five.Hint: Break the number into two digits. Use one switch statement to print the word for the firstdigit (“twenty,” “thirty,” and so forth). Use a second switch statement to print the word for thesecond digit. Don’t forget that the numbers between 11...

  • Create a 'simple' calculator. The calculator should be able to add, subtract, multiply, and divide two...

    Create a 'simple' calculator. The calculator should be able to add, subtract, multiply, and divide two numbers. You can use whatever format you want to receive the numbers (such as textboxes). You also can use whatever method (such as a button for each operation +, -, *, /) to determine what operation the user wants to accomplish. The output should be displayed in a similar format to "1 + 2 = 3" or "1 - 2 = -1". Make sure...

  • IN PERL Write a program that will ask the user for a number and will print...

    IN PERL Write a program that will ask the user for a number and will print all the integers in order starting from 1 until the number. Use the <> operator to ask for the user's input. For this program you will write a countdown timer. Your time will count down from 1 minute (60 seconds) to 0. Once the timer gets to 0 seconds the system will display a message that reads: RING RING RING! -using do while-

  • code in C++ Create a program that uses the pass by reference operator instead of the...

    code in C++ Create a program that uses the pass by reference operator instead of the return command. Your main program will need to: create empty variables call void function 1 with no input to display your name to the screen call function 2 to collect the square feet that a gallon of paint covers from user call function 2 to collect the square feet of wall that needs to be painted call function 3 to calculate the number of...

  • Write a program that would ask the user to enter an input file name, and an...

    Write a program that would ask the user to enter an input file name, and an output file name. Then the program reads the content of the input file, and read the data in each line as a number (double). These numbers represent the temperatures degrees in Fahrenheit for each day. The program should convert the temperature degrees to Celsius and then writes the numbers to the output file, with the number of day added to the beginning of the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT