Alright so I have to create a simple calculator and need to include the following features:
- an "if... else" or "switch" structure
- Display of an invalid operation, divisor, and modulus
- a properly constructed "loop"
I've spent a couple hours on it but I'm making progress (very slowly)
// ALGORITHM
// Define variables and calculations
//
// Welcome User to Calculator
// Prompt user to enter first number
// - If invalid, prompt user to re-enter first number
// - If valid, move to function selection
// Prompt user to select mathmatical function
// - If invalid, prompt user to re-enter functions
// - If valid, move to second number selection
// Prompt user to enter second number
// - If invalid, prompt user to re-enter first number
// - If valid, move to function selection
// Calculate equation
// Display equation
// Prompt user to perform another calculation (loop)
// Prompt user to exit calculator using EOF input
#include <iostream>
#include <iomanip>
#include <cmath>
usingnamespace std;
int main()
{
// Define Variables & Calculations
char function;
double m,n;
double sum (m + n);
double difference (m - n);
double product (m * n);
double quotient ( m / n);
// Welcome user
cout << "Hi and Welcome to the Calculator.\n" << endl;
// Prompt user to enter first number
cout << "Please enter the first number and press Enter: \n" << endl;
cin >> m;
// Prompt user to enter mathmatical function
cout << "Please enter mathmatical function and press Enter""(ex. '+' , '-' , '*' , '/' , '%'): \n" << endl;
cin >> function;
// Prompt user to enter second number
cout << "Please enter second number and press Enter: \n" << endl;
cin >> n;
// Compute calculation
if (m + n)
cout << "The answer to your problem is " << sum << endl;
elseif (m - n)
cout << "The answer to your problem is " << difference << endl;
elseif (m * n)
cout << "The answer to your problem is " << product << endl;
elseif (m / n)
cout << "The answer to your problem is " << quotient << endl;
system ("pause");
return 0;
}
The math on this keeps giving me a answer in scientific notation format.
"5 + 5 = 4.05359e-307"
So I need to display a invalid operation. Here's what I have so far but it's not working.
1 2 3 4 5 6 7 8
cout << "Please enter the function (+,-,*,/,%) : \n" << endl;
cin >> function;
if (function == '+', '-', '*', '/', '%');
else (function != '+', '-', '*', '/', '%');
cout << "Invalid function! Please try again.\n" << endl;
Do I need to define "+", "-", etc, in the "char function" command?
Your answer variables are all going to be garbage. You declare m and n, but give them no values, then use them to calculate 'sum', etc. So those values are also garbage.
Your if statements are meaningless. The first says, "if m + n doesn't equal 0 print the value of sum". The others are similarly flawed.
I don't want to sound discouraging, but it's all wrong. Your assumptions about how the language works are way off. They are valid (most of them, anyway), so it's not all lost. A hypothetical language could conceivably work like this. This one doesn't.
I find lines 34-37 troubling. I get the feeling you were trying to do something very specific, but I would rather not say what.
You should scrap lines 34-37 and 64-78.
Start by implementing one operation at a time and verifying that it works, then, and only then, start adding the other ones.
A small hint: you can't separate checking the 'function' variable and deciding which operation to perform*.
Another one: In the code you posted, you're not performing any calculations on useful data.
Ok, 3 things to get you moving along. First, line 64 if (m + n), This means take m, add it to n, and branch based on if it's true of false. c++ defines any non 0 value as true. This means no matter what you enter, it will go to the first if statement (unless it adds up to 0). What you should have there is if(function == '+')
The second issue is on line 34-37, those are function prototypes, but you never actually define what the function does. That means you are getting undefined behavior, and the number it is returning will be some archaic result of how your compiler chooses to implement that. The function should look more like: double sum(double value1, double value2){return value1+value2}. Functions are also typically global. These are really simple calculations, so it may be wise to not have them as functions at all, and just do the calculations as you are using cout.
The third is how to get it to loop, you will need to put everything between lines 43 and 79 in a while loop, and have a variable such as doAnotherCalculation, and ask the user after you do 1 calculation if they want to do another.
Edit: Actually, I'm not entirely sure why lines 34 and 66 don't issue a compiler error. He's doesnt define what types m and n are in the function prototype (or does it come over because he declared m and n as variables?), and on line 66 he calls sum without the () to show it's a function.
i'm a little bit surprised about the functions declarations, i thought maybe there's something i didn't know yet (another syntax that i probably don't know yet till now, but after helios' explanations i'm relived that i didn't miss anything :) )
the functions should be written
1 2 3
double sum (double n, double m) {return (m + n);}
double difference (double n, double m) {return (n - m);}
//and so forth...
oh yeah, you don't have to include the <cmath> (and <iomanip> probably)
bool retry;
do
{
retry = false;
...
if (function == '+')
cout << "The answer to your problem is " << (m + n) << endl;
elseif (function == '-')
cout << "The answer to your problem is " << (m - n) << endl;
...
else
{
retry = true;
cout << "Please enter the function (+,-,*,/,%) : \n" << endl;
}
// You may ask ther user here if he wants to do it again (if not retry is already true)
}
while(retry);
Lines 34-37 aren't function declarations, so that's why you haven't seen them before.
They're equivalent to double sum = m + n; etc.
Edit: ah, that was directed at chipp and Intrexa.
But they were probably supposed to be function declarations. However, lambda functions are declared with different syntax in C++:
char again;
do {
......
.......
..........
..........
............
cout<<"Do you want to retry? Y for Yes, N for No!"<<endl;
cin>>again;
}
while (again == 'Y' || again == 'y');
I appreciate the feedback (the good and the bad)... I've went back to the drawing board and have made some progress. I'm still having trouble with a few things though:
#1- I can get it to display the proper error message when attempting to divide/modulus by "0".
#2- After it display the error message for dividing/modulus by "0" how can I get it to restart the sequence and have the user enter the calculation again?
# 3- I can't figure why my loop is not working correctly. The program is terminating when i need it to prompt the user " Would you like to enter another calculation?"
#include <iostream>
usingnamespace std;
int main()
{
// Define Variables
char function;
char redo;
int m,n;
// Welcome user
cout << "Hi and welcome to the Calculator.\n" << endl;
// Prompt user to enter calculation
do
{
cout << "Please enter the equation you would like to solve: \n" << endl;
cin >> m >> function >> n;
// Compute calculation
if (!cin.eof())
{
if (function == '+')
cout << "The answer to " << m << " " << function << " " << n << " "
<< "is: "<< m + n << endl;
elseif (function == '-')
cout << "The answer to " << m << " " << function << " " << n << " "
<< "is: "<< m - n << endl;
elseif (function == '*')
cout << "The answer to " << m << " " << function << " " << n << " "
<< "is: "<< m * n << endl;
elseif (function == '/')
{
if (n == 0)
cout << "Cannot divide by 0. Please re-enter." << endl;
}
cout << "The answer to " << m << " " << function << " " << n << " "
<< "is: "<< m / n << endl;
elseif (function == '%')
{
if (n == 0)
cout << "Cannot modulus by 0. Please re-enter." << endl;
}
cout << "The answer to " << m << " " << function << " " << n << " "
<< "is: "<< m % n << endl;
else
cout << "Invalid function! Please re-enter equation: \n" << endl;
}
cout << "Would you like to enter another calculation? (Y/N) : " << endl;
cin >> redo;
while ( redo == 'y' || 'Y')
while (!cin.eof());
system ("pause");
return 0;
}
you forgot and misplaced the closing bracket ('}') dude... in here:
1 2 3 4 5 6 7 8 9 10
if (n == 0)
cout << "Cannot modulus by 0. Please re-enter." << endl;
}
cout << "The answer to " << m << " " << function << " " << n << " "
<< "is: "<< m % n << endl;
else
cout << "Invalid function! Please re-enter equation: \n" << endl;
}
should be:
1 2 3 4 5 6 7 8 9
if (n == 0)
cout << "Cannot modulus by 0. Please re-enter." << endl;
}
cout << "The answer to " << m << " " << function << " " << n << " "
<< "is: "<< m % n << endl;
} //here, the sign of the end of your "if"
else
cout << "Invalid function! Please re-enter equation: \n" << endl;
and then:
1 2 3 4
cout << "Would you like to enter another calculation? (Y/N) : " << endl;
cin >> redo;
while ( redo == 'y' || 'Y')
should be:
1 2 3
cout << "Would you like to enter another calculation? (Y/N) : " << endl;
cin >> redo;
} /* here, the sign of the end of your "do" */while ( redo == 'y' || 'Y');
btw, what for cin.eof ? i don't really know what it does... (haven't learn it yet -_-!)
cin.eof() checks if an end-of-file character has been read by the standard input stream. Run a program with it* and enter ^Z (Ctrl-Z) in Windows, or ^D in unix.
*
1 2 3 4 5 6 7 8 9 10 11 12
#include <iostream>
#include <string>
usingnamespace std;
int main() {
string input;
cout << "Enter data. Type ^Z (Windows) or ^D (Unix) to quit." << endl;
while( !cin.eof() )
getline( cin, input ); //read a line of text from standard input
return 0;
}
For lines 66 and 82 I keep getting the following errors when I try to compile:
66: expected primary-expression before "else"
66: expected ";" before else
82: expected 'while' at end of input
82: expected '(' at end of input
82: expected primary-expression at end of input
82: expected ')' at end of input
82: expected ';' at end of input
82: expected '}' at end of input
#include <iostream>
usingnamespace std;
int main()
{
// Define Variables
char function;
char redo;
int m,n;
do
{
// Welcome user
cout << "Hi and welcome to the Calculator.\n" << endl;
// Prompt user to enter calculation
{
cout << "Please enter the equation you would like to solve: \n" << endl;
cin >> m >> function >> n;
if (!cin.eof())
{
// Compute calculation
if (function == '+')
// Try and remove " " once compiled
cout << "The answer to " << m << " " << function << " " << n << " "
<< "is: "<< m + n << endl;
if (function == '-')
cout << "The answer to " << m << " " << function << " " << n << " "
<< "is: "<< m - n << endl;
if (function == '*')
cout << "The answer to " << m << " " << function << " " << n << " "
<< "is: "<< m * n << endl;
if (function == '/')
cout << "The answer to " << m << " " << function << " " << n << " "
<< "is: "<< m / n << endl;
elseif (n == 0)
cout << "You cannot divide by 0. Please re-enter calculation." << endl;
if (function == '%');
elseif (n == 0)
cout << "Cannot modulus by 0. Please re-enter." << endl;
cout << "The answer to " << m << " " << function << " " << n << " "
<< "is: "<< m % n << endl;
elseif
cout << "Invalid function! Please re-enter equation: \n" << endl;
}
//
// cout << "Would you like to enter another calculation? (Y/N) : " << endl;
// cin >> redo;
//
// while ( redo == 'y' || 'Y')
//
}
while (!cin.eof());
system ("pause");
return 0;
}
line 63. Put lines 60-65 in brackets {}. An if statement only executes the next single instruction, between the if statements on line 59 and 66 there are 2 instructions, so that the compiler thinks it is done with the if/elseif block, so on line 66 it sees that elseif as an entirely new line of if statements.
Edit: As for the second part, where it is having an issue with line 82, that is extremely straight forward, and is telling you exactly what is wrong and what it is expecting. On line 13 you have a do statement that doesn't line up with the ending while statement.
Thanks man... I really appreciate the help and that was huge.
I still can't figure why my loop is not working correctly? I need it to prompt the user to continue to enter calculations until "ctrl + z" are entered to end the program.
Secondly, my error message when a user attempts to divide by "0" is working correctly (line 53). The program divides by "0", returns 0 and then fails.
Chipp, I appreciate your help but I'm only more confused with your code. I've only been coding in C++ for 3 weeks. The goal of this assignment is for me to use "if...else" statement's. I've reworked it and I think I'm 90% of the way there. I'm just running into the following issues:
1. The "else" statement in line 92 displays all the time even when a simple and valid equation is entered.
2. My professor said "The test for a zero divisor or mod should immediately follow the function test and not be an else if." When I do this it doesn't compile. I've tried putting the "if" statements in braces directly under their respective equations with no luck.
3. When I divide by "0" it doesn't return an answer and the program crashes. I need it to return "Cannot divide by 0" (lines 77).
4. When I try and modulus by "0" it returns "Cannot divide by 0" (line 77) and also "Cannot modulus by 0" (line 89). Why is that?
// Simple Calculator ALGORITHM
// Do Loop
// Define variables
// Welcome User to Calculator
// Prompt user to enter first number
// - If invalid, prompt user to re-enter first number
// - If valid, move to function selection
// Prompt user to select mathmatical function
// - If invalid, prompt user to re-enter functions
// - If valid, move to second number selection
// Prompt user to enter second number
// - If invalid, prompt user to re-enter first number
// - If valid, move to function selection
// Calculate equation
// Display equation
// Prompt user to perform another calculation (loop)
// Prompt user to exit calculator using EOF input
#include <iostream>
usingnamespace std;
int main()
{
// Define Variables
char function;
char redo;
int m,n;
// Welcome user
cout << "Hi and welcome to the Calculator.\n" << endl;
// Prompt user to enter calculation
do
{
cout << "Please enter the equation you would like to solve: \n" << endl
<< endl;
cin >> m >> function >> n;
// Compute calculation
if (function == '+')
cout << "The answer to " << m << " " << function << " " << n << " "
<< "is: "<< m + n << endl;
elseif (function == '-')
cout << "The answer to " << m << " " << function << " " << n << " "
<< "is: "<< m - n << endl;
elseif (function == '*')
cout << "The answer to " << m << " " << function << " " << n << " "
<< "is: "<< m * n << endl;
elseif (function == '/')
cout << "The answer to " << m << " " << function << " " << n << " "
<< "is: "<< m / n << endl;
if (n == 0)
cout << "You cannot divide by 0. Please re-enter calculation." << endl;
elseif (function == '%')
cout << "The answer to " << m << " " << function << " " << n << " "
<< "is: "<< m % n << endl;
if (n == 0)
cout << "Cannot modulus by 0. Please re-enter." << endl;
else
cout << "Invalid function! Please re-enter equation. \n" << endl;
}
while (!cin.eof());
system ("pause");
return 0;
}
I've made a similar calculator (it takes multiple numbers and parens however, but it's still the same idea) if you want me to post the source. I used a switch statement for parsing the string. Also you have a ton of extra white space in there, I can barely read that program, its almost twice as many lines as it could be.
As for your code, you want to put the checks for whether the user entered 0 before you output the answer or your program is going to crash. Also even if the user does enter 0 you're not doing anything about, and whenever you divide by zero on a computer the program crashes, so try using exit(1) or return 0.
The reason the else on line 92 keeps getting displayed is because you left out your curly braces on line 81 so the else on line 92 is an else to the if statement on line 87. This means it will be displayed whenever the user doesn't enter 0 when they're dividing. Just give your else if statement curly braces and you'll be good.