-
January 20th, 2013, 07:53 PM
#1
My calculator program keeps crashing at the end
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();
return 0;
}
-
January 20th, 2013, 09:27 PM
#2
Re: My calculator program keeps crashing at the end
Doesn't compile for me. You have functions declared as int and string that don't return a value. If I change them to void, nothing crashes.
What does your debugger show?
Last edited by GCDEF; January 20th, 2013 at 09:35 PM.
-
January 20th, 2013, 09:48 PM
#3
Re: My calculator program keeps crashing at the end
Originally Posted by GCDEF
Doesn't compile for me. You have functions declared as int and string that don't return a value. If I change them to void, nothing crashes.
What does your debugger show?
Woah you did it! Thank you ^_^
I'm going to need to work declaring functions....
-
January 20th, 2013, 09:51 PM
#4
Re: My calculator program keeps crashing at the end
Here's my final source code.
I added loop functionality
#include <iostream>
#include <string>
using namespace std;
class CALCULATIONS{
public:
void whateva(){
cout << "Hi, I'm a calculator ^_^\n\n\nEnter a number and press enter please... \n\n";
yesloop:
enterA();
cout << "\n" << "Cool, now what do you want to do with ";
showA();
cout << "?\n\n" << "(+) Add ?\n" << "(-) Subtract ?\n" << "(*) Multiply ?\n" << "(/) Divide ?\n\n";
enterB();
addA();
cout << "\n" << "Wanna do more math? Y/N? \n\n";
yesORno();
if(z=="y"){
goto yesloop;
}
}
void enterA(){
cin >> a;
}
void enterB(){
cin >> b;
}
void showA(){
cout << a;
}
void addA(){
if(b=="+"){
cout << "\n" << "Nice, what are you adding " << a << " by?\n\n";
cin >> c;
cout << "\n" << "I think it's " << a+c << "\n\n";
}
else
subtractA();
}
void subtractA(){
if(b=="-"){
cout << "\n" << "Nice, what are you subtracting " << a << " by?\n\n";
cin >> c;
cout << "\n" << "I think it's " << a-c << "\n\n";
}
else
multiplyA();
}
void multiplyA(){
if(b=="*"){
cout << "\n" << "Nice, what are you multiplying " << a << " by?\n\n";
cin >> c;
cout << "\n" << "I think it's " << a*c << "\n\n";
}
else
divideA();
}
void divideA(){
if(b=="/"){
cout << "\n" << "Nice, what are you dividing " << a << " by?\n\n";
cin >> c;
cout << "\n" << "I think it's " << a/c << "\n\n";
}
else
addA();
}
void yesORno(){
cin >> z;
if(z=="y"){
yes();
}
if(z=="n"){
no();
}
}
void yes(){
cout << "\n" << "Enter a number please... \n\n";
}
void no(){
cout << "\n" << "See ya (^ - ^)/";
}
private:
int a;
string b;
int c;
string z;
};
int main()
{
CALCULATIONS caLc;
caLc.whateva();
return 0;
}
Last edited by Azomb; January 21st, 2013 at 11:52 AM.
-
January 21st, 2013, 11:55 AM
#5
Re: My calculator program keeps crashing at the end
There are some unnecessarily used functions but those were meant for practice.
-
January 21st, 2013, 12:02 PM
#6
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 12:06 PM.
-
January 21st, 2013, 12:47 PM
#7
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?"
-
January 21st, 2013, 12:53 PM
#8
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
Code:
if(b == "*")
Multiply();
else
if(b == "/")
Divide();
...
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.
-
January 21st, 2013, 01:10 PM
#9
Re: My calculator program keeps crashing at the end
Originally Posted by GCDEF
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
Code:
if(b == "*")
Multiply();
else
if(b == "/")
Divide();
...
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
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|