Thread: user input loop

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    3

    user input loop

    hey im havin a hell of a time doin sumthing that shud be easy ... ok the assignment is to prompt the user to input the number of grades they want to enter ... then im spose to get the average of those grades and output it all within a for loop .. . can u guys help me out?

  2. #2
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Well, why don't you try something and then post what you did and then we will help. Becareful, posting questions like this with no work will get you flammed.

    Hint : use a for loop, I am sure you have gone over it in class.

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    3

    ight heres wut i have done (in my main)

    int j;
    int loop_limit;
    int score;
    double average;

    cout << "How many scores do you want to enter? ";
    cin >> loop_limit;

    for (j = 1; j <= loop_limit; average = score / loop_limit, ++j)
    {cout << "Enter a score: ";
    cin >> score;
    average = score / loop_limit;
    }

    cout << average << endl;

  4. #4
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Ok, before anyone flames please use code tags. Simply place [*code] the begginning and [*/code] (minus the asterisk)at the end of the code you submit to make it more readable.

    Now for your problem:

    1. use variable names that are helpful, j is not helpful.
    2. no one is going to do the assignment for you so I will point you in the right direction.

    Code:
    int main(void) {
    
              int currentScore, numberScores, score, total, average;
              cout << "Enter how many scores you wish to average:";
              cin >> numberScores;
    
              for(currentScore = 0; currentScore < numberScores; currentScore++) {
                            
                       //get the scores and add them to total
               }
    
               //calculate and display average
    
               return 0;
    }
    That should get you off to a good start. Happy coding

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    3
    hey one more thing .. i just dont understand how to take in each score as being separate and add them together

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    well, you could add them right there in the loop to a 'total' (initialize to zero). you can use an array, too. also, you'll want to calculate the average *after* the loop terminates.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Code:
     int score = 0;
     int totalscore = 0;
    
    ...
    
      cin >> score;
      totalscore += score;
    Keep a running total of the scores, and divide that by the number of scores entered. Inside the loop this will be j, after the loop is done this will be loop_limit. If you aren't understanding why that works, think about it, then ask.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    i, j etc. are perfectly helpful names. Every programmer knows that a variable called i (or j, k, m, ... if i already exists) is a loop index.

    However, in C++ loop indices should be declared inside the loop:
    for(int i = 0; i < max; ++i)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540

    However, in C++ loop indices should be declared inside the loop:
    I don't even need to say anything.

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Huh?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    In the original code listing "int j" was just declared and then later on it was used in the loop. Thus the variable was viable for the entire function. I prefer not to use cryptic loop index names if their scope extends beyond the loop. Just a style point for me though.

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Yes, the code was very C-style in that regard. In C, the variable HAS to be declared at the start of the function, yet programmers didn't want to use long names for the indices, so they just assumed (mostly correctly) that anyone seeing them would know that they are indices.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. for loop with sort issues
    By redmondtab in forum C Programming
    Replies: 10
    Last Post: 10-09-2006, 10:36 AM
  2. vectors and user input
    By Chaplin27 in forum C++ Programming
    Replies: 6
    Last Post: 01-17-2005, 10:23 AM
  3. comparing user input
    By lambs4 in forum C Programming
    Replies: 5
    Last Post: 12-15-2002, 10:28 AM
  4. Format User Input When using strings
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 03-08-2002, 03:59 AM
  5. Beginner question- user input termination
    By westm2000 in forum C Programming
    Replies: 3
    Last Post: 12-02-2001, 02:48 PM