while(in_file){
//Output the line
cout << left << setw(24) << setfill( ' ' ) << "Grade Value" << ": " << grade_value << "\n"
<< left << setw(24) << setfill( ' ' ) << "Course Name" << ": ";
//Classify the course as CS150 or CS250
//increment the appropriate counter
//update the appropriate total
if( course_name == "CS150" ){
//Adding all of the grades for CS150
grade_count_cs150++;
addition_total_cs150 += grade_value;
cout << "CS150\n";
}
///Complete the else part for CS250
else {
//Adding all of the grades for CS250
grade_count_cs250++;
addition_total_cs250 += grade_value;
cout << "CS250\n";
}
//Insert a blank line
cout << "\n";
///Read next line from the input file
in_file >> grade_value >> course_name;
}//End of while loop
Both Grade_Counts are initialized as Double and grade_value/course_name were read before the while loop began.
Edit: here's even more of the code to show
ifstream in_file;
in_file.open("A4-Input.txt");
///Write the Code for File Stream Validation, Print an ERROR message if the file could not be found!
if( !in_file )
{
cerr << "\n\n Error: Input file could not be opened."
<< "\n\tExiting the program.\n\n\n";
return 1;
}
//Set cout precision
cout.precision( 2 );
cout.flags( ios::showpoint | ios::fixed );
//Read the first line
in_file >> grade_value >> course_name;
while(in_file){
//Output the line
cout << left << setw(24) << setfill( ' ' ) << "Grade Value" << ": " << grade_value << "\n"
<< left << setw(24) << setfill( ' ' ) << "Course Name" << ": ";
//Classify the course as CS150 or CS250
//increment the appropriate counter
//update the appropriate total
if( course_name == "CS150" ){
//Adding all of the grades for CS150
grade_count_cs150++;
addition_total_cs150 += grade_value;
cout << "CS150\n";
}
///Complete the else part for CS250
else {
//Adding all of the grades for CS250
addition_total_cs250 += grade_value;
grade_count_cs250++;
cout << "CS250\n";
}
//Insert a blank line
cout << "\n";
///Read next line from the input file
in_file >> grade_value >> course_name;
}//End of while loop
//Print Program Heading
cout << left << setw( 60 ) << setfill( '*' ) << "*" << "\n"
<< right << setfill( ' ' ) << setw( 38 ) << "Average Calculator\n"
<< left << setw( 60 ) << setfill( '*' ) << "*" << "\n";
//Output the final summary
///Complete the cout statement
cout << setfill(' ')
<< left << setw(40) << "Number of grades in CS150" << ": " << right << setw(8) << grade_count_cs150 << "\n"
<< left << setw(40) << "Total Addition of grades in CS150" << ": " << right << setw(8) << addition_total_cs150 << "\n"
<< left << setw(40) << "Average Numeric grade in CS150" << ": " << right << setw(8) << addition_total_cs150/grade_count_cs150 << "\n"
<< "\n"
<< left << setw(40) << "Number of grades in CS250" << ": " << right << setw(8) << grade_count_cs250 << "\n"
<< left << setw(40) << "Total Addition of grades in CS250" << ": " << right << setw(8) << addition_total_cs250 << "\n"
<< left << setw(40) << "Average Numeric grade in CS250" << ": " << right << setw(8) << addition_total_cs250/grade_count_cs250 << "\n";
///Close the input file
in_file.close();
}
//End of Program 2
else{
cout<< "INVALID CHOICE! Please choose a number from Main Menu!"<<endl;
}
return 0;
}