Error Help!!!

This is a discussion on Error Help!!! within the C++ Programming forums, part of the General Programming Boards category; I have an error in this code and can't figure it out. Can you help? Code: #include <iostream> int main ...

  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
    21,452
    Use a for loop instead of a while loop that looks like you were trying to use a do while loop or something.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,452
    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; ).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,452
    Do you know the syntax for a for loop?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,452
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,452
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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.

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