Hello please C++ question. I will rate answer. How do you set up a program that
ID: 3833052 • Letter: H
Question
Hello please C++ question. I will rate answer.
How do you set up a program that is set up so
Open first file.
Read in names and scores and store them in variables.
Close first file.
Open second file.
Read in names and score and store them in variables.
Close second file.
Create third file.
Write in the information you stored from the other two files.
Close third file.
using fstream ? How do i read in the variables that are in the 2 files (ints. and strings) and write my printed statements that use those variables in the output file? please help thanks
Update:
When prompting for file names, the required order is: 1st input file, output file, 2nd input file.
1st Input file contains (test key, then list of id#, first name, last name, answers to test):
[example@bobby keys]$ more first4five
abcdabcdabcdabcdabcd
23 boNNie jOnEs ABCDabcdABCDabcdABCD
71 amy sMITH dcbaABCDabcdABCdabCaBBBB
10 mAttHeW ADams AAAAAAAAAAAA
31 BarBarA mARTINSon ABCDABCDABCDABCDABdb
19 jENNIFER schWarTz aabbcccdabcdabcdabcd
56 lArrY blaCk ABCDABCDABCDABCDaBcbbbbbbbb
2nd input contains (id #'s ):
[example@bobby keys]$ more second4five
56 13 10
19
-5
Then Print out calculations using variables read to output file.
An input file has been created that contains the following:
1st line: The answer key consists of 20 lower case letters that represent the correct answers to the exam. 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.
Requirements:
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)
your name, section #, assignment #
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.
Rules
The program MUST make use of functions (at least 5 meaningful functions in addition to main).
The program MUST CREATE A STRUCT DATA TYPE TO STORE STUDENT DATA AND THEN DECLARE AN ARRAY OF STRUCTS.
The program MUST 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.
Failure to adhere to the 6 previous requirements will result in up to a 60% deduction of assignment's point value.
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).
Sample terminal session:
[lee@bobby keys]$ more first4five
abcdabcdabcdabcdabcd
23 boNNie jOnEs ABCDabcdABCDabcdABCD
71 amy sMITH dcbaABCDabcdABCdabCaBBBB
10 mAttHeW ADams AAAAAAAAAAAA
31 BarBarA mARTINSon ABCDABCDABCDABCDABdb
19 jENNIFER schWarTz aabbcccdabcdabcdabcd
56 lArrY blaCk ABCDABCDABCDABCDaBcbbbbbbbb
[lee@bobby keys]$ more second4five
56 13 10
19
-5
[lee@bobby keys]$ g++ assign05.cpp
[lee@bobby keys]$ ./a.out
Enter name of first input file
first4five
Enter the name of the output file
fiveout
Enter name of 2nd input file
second4five
[lee@bobby keys]$ more fiveout
Lee Misch Assign #5 Section #10__
NAME ID# SCORE
Adams,Matthew 10 15
Black,Larry 56 95
Jones,Bonnie 23 100
Martinson,Barbara 31 90
Schwartz,Jennifer 19 75
Smith,Amy 71 75
CLASS STANDINGS
ID# SCORE
23 100
56 95
31 90
19 75
71 75
10 15
# of Students: 6
Class Average: 75.00
Median Score: 82.50
BEGIN STUDENT SEARCH FILE PROCESSING
Test key: abcdabcdabcdabcdabcd
ID#: 56
Name: Larry Black
Test score: 95
Test responses: abcdabcdabcdabcdabcbbbbbbbb
13 is not a valid student ID#
ID#: 10
Name: Matthew Adams
Test score: 15
Test responses: aaaaaaaaaaaa
ID#: 19
Name: Jennifer Schwartz
Test score: 75
Test responses: aabbcccdabcdabcdabcd
-5 is not a valid student ID#
My code so far:
#include <iomanip>
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
using namespace std;
struct newstudent
{
string first, last, answers;
int idnum, score;
};
//void get_data(newstudent[], int&);
//void read_scores(ifstream& ,int[], int);
void fixstring(string& word);
void fixstring2(string& word);
void bubblesort(newstudent[], int);
void print_roster(newstudent[],int);
void read_scores(ifstream&,int[],int);
//int grade_test(newstudent);
string getanswers(ifstream&);
int main()
{
//newstudent theclass[50];
newstudent list[59];
int n = 0;
int correct = 0;
int testkeylength;
//testkey = getanswers(inf);
//testkeylength = testkey.length();
//get_data(theclass, n); //ask for 3 file names
string testkey, inputName, input2Name, outputName;
ifstream inf;// Input 1 file
ifstream inf2;// Input 2 file
ofstream outf;// Out file
//Ask for input 1 READ IN
cout << "Enter name of first input file" << endl;
cin >> inputName;
//Ask for output WRITE OUT
cout << "Enter the name of the output file" << endl; //interactively prompt the user for the name of the output file and read it
cin >> outputName;
//Ask for input 3 READ IN
cout << "Enter name of 2nd input file" << endl; //interactively prompt the user for the name of the output file and read it
cin >> input2Name; //fileName3 = input
//Read Input 1
inf.open(inputName.c_str());
inf >> testkey;
while (inf)
{
inf >> list[n].idnum >> list[n].first >> list[n].last >> list[n].answers;
//list[n].score = 0;
//read_scores(inf, cs[n].answers, 20);
n++; // add 1 to n
//inf >> list[n].idnum; // Read idnumber
}
//
//Read Input 2
inf2.open(input2Name.c_str());
while (inf2)
{
//inf2 >> checkid;
}
//
//Calculate
//read_scores(inf,list[n].answers, 20); //Reads all scores
int answerlength = testkey.length();
for (int i = 0; i < 20; i++)
inf >> list[i];
fixstring(list[n].first); //First letter upppercase, rest lower
fixstring(list[n].last); //First letter uppercase, rest lower
fixstring2(list[n].answers); //First letter uppercase, rest lower
bubblesort(list, n); //First/Last Names = Least to greatest
outf.open(outputName.c_str());
int points;
int id;
char grade;
int N = 25;
//Write variables in Output 3
outf << left << setw(N) << "NAME" << right << setw(N-15) << "ID#" << setw(N-15) << "SCORE" << endl;
for (int i = 0; i < n;i++)
{
outf << setw(N) << left << list[i].last+','+list[i].first;
//points = sum_points(list[i]);
//list[i].totpts = points;
//list[i].idnum = id;
//grade = assign_grade(points);
outf << right << setw(N-15) << list[i].idnum << setw(N-15) << "INSERT SCORE" << endl;
}
//
return 0;
}
void fixstring(string& word) //Reformats , First letter uppercase, rest lower
{
int wordlength = word.length();
word[0] = toupper(word[0]); //First letter uppercase
for (int i = 1; i < wordlength; i++) //Rest of letters are lowercase
word[i] = tolower(word[i]);
}
void fixstring2(string& word) //makes all student answers lowercase
{
int wlen=word.length();
for (int i=0; i<wlen; i++)
word[i] = tolower(word[i]);
}
void bubblesort(newstudent list[],int n) //sorts from least to greatest
{
int i; int j;
newstudent 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;
}
}
Please ask if you need more details
Explanation / Answer
#include <iomanip>
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
using namespace std;
struct newstudent
{
string first, last, answers;
int idnum, score;
};
//void get_data(newstudent[], int&);
//void read_scores(ifstream& ,int[], int);
void fixstring(string& word);
void fixstring2(string& word);
void bubblesort(newstudent[], int);
void print_roster(newstudent[],int);
void read_scores(ifstream&,int[],int);
//int grade_test(newstudent);
string getanswers(ifstream&);
int main()
{
//newstudent theclass[50];
newstudent list[59];
int n = 0;
int correct = 0;
int testkeylength;
//testkey = getanswers(inf);
//testkeylength = testkey.length();
//get_data(theclass, n); //ask for 3 file names
string testkey, inputName, input2Name, outputName;
ifstream inf;// Input 1 file
ifstream inf2;// Input 2 file
ofstream outf;// Out file
//Ask for input 1 READ IN
cout << "Enter name of first input file" << endl;
cin >> inputName;
//Ask for output WRITE OUT
cout << "Enter the name of the output file" << endl; //interactively prompt the user for the name of the output file and read it
cin >> outputName;
//Ask for input 3 READ IN
cout << "Enter name of 2nd input file" << endl; //interactively prompt the user for the name of the output file and read it
cin >> input2Name; //fileName3 = input
//Read Input 1
inf.open(inputName.c_str());
inf >> testkey;
while (inf)
{
inf >> list[n].idnum >> list[n].first >> list[n].last >> list[n].answers;
//list[n].score = 0;
//read_scores(inf, cs[n].answers, 20);
n++; // add 1 to n
//inf >> list[n].idnum; // Read idnumber
}
//
//Read Input 2
inf2.open(input2Name.c_str());
while (inf2)
{
//inf2 >> checkid;
}
//
//Calculate
//read_scores(inf,list[n].answers, 20); //Reads all scores
int answerlength = testkey.length();
for (int i = 0; i < 20; i++)
inf >> list[i];
fixstring(list[n].first); //First letter upppercase, rest lower
fixstring(list[n].last); //First letter uppercase, rest lower
fixstring2(list[n].answers); //First letter uppercase, rest lower
bubblesort(list, n); //First/Last Names = Least to greatest
outf.open(outputName.c_str());
int points;
int id;
char grade;
int N = 25;
//Write variables in Output 3
outf << left << setw(N) << "NAME" << right << setw(N-15) << "ID#" << setw(N-15) << "SCORE" << endl;
for (int i = 0; i < n;i++)
{
outf << setw(N) << left << list[i].last+','+list[i].first;
//points = sum_points(list[i]);
//list[i].totpts = points;
//list[i].idnum = id;
//grade = assign_grade(points);
outf << right << setw(N-15) << list[i].idnum << setw(N-15) << "INSERT SCORE" << endl;
}
//
return 0;
}
void fixstring(string& word) //Reformats , First letter uppercase, rest lower
{
int wordlength = word.length();
word[0] = toupper(word[0]); //First letter uppercase
for (int i = 1; i < wordlength; i++) //Rest of letters are lowercase
word[i] = tolower(word[i]);
}
void fixstring2(string& word) //makes all student answers lowercase
{
int wlen=word.length();
for (int i=0; i<wlen; i++)
word[i] = tolower(word[i]);
}
void bubblesort(newstudent list[],int n) //sorts from least to greatest
{
int i; int j;
newstudent 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;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.