/*************
Program: Assignment 9
Author: Alan P. Matie
Date: 20 Mar 2009
Description:
Write a program that does simple mathematics.
It must:
1. Ask for an operation
2. Ask for two numbers
3. Perform the calculation specified on the numbers
4. Check for division by zero and display an error message if encountered
5. Use a switch statement for the determination of mathematical operation and the operation itself
6. Use a post-test loop that stops upon the user entering ‘Q’
7. Displays beginning and ending messages printed only once each - before and after the loop
New Concepts: Switch
Challenges:
Last Modified: 24 Mar 2009
**************/
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
usingnamespace std;
// Declare constants
// Function Prototypes
int main()
{
// Declare variables below here
double x, z, result;
char operand;
// Initialization Section for real number output. DO NOT MOVE!
cout <<setiosflags(ios::fixed | ios::showpoint);
cout <<setprecision(2);
// Begin your "main processing" below here
cout <<"Welcome to the mathematical selector program!"<<endl;
do
{
cout <<"Please enter the operand of the problem you would like to solve:"<<endl;
cout <<"+ for addition"<<endl;
cout <<"- for subtraction"<<endl;
cout <<"* for multiplication"<<endl;
cout <<"/ for division"<<endl;
cout <<"Enter Q to quit"<<endl;
cout <<"Enter your choice ==> ";
cin>> operand;
cout <<"Please enter the two numbers ==> ";
cin >> x >> z;
switch (operand)
{
case'+':
result = x+z;
cout <<"The answer is: " << result <<endl;
break;
case'-':
result = x-z;
cout <<"The answer is: " << result <<endl;
break;
case'*':
result = x*z;
cout <<"The answer is: " << result <<endl;
break;
case'/':
if (z ==0 )
{
cout <<"That is an invalid operation" <<endl;
}
else
{
result = x/z;
cout <<"The answer is: " << result <<endl;
}
break;
default :
cout <<"That is an invalid operation" <<endl;
break;
}
}while (operand != 'Q');
cout <<"End of Program!"<<endl;
return 0;
}
// function definitions below here
There is no syntactical error in your code. The undesired results that you are seeing stem from the fact that you
input operand at the beginning of the do while loop and you check whether or not to exit at the end.
Regardless of whether or not that input is 'Q', the rest of the code in the loop will continue executing. If you
would like the loop to exit immediately, you need to modify or rearrange your code.
Also, please use [code][/code] blocks to post your code in the future. It makes it much easier to read.
Thanks for the response, although I don't know where/how to rearrange the code to make this end on 'Q'. Also I apologize for not using code blocks, but I honestly don't know what you are talking about; would you be willing to elaborate for me, so that I can post properly next time? Also I've noticed other people posting here and keeping their original indenting, and I was unable to do so, do I need to post in a method other than copy/paste; or manually redo all indenting?
Also my loop is required to be a post-test loop, unless I misread my textbook then that means a do-while; correct?
ran the program it seems to run fine as it is now even if you choose Q as your choice right off you have to enter 2 numbers before the do while loop works.
one solution is just prompt the user in the case statements to enter the number
do this for each case statement in your switch statement. Also as a case statement for the Quit option. Here is your program with the changes i described above
// test.cpp : Defines the entry point for the console application.
//
/*************
Program: Assignment 9
Author: Alan P. Matie
Date: 20 Mar 2009
Description:
Write a program that does simple mathematics.
It must:
1. Ask for an operation
2. Ask for two numbers
3. Perform the calculation specified on the numbers
4. Check for division by zero and display an error message if encountered
5. Use a switch statement for the determination of mathematical operation and the operation itself
6. Use a post-test loop that stops upon the user entering ‘Q’
7. Displays beginning and ending messages printed only once each - before and after the loop
New Concepts: Switch
Challenges:
Last Modified: 24 Mar 2009
**************/
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
usingnamespace std;
// Declare constants
// Function Prototypes
int main()
{
// Declare variables below here
double x, z, result;
char operand;
// Initialization Section for real number output. DO NOT MOVE!
cout <<setiosflags(ios::fixed | ios::showpoint);
cout <<setprecision(2);
// Begin your "main processing" below here
cout <<"Welcome to the mathematical selector program!"<<endl;
do
{
cout <<"Please enter the operand of the problem you would like to solve:"<<endl;
cout <<"+ for addition"<<endl;
cout <<"- for subtraction"<<endl;
cout <<"* for multiplication"<<endl;
cout <<"/ for division"<<endl;
cout <<"Enter Q to quit"<<endl;
cout <<"Enter your choice ==> ";
cin>> operand;
switch (operand)
{
case'Q':
break;
case'+':
cout <<"Please enter the two numbers ==> ";
cin >> x >> z;
result = x+z;
cout <<"The answer is: " << result <<endl;
break;
case'-':
cout <<"Please enter the two numbers ==> ";
cin >> x >> z;
result = x-z;
cout <<"The answer is: " << result <<endl;
break;
case'*':
cout <<"Please enter the two numbers ==> ";
cin >> x >> z;
result = x*z;
cout <<"The answer is: " << result <<endl;
break;
case'/':
cout <<"Please enter the two numbers ==> ";
cin >> x >> z;
if (z ==0 )
{
cout <<"That is an invalid operation" <<endl;
}
else
{
result = x/z;
cout <<"The answer is: " << result <<endl;
}
break;
default :
cout <<"That is an invalid operation" <<endl;
break;
}
}while (operand != 'Q');
cout <<"End of Program!"<<endl;
return 0;
}
// function definitions below here
I recieved a 19 out of 20 points, thanks for the input everyone!!
BTW I posted my instructors comments below:
Ending problem - two problems:
you ask for the numbers when you might have a Q; you have to ask for the numbers in the case
you do not have a case for the Q, so the default gives an error message