My calculator program keeps crashing at the end
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: My calculator program keeps crashing at the end

  1. #1
    Join Date
    Jan 2013
    Posts
    6

    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;
    }

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,121

    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 08:35 PM.

  3. #3
    Join Date
    Jan 2013
    Posts
    6

    Re: My calculator program keeps crashing at the end

    Quote Originally Posted by GCDEF View Post
    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....

  4. #4
    Join Date
    Jan 2013
    Posts
    6

    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 10:52 AM.

  5. #5
    Join Date
    Jan 2013
    Posts
    6

    Re: My calculator program keeps crashing at the end

    There are some unnecessarily used functions but those were meant for practice.

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,121

    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.

  7. #7
    Join Date
    Jan 2013
    Posts
    6

    Re: My calculator program keeps crashing at the end

    Quote Originally Posted by GCDEF View Post
    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?"

  8. #8
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,121

    Re: My calculator program keeps crashing at the end

    Quote Originally Posted by Azomb View Post
    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.

  9. #9
    Join Date
    Jan 2013
    Posts
    6

    Re: My calculator program keeps crashing at the end

    Quote Originally Posted by GCDEF View Post
    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center