So i have this homework assignment and im having a problem figuring out how to get the numbers from this file and finding the agerages. heres what i got so far.

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;

int main ()
{
    // Declarations
    string reply;
    double number;
    double total;
    int count;
    string inputFileName;
    ifstream inputFile;
	string line;

    cout << fixed << showpoint << setprecision(2);
    cout << "Input file name: ";
    getline(cin, inputFileName);
    // Open the input file.
    inputFile.open(inputFileName.c_str());      // Need .c_str() to convert a C++ string to a C-style string
    // Check the file opened successfully.
    if ( ! inputFile.is_open()) {
        cout << "Unable to open input file." << endl;
        cout << "Press enter to continue...";
        getline(cin, reply);
        exit(1);
    }

    count = 0;       // Must initialize the counter to zero
    total = 0;       // Must initialize the accumulator to zero.

	// This section reads and echo's the file one line at a time.
    while (inputFile.peek() != EOF) 
	{
        getline(inputFile, line);
        cout << line << endl;
    }


    // The while statement reads the next token into 'number'
    // It returns a value of 'false' at end-of-file, which terminates the loop.
    while (inputFile >> number) {
        count++;     // Increment the counter
        cout << setw(5) << count << setw(20) << number << endl;
        total += number;
    }

    cout << "The total is: " << setw(11) << total << endl;
    // Be careful not to divide by zero!
    if (count > 0) {
        cout << "The average is: " << setw(9) << total/count << endl;     
    }

    // Close the input file stream
    inputFile.close();
    cout << "Press enter to continue...";
    getline(cin, reply);
    return 0;	
}
Member Avatar for jencas

What is the problem?

>how to sort alphanumeric file to find averages?
I think that's your major problem: see the post title.
No need in file sorting to get an average = sum_total / number_counter ;)
The next (minor) problem: what's alphanumeric file? What's input file contents? If there are numbers only in the file - why alpha and what for getline? If there are digits and letters (alpha means letters) - what's the file format?

here are the file contents, i want to be able to seperate the colmns and be able to find the average of students. the sccore is the last colmn.

Bailey Y 16 68
Harrison N 17 71
Grant Y 20 75
Peterson N 21 69
Hsu Y 20 79
Bowles Y 15 75
Anderson N 33 64
Nguyen N 16 68
Sharp N 14 75
Jones Y 29 75
McMillan N 19 80
Gabriel N 20 62

Honestly speaking, it looks like an interrogation of a prisoner who will talk ;)...

What's the 1st number in the line?
Average - of what? It's unclear.

Please, (re)read this: [url}http://www.daniweb.com/forums/thread78060.html[/url]

ok so i have this file, and i need to read in the contents(which i posted above) and find the averages for the students listed in the file. the file has the names of the students along with the score so i dont know how to be able to find the average because of the names in the file. Sorry for not giving clear instructions.

to make it easier, here goes the assignment like it was given to me.

A certain advertising company has commissioned a study to determine if a recent advertising campaign is effective for certain age groups. Researchers have created a data file (results.txt) which contains the results attained by the study. The first column is the subject’s name (you may assume there is no whitespace in any name) and the second column indicates if the subject has seen the advertisement in question (Y or y means yes, N or n means no). The third column specifies the subject's age and the last column (the 'score') specifies how favorably the subject views the product being advertised (from 0 to 100 with a score of 100 being most favorable). A example of the file results.txt is attached as part of this assignment. You should download this file to a convenient place on your hard drive. Please note that the file is a sample only. Your program should work on any data file that is formatted in a similar way.

Your assignment is to prompt the user to enter the full pathname to the data file on disk. If the file does not exist in the specified location, your program should exit with a suitable error message.

The first thing your program should do is output to the screen a copy of the data read in from the disk file. This is known as “echoing” the input data.

Your program should then calculate and display the the following results:
 Average score for subjects under 18 who have not seen the ad.
 Average score for subjects under 18 who have seen the ad.
 Average score for subjects 18 and over who have not seen the ad.
 Average score for subjects 18 and over who have seen the ad.
Display your results to two places of decimals, and write your program to automatically list your four calculated averages one to a line, along with a suitable label for each result.

Finally, your program should calculate and display to two places of decimals the overall average score for all of the subjects surveyed. (Note: Mathematically, this is NOT the average of the four averages you calculated previously).

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.