bg287

csc201 20100929 Homework

Posted in csc201 by bnmng on 2010 09/29
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
int main () {
	
	ifstream infile;
	ofstream outfile;
	string section; //The section to be read from the input file
	int score, score_count;  //The score to be read from the input file and a counter for how many scores are on each line
	int score_max, score_min ; //The max and min score and sum of scores to be used for the average
	int average_count; //The sum of scores and count to be used for the average of averages
	float score_sum, score_average, average_sum, average_average; //The average for each line of scores, and the final average.
	char zs; //User input to skip or count as zero sections with no data.
	
	//Initialize
	average_count=0;
	average_sum=0;
	
	infile.open ("indata.dat");
	outfile.open ("outdata.dat");
	if(!infile) {
		cout << "Oops.  Something wrong with the input file";
		cin.get(); cin.get();
		return 1;
	}
	if(!outfile) {
		cout << "Oops.  Something wrong with the output file";
		cin.get(); cin.get();
		return 1;
	}
	
	infile >> section;
	outfile <<     "Section Min  Max    Avg\n\n";
	cout    << "\n\nSection Min  Max    Avg\n\n";
	
	while(infile) {      // Loop for each line 
		
		score_count=0;
		score_average=0;
		score_sum=0;
		
		//Begin reading scores
		infile >> score;
		score_max=score; score_min=score;
		while ( score > -1 and infile) { //Loop for each schore
			score_count++;
			score_sum+=score;
			if(score > score_max) score_max=score;
			if(score < score_min) score_min=score;
			infile >> score;
		}
		if (score_count<1) {
			zs=' ';
			cout << "There are no scores for section " << section << ".  Count as (z)ero or (s)kip?:";
			while (!(zs=='s' or zs=='z')) {
				cin >> zs;
			} 
		}
		if ((score_count>0) or (zs=='z')) { 
			average_count++;
			if (score_count>0) {
				score_average=score_sum / score_count;
				outfile << section << fixed << setw(5) << score_max << setw(5) << score_min << setw(10) << setprecision(2) << score_average << endl << endl;
				cout    << section << fixed << setw(5) << score_max << setw(5) << score_min << setw(10) << setprecision(2) << score_average << endl << endl;
			}else {
				score_average=0;
				outfile << section << fixed << setw(20)  << setprecision(2) << score_average << endl << endl;
				cout    << section << fixed << setw(20)  << setprecision(2) << score_average << endl << endl;
			}
			average_sum+=score_average;
			
		} 
		//Begin reading the next line
		infile >> section;
	}
	average_average=average_sum / average_count;
	outfile << "Total Avg: " << fixed << setw(13) << setprecision(2) << average_average << endl << endl;
	cout    << "Total Avg: " << fixed << setw(13) << setprecision(2) << average_average << endl << endl;

	cin.get();  //No sytem("PAUSE") in linux.
	cin.get();

	return 0;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: