CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jan 2011
    Location
    Orange County, CA
    Posts
    82

    Angry Running total all messed up

    Code:
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    int main()
    {
       system ("TITLE Calculator");
       system ("COLOR 0A");
    
    
       while (1)
        {
            int menu_choice, nums, total;
            char answer, esc;
    
            cout << "                          ::Menu::\n"
                 << "Type the number of the choice you want and press \"Enter\".\n\n"
                 << "1. Addition\n"
                 << "2. Subtraction\n"
                 << "3. Multiplication\n"
                 << "4. Division\n"
                 << "5. Temperature Converter\n"
                 << "6. Exit\n\n"
                 << "Menu Choice: ";
            cin >> menu_choice;
    
    
            if (menu_choice < 1 || menu_choice > 6 )
                {
                    system ("CLS");
                    cout << "          Error\n" << menu_choice << " is not an available choice.\n\n";
                    system ("PAUSE");
                    system ("CLS");
                }
    
    
            else if (menu_choice == 1)
                {
                    while (1)
                        {
                            total = 0;
    
                            system ("CLS");
                            cout << "Enter the numbers (separated by a space) you want added together,\n"
                                 << "when done type press \"Enter\".\n\n"
                                 << "Numbers: ";
                            cin >> nums;
                            total += nums;  // this glitches it up                        
                            
                            if (total > 2147483647 || total < -2147483647)  // Not reading the negative
                                {
                                    system ("CLS");
                                    cout << "                  Error\n"
                                         << "The total of the number(s) you entered are too\n"
                                         << "large for this program to handle.\n\n";
                                    system ("PAUSE");
                                    system ("CLS");
                                    continue;
                                }
    
    
                            cout << "The total is: " << total << ".\n";
                            cout << "\n\nWould you like to add more numbers? (Y/N)\n"
                                 << "Answer: ";
                            cin >> answer;
    
    
                            if (answer == 'y' || answer == 'Y')
                                    {
                                        system ("CLS");
                                        continue;
                                    }
    
                            else if (answer == 'n' || answer == 'N')
                                    {
                                        system ("CLS");
                                        break;
                                    }
                        }
                }
    ____________________________________________________________________________
    Ok, i got bored today, so i decided to make a calculator, this is the first section of a soon to be ongoing project outta boredom. BUT, so far i have two problems it, 1st it wont read the neg. when i try to limit the total, and 2nd the running total doesnt work right, it just glitches up and doesnt work, any help??
    Please note: i know im using system commands, i know they make the program run slower, but its all i know how to use at the moment and i dont think itll matter for a calculator, so please dont bring it up unless that is the cause of the problems, thx.
    Last edited by Marc G; January 11th, 2011 at 03:44 AM. Reason: Added code tags

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Running total all messed up

    You might want to elaborate on what exactly is the problem, e.g., explain how you know "this glitches it up".

    Oh, and please post code in [code][/code] bbcode tags.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Running total all messed up

    Quote Originally Posted by iiSoMeGuY 7x View Post
    so far i have two problems it, 1st it wont read the neg. when i try to limit the total, and 2nd the running total doesnt work right, it just glitches up and doesnt work, any help??
    Did you step through your code with the debugger?
    One error in your code is that you don't check for valid input. You check the range of inputted values, but if a user enters some text, your program has undefined behavior.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Running total all messed up

    You're telling the user to enter a series of numbers separated by a space, and trying to put all that into a single int.

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Running total all messed up

    Quote Originally Posted by iiSoMeGuY 7x View Post
    Ok, i got bored today, so i decided to make a calculator, this is the first section of a soon to be ongoing project outta boredom.
    Just to warn you, if you plan to make this more elaborate, the way you've started the program is not the way to program such a calculator.

    You'll get tripped up if you ever decide to use precedence rules, i.e
    Code:
    5 + 2 * 4 = 13 (not 28).
    Or parentheses, or...

    There are formal ways of writing such a program, but that's for later.

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Jan 2011
    Location
    Orange County, CA
    Posts
    82

    Re: Running total all messed up

    never mind, i just changed it so im not using any += or -= (i was trying something new), and added some more loops, but thanks for trying to help anyways

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Running total all messed up

    Quote Originally Posted by iiSoMeGuY 7x View Post
    i knew about that, i just started on it, im gonna add more stuff later
    Since you are bored (as your first post suggested), I think you would learn a lot more if you allow the user to input any mathematical expression and then you attempt to evaluate it.

    Instead of limiting the user to only add, or only subtract, multiply, etc. have them do anything they want, as long as the expression is legal. Then return the correct answer back to them.

    Something like this:
    Code:
    Enter expression:   2 * 8 + 5
    Your answer is: 21
    
    Enter expression: 3 + 4 * 8
    Your answer is 35
    
    Enter expression: 1 + 1 + 4 * 2 - 3
    Your answer is 7
    That is much closer to a calculator than what you're coding now, and should keep you busy for a while.

    Regards,

    Paul McKenzie

  8. #8
    Join Date
    Jan 2011
    Location
    Orange County, CA
    Posts
    82

    Re: Running total all messed up

    Quote Originally Posted by D_Drmmr View Post
    Did you step through your code with the debugger?
    One error in your code is that you don't check for valid input. You check the range of inputted values, but if a user enters some text, your program has undefined behavior.
    i knew about that, i just started on it, im gonna add more stuff later

  9. #9
    Join Date
    Jan 2011
    Location
    Orange County, CA
    Posts
    82

    Re: Running total all messed up

    Quote Originally Posted by laserlight View Post
    You might want to elaborate on what exactly is the problem, e.g., explain how you know "this glitches it up".
    it wont show you did anything, skip to "Would you like to add more numbers...", or just says that the total is 5 no matter what.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured