Bring in program grades.cpp and grades.txt from the Lab 10 folder. Fill in the c
ID: 3761320 • Letter: B
Question
Bring in program grades.cpp and grades.txt from the Lab 10 folder. Fill in the code in bold so that the data is properly read from grades.txt. and the desired output to the screen is as follows:
#include
#include
using namespace std;
// PLACE YOUR NAME HERE
const int MAXNAME = 20;
int main()
{
ifstream inData;
inData.open("grades.txt");
char name[MAXNAME + 1]; // holds student name
float average; // holds student average
inData.get(name,MAXNAME+1);
while (inData)
{
inData >> average;
// Fill in the code to print out name and
// student average
// Fill in the code to complete the while
// loop so that the rest of the student
// names and average are read in properly
}
return 0;
}
Explanation / Answer
#include<iostream>
#include<fstream>
using namespace std;
// PLACE YOUR NAME HERE
const int MAXNAME = 20;
int main()
{
ifstream inData;
inData.open("grades.txt");
char fname[MAXNAME + 1]; // holds student first name
char lname[MAXNAME + 1]; // and this one student's last name
float average; // holds student average
if (inData) {
while (!inData.eof())
{
inData>>fname>>lname;
inData >> average;
printf("%s %s has a(n) %2f average ", fname, lname, average);
}
} else {
cout<<"Cannot open file!! ";
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.