Public Group

# Help with my Calculator

This topic is 2537 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm new to programming and i need some help with adding a way for the user to start over at the Calculator() function
Also if there is any tips and/or things I did wrong please tell me.

My main.cpp
 #include "stdafx.h" #include <iostream> #include <conio.h> #include <string> #include "Header File.h" using namespace std; int main() { cout <<"Enter your name:\n"; string sName; cin >> sName; cout <<"\n\nNice to meet you, " << sName <<endl; Calculator(); { cout <<"\n\nThis is a simple calculator"<< endl; cout <<"\nEnter the first number: " <<endl; double dNumb1; cin >> dNumb1; cout <<"\nEnter the proper mathmatical operation(+, -, /, or *); " << endl; char chOperation; cin >> chOperation; cout <<"\nEnter the second number: " << endl; double dNumb2; cin >> dNumb2; cout <<"\nAnswer: "; if (chOperation == '+') cout << dNumb1 <<" + "<< dNumb2 <<" = "<< dNumb1 + dNumb2 << endl; if (chOperation == '-') cout << dNumb1 <<" - "<< dNumb2 <<" = "<< dNumb1 - dNumb2 << endl; if (chOperation == '*') cout << dNumb1 <<" * "<< dNumb2 <<" = "<< dNumb1 * dNumb2 << endl; if (chOperation == '/') cout << dNumb1 <<" / "<< dNumb2 <<" = "<< dNumb1 / dNumb2 << endl; else cout <<"You entered an invalid operation!"<< endl; } cout <<"\nPress enter to quit this program....."; _getch(); return 0; } 

 #include "stdafx.h" #ifndef Calculator() #define Calculator() #endif 

##### Share on other sites
If you want to do a Calculator function, you shoul have something like this

 #include "stdafx.h" #include <iostream> #include <conio.h> #include <string> //#include "Header File.h" using namespace std; void Calculator() { cout <<"\n\nThis is a simple calculator"<< endl; cout <<"\nEnter the first number: " <<endl; double dNumb1; cin >> dNumb1; cout <<"\nEnter the proper mathmatical operation(+, -, /, or *); " << endl; char chOperation; cin >> chOperation; cout <<"\nEnter the second number: " << endl; double dNumb2; cin >> dNumb2; cout <<"\nAnswer: "; if (chOperation == '+') cout << dNumb1 <<" + "<< dNumb2 <<" = "<< dNumb1 + dNumb2 << endl; else if (chOperation == '-') cout << dNumb1 <<" - "<< dNumb2 <<" = "<< dNumb1 - dNumb2 << endl; else if (chOperation == '*') cout << dNumb1 <<" * "<< dNumb2 <<" = "<< dNumb1 * dNumb2 << endl; else if (chOperation == '/') cout << dNumb1 <<" / "<< dNumb2 <<" = "<< dNumb1 / dNumb2 << endl; else cout <<"You entered an invalid operation!"<< endl; } int main() { cout <<"Enter your name:\n"; string sName; cin >> sName; cout <<"\n\nNice to meet you, " << sName <<endl; Calculator(); cout <<"\nPress enter to quit this program....."; _getch(); return 0; } 

The code of the function must be outside of the main, and you call the function inside the main...
Also you need to put "else if" at all the if of the function (exept the first). If you don't, you will see the "You entered an invalid operation!" every time you don't press the "/"

I hope I haven't missunderstood the what you were doing.

##### Share on other sites
To allow the user to keep using your calculator, I would make the Calculator function return a bool that indicates to the main function whether or not the user wanted to quit, and then put the Calculator function in a loop that stops when the function returns true. If you have the Calculator function return true when the user wants to quit and false otherwise, you could simply have this code calling the Calculator function:
 while(!Calculator()) { } 

##### Share on other sites
Your header file does not contain any code so it shouldn't be necessary to include it unless you put the function declaration in it. Also as a little nit pick it is standard convention to use capital letters for your header guard and append "_H" to them, i.e.

 #ifndef CALCULATOR_H #define CALCULATOR_H #include "stdafx.h" void Calculator() { // Insert Relevant Code } #endif 

As RulerOfNothing suggested if would make more sense for the Calculator function to return a boolean would make more sense. For example if you wanted the program to ask the user if he wanted to quit or not you could do:

 void Calculator() { // Existing Code ... char chExit; cout << "\nWould you like to exit? (Y/N)" << endl; cin >> chExit; if (chExit == 'Y') return true; else return false; } 

And then using what the code RulerOfNothing posted in your main function.

##### Share on other sites
Ok I made the changes that were needed but I'm still having some problems with my while loop and getting the Calculator() to run over again if needed.

##### Share on other sites

Ok I made the changes that were needed but I'm still having some problems with my while loop and getting the Calculator() to run over again if needed.

Could you post the changed code in that case?

##### Share on other sites
 #include "stdafx.h" #include <iostream> #include <conio.h> #include <string> using namespace std; void Calculator() { cout <<"\n\nThis is a simple calculator"<< endl; cout <<"\nEnter the first number: " <<endl; double dNumb1; cin >> dNumb1; cout <<"\nEnter the proper mathmatical operation(+, -, /, or *); " << endl; char chOperation; cin >> chOperation; cout <<"\nEnter the second number: " << endl; double dNumb2; cin >> dNumb2; cout <<"\nAnswer: "; if (chOperation == '+') cout << dNumb1 <<" + "<< dNumb2 <<" = "<< dNumb1 + dNumb2 << endl; else if (chOperation == '-') cout << dNumb1 <<" - "<< dNumb2 <<" = "<< dNumb1 - dNumb2 << endl; else if (chOperation == '*') cout << dNumb1 <<" * "<< dNumb2 <<" = "<< dNumb1 * dNumb2 << endl; else if (chOperation == '/') cout << dNumb1 <<" / "<< dNumb2 <<" = "<< dNumb1 / dNumb2 << endl; else cout <<"You entered an invalid operation!"<< endl; cout <<"Would you like to run this program again?(Y/N)"<< endl; char chLoop; cin >> chLoop; if (chLoop == 'Y') //I know this was a complete fail lol { return true; } else { return false; } } int main() { cout <<"Enter your name:\n"; string sName; cin >> sName; cout <<"\n\nNice to meet you, " << sName <<endl; Calculator(); cout <<"\nPress enter to quit this program....."; _getch(); return 0; } 

##### Share on other sites
Well, you need to modify the signature of the Calculator function so that the Calculator function returns a bool instead of nothing. You can do this by replacing void Calculator() with bool Calculator(). After you do this, you can use Calculator() in any place that expects a boolean value. Now a while loop is built like this:
 while(Condition) { stuff } 
where Condition is a boolean value, and stuff is program code. What this does is execute the code in the braces so long as Condition is true. So if you have  while(!Calculator()) { } 
then the program will first run Calculator(), take the logical inverse of the result, and do the code inside the braces (in this case nothing) then run Calculator again if Calculator returned false the first time.

##### Share on other sites
I finally got it done with a do while loop thanks for everyone's help.

1. 1
2. 2
Rutin
21
3. 3
A4L
15
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633736
• Total Posts
3013603
×