I have a program exercise from C++ programming 6th edition: pg 585 exercise 6. I
ID: 663701 • Letter: I
Question
I have a program exercise from C++ programming 6th edition: pg 585 exercise 6. I need to
Use the file in this folder as input but assume that the file name can change so prompt the user.
The file format is different than what is indicated in the book so adapt your code accordingly.
Assume that any answer that is not "T" or "t" or "F" or "f" is invalid and considered wrong. As indicated in the book, treat spaces as no answer.
Use a "switch" statement when determining the grade.
Only display the number of records processed to the console, instead save the output indicated in the book to a file named "FinalGrades.txt".
Below is my code but I have a lot of errors:
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
//function prototypes
void readDataFromFile(ifstream &, char[], string[], string[]);
void calculateScores(char[], string[], double[]);
void calculateGrades(double[], char[]);
//global declarations for constants
const int MAX_ANSWERS = 20;
const int MAX_STUDENTS = 200;
const double MAX_MARKS = 40.0;
//global declarations for a vaiable
int studentsCounter = 0;
//start main function
int main()
{
//variables declaration
ifstream inFIle;
string fileName;
char testAnswers[MAX_ANSWERS];
string studentID[MAX_STUDENTS];
string studentScores[MAX_STUDENTS];
double studentScores[MAX_STUDENTS];
char studentGrades[MAX_STUDENTS];
Prompt the user to enter the name of a file and open the file and display an error message if the file is not found.
//prompt the user to enter the name of a file
cout << "Enter the file name:";
cin >> fileName;
/*open the file and display an error messageif the file not found*/
inFile.open(FinalGrades.txt);
if (InFile)
{
/*display an error message if the file not found and exit the program */
cout << "FinalGrades.txt" << FileName
<< "is not found." << endl;
system("pause");
return 1;
}//end if
Call readDataFromFIle, calculateScores, and calculateGrades functions to read data and calculate score and grade of students.
//call readDataFromFile function
readDataFromFIle(inFile, testAnswers, studentID, studentAnswers);
//call calculateScores function
calculateScores(testAnswers, studentAnswers, studentScores);
//call calculateGrades function
calculateGrades(studentScores, studentGrades);
Display the details of each student as ID, answers, score, and grade respectively.
//display the results
cout << "
Results of the students."<<endl;
cout << "Student_ID Student_Answers Score Grade"
<< endl;
cout << "--------------------------------------" << endl;
for (int student = 0, student < st[studentsCounter; student++)
{
cout << studentID[student] << " "
<< studentAnswers[student] << " "
<< studentScores[student] << " "
<< studentGrades[student] << endl;
}//end for
//close the input file
inFile.close();
//pause the system for a while
system("pause");
return 0;
}//end of main function
Explanation / Answer
Program:
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
//function prototypes
void readDataFromFile(ifstream &, char[], string[], string[]);
void calculateScores(char[], string[], double[]);
void calculateGrades(double[], char[]);
//global declarations for constants
const int MAX_ANSWERS = 20;
const int MAX_STUDENTS = 200;
const double MAX_MARKS = 40.0;
//global declarations
int studentsCounter = 0;
char testAnswers[MAX_ANSWERS];
string studentID[MAX_STUDENTS];
string studentAnswers[MAX_STUDENTS];
double studentScores[MAX_STUDENTS];
char studentGrades[MAX_STUDENTS];
//start main function
int main()
{
//variables declaration
ifstream inFile;
string fileName;
//Prompt the user to enter the name of a file and open the file and display an error message if the file is not found.
//prompt the user to enter the name of a file
cout << "Enter the file name:";
cin >> fileName;
/*open the file and display an error messageif the file not found*/
inFile.open("FinalGrades.txt");
if (inFile)
{
/*display an error message if the file not found and exit the program */
cout << fileName<< " is not found." << endl;
system("pause");
return 1;
}//end if
//Call readDataFromFIle, calculateScores, and calculateGrades functions to read data and calculate score and grade of students.
//call readDataFromFile function
readDataFromFile(inFile, testAnswers, studentID, studentAnswers);
//call calculateScores function
calculateScores(testAnswers, studentAnswers, studentScores);
//call calculateGrades function
calculateGrades(studentScores, studentGrades);
//Display the details of each student as ID, answers, score, and grade respectively.
//display the results
cout << " Results of the students."<<endl;
cout << "Student_ID Student_Answers Score Grade"<< endl;
cout << "--------------------------------------" << endl;
for (int student = 0; student < studentsCounter; student++)
{
cout << studentID[student] << " "
<< studentAnswers[student] << " "
<< studentScores[student] << " "
<< studentGrades[student] << endl;
}//end for
//close the input file
inFile.close();
//pause the system for a while
system("pause");
return 0;
}//end of main function
Note: check bolded statements. errors occured due to wrong identifiers (ex: InFile instead of inFile.)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.