Please help. My code is below and it is incomplete. It compiles, but doesn\'t di
ID: 3833932 • Letter: P
Question
Please help. My code is below and it is incomplete. It compiles, but doesn't display everything I need it to. Please fix my code.
C++ Program Help
This uses functions, structs, arrays of structs, filestreams.
For a multiple choice test, the possible responses are a, b, c, or d.
The maximum number of the class is 35.
An input file has:
1st line: 20 lower case letters that represent the correct answers. There will be no blank spaces between the letters. Example: abcdabcdabcdabcdabcd
2nd line - last line: Each line will start with an ID# (int), followed by the first and last names of a student (names will be separated by at least one blank space). The names may be in a mixture of upper and lower case letters. The names will be followed by at least 1 blank space and then that student's responses to the test questions will be provided. There will not be any blank spaces between the responses, but they may be in a mixture of upper and lower case letters. An upper case letter should be considered correct if it matches the lower case equivalent in the key.
Example: 63 biLL jOnEs aBcdAbCDabDcabDDABCD
NOTE: The key will consist of exactly 20 characters. However, the student responses may consist of more or less than 20 characters. Assume that the first 20 characters in a student response correspond to the 20 test questions. In other words, if a student only provides 15 answers, then the last 5 questions are wrong. If a student provides 25 answers, the last 5 responses should be ignored.
A second input file contains several integers that are supposed to be the ID#s for some of the students in the class.
Create a C++ program that will
interactively prompt for and read the name of the first input file (with key and student data) and use a filestream variable to represent the file
read the data from the input file and store the student information (ID#s, names, test answers) into an array of structs, counting the students as the data is read
reformat the names into a more conventional form
use the key to grade the exams and store each student's score in the array
interactively prompt for and read the name of an output file
write the following to the specified output file (separate each section with a blank line to improve readability)
a report that displays the names of the students (alphabetically by last name) along with their ID#s and test scores (see formatting specifications and samples below)
a report that displays the class standings with ID#s and test scores (in descending order) (see formatting specifications and samples below)
the standings report should include
the number of students who took the test with a label (maximum class size is 35)
the class average (2 digits to right of decimal)
the median (middle value or average of middle values if there are an even number of students, 2 digits to right of decimal)
interactively prompt for and read the name of the second input file< >write an appropriate message to indicate the start of the second input file processingwrite the test key to the output file (with label)
read each integer, if it matches an ID# in the student array, write the student's name, test score, and test answers (you may reformat the test answers if you wish) with labels to the output file
if the integer does not match an ID# in the array, write an error message that includes the invalid ID# to the output file
Grading a Test
Each of the questions on the test is worth 5 points. The test score is the # of correct responses multiplied by the point value of a question.
Assumptions
The first input file will not be empty. Each line in the file will be terminated by a linefeed (' '). It will be formatted as described above.
The second input file will not be empty. Each integer will be separated by whitespace. The last line in the file will be terminated by a linefeed (' ').
ID#s will be between 1 and 99.
The length of a name (first or last) will be 10 characters or less, for formatting purposes.
The program MUST make use of functions (at least 5 meaningful functions in addition to main).
CREATE A STRUCT DATA TYPE TO STORE STUDENT DATA AND THEN DECLARE AN ARRAY OF STRUCTS.
PASS PARAMETERS to communicate values. No global variables are allowed.
No goto statements may be used.
Program must make use of filestream variables to represent the input and output files. Each input file may only be read one time.
When prompting for file names, the required order is: 1st input file, output file, 2nd input file.
Average and median (if even number of students) should be displayed with 2 digits to right of decimal.
For first report, right justify ID#s and test scores. Left justify names. Display names: last,first.
For second report (class standings), right justify ID#s and scores.
Program must include preprocessor directives for all header files used.
Program must use a static array (do not use a variable for the size of the array).
Struct declaration - each member (field) must be adequately described with a comment.
When a named constant and/or major variable is declared, provide a meaningful description of what it represents in the program.
For each function, clearly state what will be passed into the function and what will be passed out or returned by the function. Document important local variables.
DO NOT USE SSTREAM, ALGORITHM, LIMIT IN YOUR PROGRAM.
#include
#include
#include
#include
#include
using namespace std;
const int MAX = 35;
const int N = 25;
struct studentInfo
{
string first, last; //student's first and last name
int idNum; //student's ID#
};
void get_data(studentInfo[], int&); //takes in student last name, first name, and ID number.
void fixstring(string&); //Reformats the string of last names and first names.
void bubblesortOne(studentInfo[], int); //sorts the names of students in alphabetical order.
void print_roster(studentInfo[], int); //prints things
int main()
{
studentInfo theClass[MAX]; //array to store student information
int n; //number of students in the class
get_data(theClass, n); //goes to get_data function
bubblesortOne(theClass, n); //goes to bubblesort function for sorting names.
print_roster(theClass, n); //goes to print roster function to print things.
return 0;
}
void get_data(studentInfo theClass[], int& n)
{
string filename; //name of input file
ifstream in; //input file
cout << "Enter the name of the first input file" << endl;
cin >> filename;
n = 0; //Not sure what this does.
in.open(filename.c_str());
in >> theClass[n].idNum;
while(in)
{
in >> theClass[n].last >> theClass[n].first;
fixstring(theClass[n].last); //initializes fixing the string of last name
fixstring(theClass[n].first); //initializes fixing the string of first name
n++;
in >> theClass[n].idNum;
}
in.close();
// Goes back to main to work on bubblesort
}
void fixstring(string& word)
{
int wLen = word.length(); //declares word length (of the string)
word[0] = toupper(word[0]);
for (int i=1; i word[i] = tolower(word[i]);
//After fixing first and last name string, goes back to get_data
}
void bubblesortOne(studentInfo list[], int n)
{
int i; int j;
studentInfo temp;
for (i=0; i < n-1; i++)
for (j=0; j < n-(i+1); j++)
if (list[j].last > list[j+1].last)
{
temp = list[j];
list[j] = list[j+1];
list[j+1] = temp;
}
//Goes back to main after sorting.
}
void print_roster(studentInfo list[], int n)
{
cout << left << setw(N) << "NAME" << right << setw(N-15) << "ID#" << setw(N-15) << "SCORE" << endl;
for (int i=0;i {
cout << setw(N) << left << list[n].last+','+list[n].first;
}
}
Explanation / Answer
Fix 1
void fixstring(string& word)
{
int wLen = word.length(); //declares word length (of the string)
word[0] = toupper(word[0]);
for (int i=1; i<wLen; i++) // fixed the for loop here
word[i] = tolower(word[i]);
//After fixing first and last name string, goes back to get_data
}
Fix 2:
void print_roster(studentInfo list[], int n)
{
cout << left << setw(N) << "NAME" << right << setw(N-15) << "ID#" << setw(N-15) << "SCORE" << endl;
for (int i=0;i<n; i++) { // id is not printed here and not counter variable used. // This should print everything
cout << setw(N) << left << to_string(list[i].idNum)<<" "<<list[i].last+','+list[i].first<<endl;
}