Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Program Which: Takes numbers from a user, and sum's them.

Program Which: Takes numbers from a user, and sum's them.

Scheduled Pinned Locked Moved C / C++ / MFC
c++htmlcom
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rbwest86
    wrote on last edited by
    #1

    Hello all, I am working on my homework for my C++ programming class and I am hitting a brick wall. Everywhere I look online does not seem to have any information in which I can apply to my program. I am supposed to use a "while" loop which I am, and thats working. The part which is not working is the users input should add it to the previous entry, and after the user types "0" it will display the sum on the screen. I have looked at the following references to try and make sense of things: http://www.cplusplus.com/reference/std/numeric/partial_sum/[^] http://www.cplusplus.com/reference/std/numeric/accumulate/[^] http://frank.mtsu.edu/~csci117/manual/lab7/lab7.html[^] I am trying very hard to understand this, but I can not find a good online reference of the libraries. www.cplusplus.com is very good but dosent have the info I need, or more than likely I can not find it. But here is the code to which I am trying to correct:

    #include <iostream>
    #include <string>

    using namespace std;

    int main()
    {

    // Global Variables
    int userinput;
    int sum = 0;
    sum = sum + userinput;

    // Start While loop
    while (userinput != 0)
    {
    // Prompt User for input
    cout << "Please enter a number. When you are finished, type 0 and press Enter." ;
    cin >> ++userinput ;

    }
    cout << "The sum is:" << sum << endl;
    return 0;
    }

    Thank's to all in advance for pointing me in the correct direction so I can figure this out. I should have put this at the top: I DO NOT WANT THE ANSWER, I WOULD PREFER THE REFERENCE TO FIND MY OWN.

    L R 2 Replies Last reply
    0
    • R rbwest86

      Hello all, I am working on my homework for my C++ programming class and I am hitting a brick wall. Everywhere I look online does not seem to have any information in which I can apply to my program. I am supposed to use a "while" loop which I am, and thats working. The part which is not working is the users input should add it to the previous entry, and after the user types "0" it will display the sum on the screen. I have looked at the following references to try and make sense of things: http://www.cplusplus.com/reference/std/numeric/partial_sum/[^] http://www.cplusplus.com/reference/std/numeric/accumulate/[^] http://frank.mtsu.edu/~csci117/manual/lab7/lab7.html[^] I am trying very hard to understand this, but I can not find a good online reference of the libraries. www.cplusplus.com is very good but dosent have the info I need, or more than likely I can not find it. But here is the code to which I am trying to correct:

      #include <iostream>
      #include <string>

      using namespace std;

      int main()
      {

      // Global Variables
      int userinput;
      int sum = 0;
      sum = sum + userinput;

      // Start While loop
      while (userinput != 0)
      {
      // Prompt User for input
      cout << "Please enter a number. When you are finished, type 0 and press Enter." ;
      cin >> ++userinput ;

      }
      cout << "The sum is:" << sum << endl;
      return 0;
      }

      Thank's to all in advance for pointing me in the correct direction so I can figure this out. I should have put this at the top: I DO NOT WANT THE ANSWER, I WOULD PREFER THE REFERENCE TO FIND MY OWN.

      L Offline
      L Offline
      loyal ginger
      wrote on last edited by
      #2

      Outside the loop you should initialize the variable "userinput". The statement

      sum = sum + userinput

      before the loop should be removed. This statement should appear somewhere inside the loop. Also, for this you can use the += operator instead. The ++userinput inside the loop should just be userinput without the ++ operator. The variables "userinput" and "sum" are not "Global Variables". They are local to the function "main". You need to work out the details.

      R 1 Reply Last reply
      0
      • L loyal ginger

        Outside the loop you should initialize the variable "userinput". The statement

        sum = sum + userinput

        before the loop should be removed. This statement should appear somewhere inside the loop. Also, for this you can use the += operator instead. The ++userinput inside the loop should just be userinput without the ++ operator. The variables "userinput" and "sum" are not "Global Variables". They are local to the function "main". You need to work out the details.

        R Offline
        R Offline
        rbwest86
        wrote on last edited by
        #3

        Thank you very much. I got it working now. I read over this while I was at dinner with the wife and figured it out at the dinner table! here is the working code:

        #include

        using namespace std;

        int main()
        {

        // Local Variables
        int userinput;
        int sum = 0;

        // Start While loop
        while (userinput != 0)
        {
        // Prompt User for input
        cout << "Please enter a number. When you are finished, type 0 and press Enter." ;
        cin >> userinput ;
        sum = sum + userinput;

        }
        cout << "The sum is:" << sum << endl;
        return 0;
        }

        L 1 Reply Last reply
        0
        • R rbwest86

          Hello all, I am working on my homework for my C++ programming class and I am hitting a brick wall. Everywhere I look online does not seem to have any information in which I can apply to my program. I am supposed to use a "while" loop which I am, and thats working. The part which is not working is the users input should add it to the previous entry, and after the user types "0" it will display the sum on the screen. I have looked at the following references to try and make sense of things: http://www.cplusplus.com/reference/std/numeric/partial_sum/[^] http://www.cplusplus.com/reference/std/numeric/accumulate/[^] http://frank.mtsu.edu/~csci117/manual/lab7/lab7.html[^] I am trying very hard to understand this, but I can not find a good online reference of the libraries. www.cplusplus.com is very good but dosent have the info I need, or more than likely I can not find it. But here is the code to which I am trying to correct:

          #include <iostream>
          #include <string>

          using namespace std;

          int main()
          {

          // Global Variables
          int userinput;
          int sum = 0;
          sum = sum + userinput;

          // Start While loop
          while (userinput != 0)
          {
          // Prompt User for input
          cout << "Please enter a number. When you are finished, type 0 and press Enter." ;
          cin >> ++userinput ;

          }
          cout << "The sum is:" << sum << endl;
          return 0;
          }

          Thank's to all in advance for pointing me in the correct direction so I can figure this out. I should have put this at the top: I DO NOT WANT THE ANSWER, I WOULD PREFER THE REFERENCE TO FIND MY OWN.

          R Offline
          R Offline
          rbwest86
          wrote on last edited by
          #4

          Now that the program adds the numbers and provides the sum at the end, I need to provide a means of displaying how many times a user input something. The example output would be: Sum = 134 Total numbers of inputs = 8 So I have been looking to find something on counters. I have looked at multiple web sites yet again and can not find anything. I have fixed my previous code and it is listed below. Once again, I DO NOT WANT THE ANSWER, I WANT THE REFERENCE SO I CAN LOOK IT UP MYSELF. Being pointed in the right direction helps me most. Thank you all again.

          #include <iostream>

          using namespace std;

          int main()
          {

          // Local Variables
          int userinput;
          int sum = 0;
          int count = 0 ;

          // Welcome Message
          cout << "Roberts Number program v1" << endl;
          cout << endl;
          cout << endl;
          cout << "When you are finished, enter '0'" << endl;
          cout << endl;

          // Start While loop
          while (userinput != 0)
          {
          // Prompt User for input
          cout << "Input a number, then press enter. " ;
          cin >> userinput ;
          sum += userinput;

          }
          cout << "The sum is:" << sum << endl;
          return 0;
          }

          L 1 Reply Last reply
          0
          • R rbwest86

            Now that the program adds the numbers and provides the sum at the end, I need to provide a means of displaying how many times a user input something. The example output would be: Sum = 134 Total numbers of inputs = 8 So I have been looking to find something on counters. I have looked at multiple web sites yet again and can not find anything. I have fixed my previous code and it is listed below. Once again, I DO NOT WANT THE ANSWER, I WANT THE REFERENCE SO I CAN LOOK IT UP MYSELF. Being pointed in the right direction helps me most. Thank you all again.

            #include <iostream>

            using namespace std;

            int main()
            {

            // Local Variables
            int userinput;
            int sum = 0;
            int count = 0 ;

            // Welcome Message
            cout << "Roberts Number program v1" << endl;
            cout << endl;
            cout << endl;
            cout << "When you are finished, enter '0'" << endl;
            cout << endl;

            // Start While loop
            while (userinput != 0)
            {
            // Prompt User for input
            cout << "Input a number, then press enter. " ;
            cin >> userinput ;
            sum += userinput;

            }
            cout << "The sum is:" << sum << endl;
            return 0;
            }

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            rbwest86 wrote:

            counters

            A counter is just a variable that holds a number. So per your requirement I will reword it to provide the hint that you need... Each time that the user enters a number, increase the counter, then when the user is done entering numbers, output the number of times that the user entered a value.

            Why is common sense not common? Never argue with an idiot. They will drag you down to their level where they are an expert. Sometimes it takes a lot of work to be lazy Individuality is fine, as long as we do it together - F. Burns Help humanity, join the CodeProject grid computing team here

            1 Reply Last reply
            0
            • R rbwest86

              Thank you very much. I got it working now. I read over this while I was at dinner with the wife and figured it out at the dinner table! here is the working code:

              #include

              using namespace std;

              int main()
              {

              // Local Variables
              int userinput;
              int sum = 0;

              // Start While loop
              while (userinput != 0)
              {
              // Prompt User for input
              cout << "Please enter a number. When you are finished, type 0 and press Enter." ;
              cin >> userinput ;
              sum = sum + userinput;

              }
              cout << "The sum is:" << sum << endl;
              return 0;
              }

              L Offline
              L Offline
              loyal ginger
              wrote on last edited by
              #6

              Even though this program works, you are relying on the variable userinput having an initial non-zero value. The compiler actually does not always guarantee that. To make the program robust, I suggest you take control on the values of variables and not rely on some random factors. Good luck and happy programming!

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups