Thread: counter problem

  1. #1
    Unregistered
    Guest

    counter problem

    hello

    i write a program like this:#include <iostream.h>
    #include <iomanip.h>
    int main ()
    {
    int score,
    totalscore,
    counter,
    execute;
    double avgscore;
    execute = 999;
    counter = 0;
    totalscore = 0;
    cout << "Intruction: \nEnter score range from 0 - 100 \nAfter that enter 999 for average score calculation " << endl;

    cout << "Now enter a score: " ;
    cin >> score;
    do
    {
    if ( score < 0 || score > 100 )
    cout << "Error, input again " << endl;
    cout << "Enter a score: " ;
    cin >> score;

    }while ( score < 0 || score > 100 );


    while ( score != execute )
    {

    totalscore = totalscore + score;
    counter++;
    cout << "Enter next score: ";
    cin >> score;

    avgscore = totalscore/counter;
    cout << "The average score is: " << avgscore << endl;

    }


    return 0;
    }



    // the problem is when enter score : 80, 90, 105, 70, 999
    valid score should be: 80, 90, 70
    valid count should be: 3
    its good to display error at 105, however the "counter" count it as an entry, so the total score devide more than valid counter, the average score will be wrong.

    any one can help to fix this? I try too many way, but won't work
    hopefully to solve it here.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try this.
    Code:
    #include <iostream.h> 
    #include <iomanip.h> 
    int main () 
    { 
    int score, 
    totalscore, 
    counter, 
    execute; 
    double avgscore; 
    execute = 999; 
    counter = 0; 
    totalscore = 0; 
    cout << "Intruction: \nEnter score range from 0 - 100 \nAfter that enter 999 for average score calculation " << endl; 
    
    cout << "Now enter a score: " ; 
    cin >> score; 
    do {
       while ( score < 0 || score > 100 )
       { 
          cout << "Error, input again " << endl; 
          cout << "Enter a score: " ; 
          cin >> score; 
       }; 
    
       totalscore += score; 
       counter++; 
       cout << "Enter next score: "; 
       cin >> score; 
    
    } while ( score != execute );
    
    avgscore = totalscore/counter; 
    cout << "The average score is: " << avgscore << endl; 
    
    
    return 0; 
    }

  3. #3
    Unregistered
    Guest
    thank you buddy

    it works!!!!!!!



    I really mess up side down right?

    thank you again swoopy

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >I really mess up side down right?
    Exactly. Of course often there's several different ways to get the job accomplished.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 03:36 PM
  2. problem in reading alphabets from file
    By gemini_shooter in forum C Programming
    Replies: 6
    Last Post: 03-09-2005, 01:49 PM
  3. String input problem, gets
    By willc0de4food in forum C Programming
    Replies: 13
    Last Post: 03-05-2005, 02:05 PM
  4. linked list problem
    By kzar in forum C Programming
    Replies: 8
    Last Post: 02-05-2005, 04:16 PM
  5. how to obtain first character of every other word
    By archie in forum C++ Programming
    Replies: 8
    Last Post: 02-18-2002, 01:58 PM