C++ - loops

This is a discussion on C++ - loops within the C++ Programming forums, part of the General Programming Boards category; I cannot understand this exercise: "Write a program that computes a running sum of inputs from the user, terminating when ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    2

    C++ - loops

    I cannot understand this exercise:

    "Write a program that computes a running sum of inputs from the user, terminating when the user gives an input value of 0"

    I do not understand what I'm supposed to do here.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    2,241
    It sounds like the general algorithm is supposed to be like this:

    1. Read number from the user
    2. If that number is zero, exit. Otherwise, add it to a sum variable and go back to #1.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    2
    Ok, I will try it.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    10
    This should get you started in the right direction
    Code:
    #include <iostream>
    
    using namespace std;
    
    
    
    int main()
    {
        int num = 0;
        int num2 = 0;
        int total = 0;
        do
        {
        cout << "Enter a number: " << endl;
        cin >> num;
        cout << "Enter a number: " << endl;
        cin >> num2;
        total += num + num2;
        }
        while(num || num2 != 0);
    
    cout << total << endl;
    
    return 0;
    
    
    
    }

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    2,241
    Code:
    while(num || num2 != 0);
    What are you expecting this line to do?

    And this could have been done with a single input variable.

  6. #6
    Registered User
    Join Date
    Dec 2012
    Posts
    2
    Quote Originally Posted by Matticus View Post
    And this could have been done with a single input variable.
    True. Remove the second variable, it achieves the same result with only one variable.
    Code:
    int main()
    {
        int num = 0;
        int total = 0;
        
        do{
            cout << "Enter a number: " << endl;
            cin >> num;
            
            total+=num;
        
        }while(num != 0);
        
        cout << total << endl;
    
        system("pause"); 
        return 0;
    }

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    10
    Quote Originally Posted by Matticus View Post
    Code:
    while(num || num2 != 0);
    What are you expecting this line to do?

    And this could have been done with a single input variable.
    Note I said "This should get you started in the right direction" which implies I put it there knowingly, we could argue for hours how this program could be done better and more efficiently but that would be counter productive. But I accept your input yes, it could be done much better.

  8. #8
    Registered User
    Join Date
    Nov 2011
    Posts
    10
    Quote Originally Posted by aejr120 View Post
    True. Remove the second variable, it achieves the same result with only one variable.
    Code:
    int main()
    {
        int num = 0;
        int total = 0;
        
        do{
            cout << "Enter a number: " << endl;
            cin >> num;
            
            total+=num;
        
        }while(num != 0);
        
        cout << total << endl;
    
        system("pause"); 
        return 0;
    }
    Just a little tip adding system("pause") will increase your file size substantially, you can replace it with something like this
    Code:
    cout << "Press enter to exit: "
    char x;
    cin x;
    Side notence yes I know I could use better code than this or read for a specific key, but this will suffice.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,450
    The problem with system is not that it increases size, but that it is Evil™.
    Here's why.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    2,241
    Quote Originally Posted by Dynamic View Post
    Note I said "This should get you started in the right direction" which implies I put it there knowingly, we could argue for hours how this program could be done better and more efficiently but that would be counter productive. But I accept your input yes, it could be done much better.
    You didn't answer my question, though.

    And how is this the "right direction"? This specific program might get lucky, but if we change the sentinel value to, say, -1, you have at least two major issues.

    Your sentinel value gets factored into the sum, and the syntax of your "while()" loop is wrong.

  11. #11
    Registered User
    Join Date
    Nov 2011
    Posts
    10
    Quote Originally Posted by Matticus View Post
    You didn't answer my question, though.

    And how is this the "right direction"? This specific program might get lucky, but if we change the sentinel value to, say, -1, you have at least two major issues.

    Your sentinel value gets factored into the sum, and the syntax of your "while()" loop is wrong.
    Hence saying right direction, or I am suppose to give this person a complete working solution for their college assignment?

  12. #12
    Registered User
    Join Date
    Jun 2011
    Posts
    2,241
    IMHO, examples with incorrect syntax is not the right direction. But that's just my opinion - you're free to disagree.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. converting for loops to while loops
    By nabhatt in forum C Programming
    Replies: 3
    Last Post: 02-16-2012, 08:45 PM
  2. Replies: 3
    Last Post: 06-01-2011, 04:19 PM
  3. loops, menu loops
    By gloworm in forum C Programming
    Replies: 17
    Last Post: 04-12-2010, 07:59 PM
  4. Help on loops
    By GAMEPROJO3 in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2005, 03:46 AM
  5. need a lil help with some loops
    By blindleaf in forum C Programming
    Replies: 7
    Last Post: 03-16-2003, 05:32 PM

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