Thread: Error Help!!!

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    83

    Error Help!!!

    I have an error in this code and can't figure it out. Can you help?


    Code:
    #include <iostream>
    
    int main () {
      int counter, numEntered, numValues;
      double sum;
    
       numValues = 0;
        sum = 0.0;
        counter = 0;
       
     while (numValues < 5) 
        numValues++;
          
        cout << "Enter value " << numValues << ": ";
        cin >> numEntered;
       
     do counter++;
     
        sum += numEntered;
        
        cout << sum << " The sum is: " << endl;
          
     return 0;
    }
    Last edited by walkonwater; 03-08-2009 at 11:56 AM.

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    83
    I am trying to make it look like this:

    Enter number 1: __
    Enter number 2: __
    Enter number 3: __
    Enter number 4: __
    Enter number 5: __

    The sum is ___.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Use a for loop instead of a while loop that looks like you were trying to use a do while loop or something.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    83
    I'm a beginner. Which one would I change? Can you show me how?

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    83
    Well when I run this program with a while loop, it says partse error before '+='

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Actually, I suggest that you rewrite from scratch with a for loop. You only need three variables: one to hold the sum, one to hold the user input, and another to count from 1 to 5. The variables for sum and user input probably should be of the same type (e.g., double), while the counter should be of an integer type.

    Additionally, you should fully qualify cin, cout and endl as std::cin, std::cout and std::endl respectively, unless you use a using directive (using namespace std; ).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    83
    well could you help me with that? I am new and don't know how to do this very well? I am sorry about this. How would I write that?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Do you know the syntax for a for loop?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    83
    I kind of know it. Could you help me rewrite this program?

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by walkonwater
    I don't know anything really.
    Well, you obviously know enough to get some parts of a program correct

    Quote Originally Posted by walkonwater
    Could you help me rewrite the program?
    Yes, but I will not give you the answer outright, though if you work hard enough, I might be convinced to show you a "model answer".

    Anyway, to loop from 1 to n, you might write this for loop:
    Code:
    for (int i = 1; i <= n; ++i) {
        // ...
    }
    I would expect your program to be along these lines:
    Code:
    #include <iostream>
    
    int main() {
        using namespace std;
    
        double sum = 0.0;
    
        // Insert for loop to request and read in user input and compute the sum
        // ...
    
        cout << "\nThe sum is: " << sum << endl;
    
        return 0;
    }
    Notice my use of indentation.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Feb 2009
    Posts
    83
    I'm have been working on this for 3 hours and don't have it yet. I used your stuff but I need your help please??????
    Last edited by walkonwater; 03-08-2009 at 12:51 PM.

  12. #12
    Registered User
    Join Date
    Feb 2009
    Posts
    83
    I also don't understand what the integers in the loop are for?

  13. #13
    Registered User
    Join Date
    Feb 2009
    Posts
    83
    I think I am more confused now than I was earlier.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by walkonwater
    I used your stuff but I need your help please?
    Uh, what qualifies as helping you?

    Quote Originally Posted by walkonwater
    I also don't understand what the integers in the loop are for?
    I gave you the syntax of a for loop that, with some modification, can be used to solve the problem. Have you only learnt about while loops?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Feb 2009
    Posts
    83
    make it four hours.

Popular pages Recent additions subscribe to a feed