Calculator problem.

This is a discussion on Calculator problem. within the C++ Programming forums, part of the General Programming Boards category; Can't get this code here to work tried everything I can think of and looked at the FAQ. Should add ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    27

    Calculator problem.

    Can't get this code here to work tried everything I can think of and looked at the FAQ. Should add up multiple variables.
    Code:
    #include <iostream>
    int main(void)
    {
        int sum = 0, value;
        while (std::cin >> value)
        sum += value;
        std::cout << "Sum is: " << sum << std::endl;
        std::cin.get();
        std::cin.ignore();
        return 0;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,540
    So what actually happens?

    Does the loop never exit?

    If the loop does exit, does the window disappear too quickly?

    Which OS/Compiler are you using?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jul 2006
    Posts
    27
    Using latest Dev compiler, the window wont stay open and it wont add the numbers correctly.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,540
    Well if you add some braces to your loop, to make it a proper compound statement, and also output the current total, does it work up until the end?
    This is called debugging - learn how to do it.

    How do you exit the loop?
    - press ctrl-z
    - type in some random characters to cause the numeric conversion to fail?

    Perhaps your ignore() and get() are the wrong way round?
    How many chars does ignore() ignore by default?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Jul 2006
    Posts
    27
    When i type in the numbers it jujst gives me teh smallest number i entered as the sum =P
    As you can see im extremely new to programming, and debugging is or was rather far from my mind... ignore() and get() seem fine, i tried them the other way and it didnt change anything.

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,702
    Using cin in a loop is really funky if you're not careful. You really should consider what this FAQ says: Converting a string to an int. Writing your program after you understand that is much easier for you and anyone using your program, I think.
    Last edited by whiteflags; 08-01-2006 at 01:51 AM.

  7. #7
    Registered User
    Join Date
    Jul 2006
    Posts
    27
    Hmm that FAQ is somewhat advanced for me, sadly lol... However its a step in the right direction, thank you.

  8. #8
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    Maybe this:
    Code:
    #include <iostream>
    int main(void)
    {
        int sum = 0, value;
        while (std::cin >> value)
        sum += value;
        std::cin.clear();
        std::cin.ignore(300,'\n');
        std::cout << "Sum is: " << sum << std::endl;
        std::cin.get();
        return 0;
    }
    Last edited by siavoshkc; 08-01-2006 at 02:19 AM.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  9. #9
    Registered User
    Join Date
    Jul 2006
    Posts
    27
    Quote Originally Posted by Mrzach3590
    Can't get this code here to work tried everything I can think of and looked at the FAQ. Should add up multiple variables.
    Code:
    #include <iostream>
    int main(void)
    {
        int sum = 0, value;
        while (std::cin >> value)
        sum += value;
        std::cout << "Sum is: " << sum << std::endl;
        std::cin.get();
        std::cin.ignore();
        return 0;
    }
    Ok, theres the original messed up code, and heres the code that finally worked...
    Code:
    #include <iostream>
    using namespace std;
    int main(void)
    {
        int sum = 0, value;
        while (std::cin >> value) {
        sum += value;
    
        std::cout << "Sum is: " << sum << std::endl;
    }
        std::cin.ignore();
        std::cin.get();
        return 0;
    }

  10. #10
    Registered User
    Join Date
    May 2006
    Posts
    903
    I'd just like to point out that debugging is never too advanced for you. You need to learn it at the very beginning of your learning. That, and searching information on the internet. Too often you see threads of people asking, for example, how to get the handle to a window that's not ours yet a simple search on MSDN would return FindWindow() which is the obvious answer. My two cents.

  11. #11
    Registered User
    Join Date
    Jul 2006
    Posts
    27
    I didnt say it was to advanced, just that i wasnt thinking about it lol. Altho I might as well go find out more about it.

  12. #12
    Registered User
    Join Date
    Jul 2006
    Posts
    27
    Compiles and runs, I want it to produce the the numbers inbetween the numbers i enter... I enter 10 1 it gives me 10 I enter 1 10 it gives me 2 9... I want the anwaser to be 2 9 or 9 2 not 10 1.... how can i fix this?
    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
        std::cout<<"Input 2 numbers: ";
        int x, y;
      std::cin >> x >> y;
         if ( x <= y)
        {
              x < y, y <= x, x++;
              x > y, x >= y, y--;
              } 
              std::cout <<"Range of numbers is: "<< x << " to " << y;
             std::cin.ignore();
              std::cin.get();
              
              return 0;
              }

  13. #13
    C++No0b!!!
    Join Date
    Jul 2005
    Location
    penn
    Posts
    66
    there really isnt any need for the std::
    i thought using namespace std fixed this but maybe you need
    #include <conio.h>

  14. #14
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,540
    > x < y, y <= x, x++;
    What's this supposed to do?

    > I want the anwaser to be 2 9 or 9 2 not 10 1.... how can i fix this?
    With an if statement like you've already got.
    Just refine the contents of the if part and the else part (as yet unwritten).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  15. #15
    Registered User
    Join Date
    Jul 2006
    Posts
    27
    Alright ill give it a try

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21