What do I need to gather all lines of numbers say 88 on one line, and 92 on the second and so on?

I have:
ifstream theFile("grades.txt");

theFile >> sc1 >> sc2 >> sc3;// this gives me each number, but I need to be able to read them all then find the avg. Do I have to list each number as a separate input from file(ie, sc1, sc2)

Do you have to calculate the averages line by line? or only once for all the numbers in the file?

I need to read all scores in a file then
I am to:
The average score is simply the sum of all of the scores in the file, divided by the number of scores in the file. To find the average we just need to add each score as it is read in. Keep track of the number of scores to use as the divisor when finding the average.
To find the largest score, do the following:

  1. Define a variable, MAX to hold the highest score.
  2. Read in the first score.
  3. Test the state of the stream to make certain that you have read valid data.
  4. Store the value you just read into max.
  5. Read in the next score.
  6. Test the state of the stream to make certain that you have read valid data.
  7. See if the score that you just read in is greater than max. If it is, set max equal to this value.
  8. If you have not reached the end of the file, go back to step 5.

Not sure how to read in everything then assign MAX :(.

You can use a loop to go through all the numbers in the file.
something like this.

int number;
int score = 0;
int count = 0;
while ( theFile >> number )
{
     score += number;
     count++;
}
average = score / count;

figure out how to find max yourself :).

Hmm maybe a push in the right direction? Do I need a 2nd loop or just something like:
MAX = (score > number) ?

Okay I have this but its not working:

int max = 0;
        int min = 0;
        score += number;
        count++;
        if (score > max)
            max = score;
        if (score < min)
            min = score;

What am i missing?

int max = 0; // If negative numbers are allowed set this to the first number you read (i.e. when count == 0 )
        int min = 0; // Set this to the first number you read(i.e. when count == 0 ). Otherwise you may always get 0 as the min. 
        score += number;
        count++;
        if (number> max)
            max = number;
        if (number< min)
            min = number;

All that should be in the while loop? I am getting errors now. I need to cout the max, min , and avg. What I have:

cout << "\nThe values were " << "Avg: " << average << "High: " << max  << "Low: " << min  << endl;

Declare max , min , count , score and avg out of the loop. Set the values for those inside the loop. After that output the values after exiting the loop.

Okay almost got it, but having trouble with that zero showing up for min still? i know to set count == 0 but not sure what else

post code.

while (theFile >> number )
    {    
        for (count == 0; count < 100; count++)
        score += number;

        //count++;
        if (number > max)
            max = number;
        if (number < min)
            min = number;

Hmm, also, my executable doesn't stay open when i run it. Runs fine in my compiler, but won't stay up when running alone.

while (theFile >> number )
    {    
        for (count == 0; count < 100; count++)
        score += number;
        
        //count++;
        if (number > max)
            max = number;
        if (number < min)
            min = number;

I mean the whole code.

Oh sorry.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;


int main( )
{
    
   // Open the file
   ifstream theFile("grades.txt");
   
       if (theFile.good( ) )
   {
    int number;
    int average;
    int score = 0;
    int count = 0;
    int max = 0;
    int min = 0;
    while (theFile >> number )
    {    
        
        score += number;
        count++;
        if (number > max)
            max = number;
        if (number < min)
            min = number;
    }
    
    
    average = score / count;
    
  // print the results
       cout << "The values were " << " Avg: " << average << " High: " << max  << " Low: " << min  << endl;
        
         
      
   }
   else
   {
       cout << "\nCould not open the file...";
    
   }
   theFile.close();
   system("PAUSE");
   return 0;
}
#include <iostream>
#include <fstream>
// #include <string> you are not using strings so no need for this.
using namespace std;


int main( )
{
    
   // Open the file
   ifstream theFile("grades.txt");
   
       if (theFile.good( ) )
   {
    int number = 0; // always initialize the variables
    double average = 0.0 ; // always initialize the variables
    int score = 0;
    int count = 0;
    int max = 0;
    int min = 0;
    while (theFile >> number )
    {    
         if ( count == 0 )
        {
              max = number;
              min = number;
        }
        else
        {
              if (number > max)
                   max = number;
              if (number < min)
                   min = number;
        }
         score += number;
        count++;
    }
    
    
    if ( count ) // just in case that there was nothing in the file
             average = score / count;
    
  // print the results
       cout << "The values were " << " Avg: " << average << " High: " << max  << " Low: " << min  << endl;
        
         
      
   }
   else
   {
       cout << "\nCould not open the file...\n";
    
   }
   theFile.close();
   // system("PAUSE");// bad practice. use something else like 
   cout << "press enter to continue...";
   cin.get();
    return 0;
}

Thank you!!!!!!!!!!! Much appreciated! :D

What makes the program stay open, the cin.get()?

cin.get() in case of c++ and getchar() for C

sir wolfpack!! please guide me in learning c++ because I am bigneer
I have sent enough msgs to others still no help sound from any one now I am connecting to you please help me sir..

How about we not post to threads that are like 3 years old?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.