Fix any errors that you find. Use in-line comments to explain how and why you fixed the bugs. Test your program. If you found and fixed all the errors, your code should function as specified: The code should allow users to enter basic, one operator arithmetic expressions using integers or doubles. Then, it should calculate the result of the expression and output the results. For example, if the user inputs “2 + 2,” the calculator should output “2 + 2 = 4.” The operations the program should perform are addition (+), subtraction (-), multiplication (*), and division (/). The user should be asked whether they wish to continue after evaluating each expression. If the user types “Y” or “y,” the program should ask for a new arithmetic expression. If the user types “N” or “n,” the program should terminate with the message: “Program Finished.”
Fix any errors that you find. Use in-line comments to explain how and why you fixed the bugs. Test your program. If you found and fixed all the errors, your code should function as specified: The code should allow users to enter basic, one operator arithmetic expressions using integers or doubles. Then, it should calculate the result of the expression and output the results. For example, if the user inputs “2 + 2,” the calculator should output “2 + 2 = 4.” The operations the program should perform are addition (+), subtraction (-), multiplication (*), and division (/). The user should be asked whether they wish to continue after evaluating each expression. If the user types “Y” or “y,” the program should ask for a new arithmetic expression. If the user types “N” or “n,” the program should terminate with the message: “Program Finished.”
Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
Related questions
Question
- Fix any errors that you find. Use in-line comments to explain how and why you fixed the bugs.
- Test your program. If you found and fixed all the errors, your code should function as specified:
- The code should allow users to enter basic, one operator arithmetic expressions using integers or doubles. Then, it should calculate the result of the expression and output the results. For example, if the user inputs “2 + 2,” the calculator should output “2 + 2 = 4.”
- The operations the program should perform are addition (+), subtraction (-), multiplication (*), and division (/).
- The user should be asked whether they wish to continue after evaluating each expression. If the user types “Y” or “y,” the program should ask for a new arithmetic expression. If the user types “N” or “n,” the program should terminate with the message: “Program Finished.”
![```cpp
#include <iostream>
c/Calculator.cpp
void main()
{
char statement[100];
int op1;
int op2;
char operation;
char answer = "Y";
while (answer=='y')
{
cout << "Enter expression" << endl; // Space needed after <<
cin >> opl >> operation >> op2; //Swap the op1 and op2, wrong place.
if (operation == '+') // Change " " to ' ' for character
cout << opl << " + " << op2 << " = " << opl + op2 << endl; //Reverse the >> after op2
if (operation == '-')
cout << opl << " - " << op2 << " = " << opl - op2 << endl; //Reverse the >> for cout statement
if (operation == '*') //Semicolon needed
cout << opl << " * " << op2 << " = " << opl * op2 << endl; //Add ; to end of line and replaced the / for *
if (operation == '/') //Semicolon needed
cout << opl << " / " << op2 << " = " << opl / op2 << endl; //Replace * for /
cout << "Do you wish to evaluate another expression? " << endl;
cin >> answer;
}
}
```
### Explanation
The above C++ code is intended to be a simple calculator program. It allows the user to input an arithmetic expression, evaluates it, and then outputs the result. Below are the key elements and annotations for the code:
1. **Libraries and Main Function**:
- `#include <iostream>`: This includes the I/O stream library for input and output functions.
- `void main()`: The program execution starts from this main function.
2. **Variables**:
- `char statement[100];`: An array to hold the expression as a string.
- `int op1, op2;`: Integer variables to hold the two operands.
- `char operation;`: Character variable to store the operator.
- `char answer = "Y";`: Character variable to control the loop, initialized with "Y".
3. **Input and Calculation Loop**:
- The program uses a `while` loop to repeatedly ask for input as long as the user decides to continue (answer is '](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F471cb0b1-24a7-4f47-a754-0c03ff5b6852%2F2707170f-fa42-4396-a7ae-ef89d1f546d8%2F6mkhlf_processed.png&w=3840&q=75)
Transcribed Image Text:```cpp
#include <iostream>
c/Calculator.cpp
void main()
{
char statement[100];
int op1;
int op2;
char operation;
char answer = "Y";
while (answer=='y')
{
cout << "Enter expression" << endl; // Space needed after <<
cin >> opl >> operation >> op2; //Swap the op1 and op2, wrong place.
if (operation == '+') // Change " " to ' ' for character
cout << opl << " + " << op2 << " = " << opl + op2 << endl; //Reverse the >> after op2
if (operation == '-')
cout << opl << " - " << op2 << " = " << opl - op2 << endl; //Reverse the >> for cout statement
if (operation == '*') //Semicolon needed
cout << opl << " * " << op2 << " = " << opl * op2 << endl; //Add ; to end of line and replaced the / for *
if (operation == '/') //Semicolon needed
cout << opl << " / " << op2 << " = " << opl / op2 << endl; //Replace * for /
cout << "Do you wish to evaluate another expression? " << endl;
cin >> answer;
}
}
```
### Explanation
The above C++ code is intended to be a simple calculator program. It allows the user to input an arithmetic expression, evaluates it, and then outputs the result. Below are the key elements and annotations for the code:
1. **Libraries and Main Function**:
- `#include <iostream>`: This includes the I/O stream library for input and output functions.
- `void main()`: The program execution starts from this main function.
2. **Variables**:
- `char statement[100];`: An array to hold the expression as a string.
- `int op1, op2;`: Integer variables to hold the two operands.
- `char operation;`: Character variable to store the operator.
- `char answer = "Y";`: Character variable to control the loop, initialized with "Y".
3. **Input and Calculation Loop**:
- The program uses a `while` loop to repeatedly ask for input as long as the user decides to continue (answer is '
Expert Solution

This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution!
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images

Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Recommended textbooks for you

Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education

Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON

Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON

Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education

Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON

Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON

C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON

Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning

Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education