Hi all
I'm new to C++ and i decided to make a calculator with my limited knowledge. It works and all but it keeps crashing when it finishes. Anyone have an explanation/solution?
Here's my source code:
#include <iostream>
#include <string>
using namespace std;
class CALCULATIONS{
public:
int enterA(){
cin >> a;
}
int enterB(){
cin >> b;
}
int showA(){
cout << a;
}
string addA(){
if(b=="+"){
cout << "\n" << "Nice, what are you adding " << a << " by?\n\n";
cin >> c;
cout << "\n" << "I think it's " << a+c << endl;
cout << "Ima stop working now for no reason ";
}
else
subtractA();
}
string subtractA(){
if(b=="-"){
cout << "\n" << "Nice, what are you subtracting " << a << " by?\n\n";
cin >> c;
cout << "\n" << "I think it's " << a-c << endl;
cout << "Ima stop working now for no reason ";
}
else
multiplyA();
}
string multiplyA(){
if(b=="*"){
cout << "\n" << "Nice, what are you multiplying " << a << " by?\n\n";
cin >> c;
cout << "\n" << "I think it's " << a*c << endl;
cout << "Ima stop working now for no reason ";
}
else
divideA();
}
string divideA(){
if(b=="/"){
cout << "\n" << "Nice, what are you dividing " << a << " by?\n\n";
cin >> c;
cout << "\n" << "I think it's " << a/c << endl;
cout << "Ima stop working now for no reason ";
}
else
addA();
}
private:
int a;
string b;
int c;
};
int main()
{
CALCULATIONS caLc;
cout << "Hi, I'm a calculator ^_^\n\n\nEnter a number and press enter please... \n\n";
caLc.enterA();
cout << "\n" << "Cool, now what do you want to do with ";
caLc.showA();
cout << "?\n\n" << "(+) Add ?\n" << "(-) Subtract ?\n" << "(*) Multiply ?\n" << "(/) Divide ?\n\n";
caLc.enterB();
caLc.addA();
Re: My calculator program keeps crashing at the end
If you're looking for opinions, I'd suggest a separate function that gets the numbers, and passing those numbers to the add, multiply functions. Any time you're doing the same thing repeatedly, you should be using a function that you call.
Why do you have calls like
else
multiplyA();
in all operation functions?
You know the operation type in whateva. Just call the appropriate function after enterB.
Last edited by GCDEF; January 21st, 2013 at 11:06 AM.
Re: My calculator program keeps crashing at the end
Originally Posted by GCDEF
If you're looking for opinions, I'd suggest a separate function that gets the numbers, and passing those numbers to the add, multiply functions. Any time you're doing the same thing repeatedly, you should be using a function that you call.
Why do you have calls like
else
multiplyA();
in all operation functions?
You know the operation type in whateva. Just call the appropriate function after enterB.
I started learning like two days ago... It was the only way I could think of this working. And so it can respond with "Nice, what are you (adding/subtracting/multiplying/dividing) 5 by?"
Re: My calculator program keeps crashing at the end
Originally Posted by Azomb
I started learning like two days ago... It was the only way I could think of this working. And so it can respond with "Nice, what are you (adding/subtracting/multiplying/dividing) 5 by?"
Following your design, you could have a GetAandC() function. After you find out which operation the user wants, set up an if or switch statement like
Also, you really need to forget you've even heard of the goto statment. NO respectable C++ programmer would use it. Learn how to use while and for loops to repeat operations.
Also, when posting, please use CODE tags to preserve formatting.
Also, you really need to forget you've even heard of the goto statment. NO respectable C++ programmer would use it. Learn how to use while and for loops to repeat operations.
Also, when posting, please use CODE tags to preserve formatting.
It works and makes it neater. Thank you~
This program gave me the biggest headache last night, so thank you very much for the help
Bookmarks