Image

Imageangels_voice wrote in Imagecpp

C++ Homework - Help!

Hello, this is my first time posting here. I am a freshman in college, and I'm having trouble with writing a certain program for my C++ class... It might be pretty easy to a lot of you, but I would just like to note that I have NEVER done programming in my life before college, and I was struggling last term with Java.

This program has to deal mostly with calling .txt files... inputs from data files and having them output as a file...


Each year the state legislature rates the productivity of the faculty of each of the
state-supported colleges and universities. The rating is based on reports submitted
by the faculty members indicating the average number of hours worked per week
during the school year. Each faculty member is rated, and the university receives
an overall rating. The faculty productivity ratings are computed as follows:

a) Highly productive means over 55 hours per week reported.
b) Satisfactory means reported hours per week are 35 and above, but less than 55.
c) Overpaid means reported hours per week are less than 35.

Write a program that reads data from a faculty.txt file. The file consists of two
columns, name of the faculty (string) and hours reported (int). Your program
should read one line at a time (name and hours), calculate the faculty member
rating, and then display the faculty name, the hours reported and the rating. Also,
store the results (name, hours, rating) in an output file named rating.txt. The
output file has three columns per line: name of the faculty, hours and rating. Your
program should also calculate the university overall rating. To do this, calculate
the average of all the hours reported and then use that value to rate the university.
For example, assume that faculty.txt: has the following data:

John 63
Andy 37
Jake 20
Anna 55
Saul 72
Tony 40
Tina 12

Your sample output should look similar to this:

**** University rating program. ****

Name Hours Rating
John 63 Highly Productive
Andy 37 Satisfactory
Jake 20 Over Paid
Anna 55 Satisfactory
Saul 72 Highly Productive
Tony 40 Satisfactory
Tina 12 Over Paid

University Overall rating:
Average Hours: 42.7
Rating: Satisfactory

And your output file rating.txt should look like this:

John 63 Highly Productive
Andy 37 Satisfactory
Jake 20 Over Paid
Anna 55 Satisfactory
Saul 72 Highly Productive
Tony 40 Satisfactory
Tina 12 Over Paid


I think my main problem is that I don't know how to make it read the number of hours, since there's a space in between the hours and the name. If the text file had just numbers like:

100
90
80

or whatever, and I had to calculate the data from that, I think I'd be fine... But I'm really confused... if someone could please help me, it'd be greatly appreciated... This is due Tuesday, February 28, before midnight.

Edit: Okay, I got the program done tonight with help from a friend in my class. Thanks for your input, everyone! Here's the

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

int main()
{
string name;
string rating;
int count = 0;
int hours;
int total_hours = 0;
double average;
int total_teachers = 0;

cout << "Please enter a number of teachers from the list: ";
cin >> total_teachers;

cout << endl;

ifstream fin;
fin.open("faculty.txt");

ofstream fout;
fout.open("ratings.txt");

cout << "Name" << '\t' << "Hours" << '\t' << "Rating" << endl << endl;

while (count < total_teachers)
{
fin >> name;
fin >> hours;

total_hours = total_hours + hours;

if (hours > 55)
{
rating = "Highly Productive";
}

else if (hours < 35)
{
rating = "Over Paid";
}

else
{
rating = "Satisfactory";
}

cout << name << '\t' << hours << '\t' << rating << endl;

count++;

fout << name << '\t' << hours << '\t' << rating << endl;

} // end while

average = static_cast< double >(total_hours) / static_cast< double >(total_teachers);

if (average > 55.0)
{
rating = "Highly Productive";
}

else if (average < 35.0)
{
rating = "Over Paid";
}

else
{
rating = "Satisfactory";
}

cout << endl;

cout << fixed << setprecision(1) << "Average Hours Worked: " << average << endl;
cout << "Average Rating: " << rating << endl << endl;

fin.close();
fout.close();

return 0;
}