I am considering each student data in 2 lines. 1->name, 2->id, score
Code:
#include<bits/stdc++.h>
using namespace std;
//structure of a student data
typedef struct student {
string name;
int id;
double score;
}student;
//function used to modify the size of the array
student* get_exact(student array[],int current){
student *new_array;
new_array=new student[current];
for(int i=0;i<current;++i)
new_array[i]=array[i];
return new_array;
}
//function to print the student data..
//It is just to check the data.
void print(student student_array[],int size){
for(int i=0;i<size;++i){
cout<<(student_array[i].id)<<"
"<<(student_array[i].name)<<"
"<<(student_array[i].score)<<endl;
}
}
//Finding student with maximum score
void find_max(ofstream& outFile, student student_array[],int
size){
int max=0;
for(int i=1;i<size;++i){
if(student_array[max].score<student_array[i].score)
max=i;
}
//Write the student details in output file.
outFile<<"high score"<<endl;
outFile<<"ID # NAME SCORE"<<endl;
outFile<<"---------------------------"<<endl;
outFile<<(student_array[max].id)<<"
"<<(student_array[max].name)<<"
"<<(student_array[max].score)<<endl;
}
//finding total sum of scores
void find_sum(ofstream& outFile, student student_array[],int
size){
double total_score=student_array[0].score;
for(int i=1;i<size;++i)
total_score+=student_array[i].score;
//Writing total score to file
outFile<<"total score for all students:
"<<endl;
outFile<<total_score<<endl;
}
//comparing 2 strings case insensitive
bool compare(string a,string b){
int l1=a.length(),l2=b.length();
if(l1!=l2) return false;
for(int i=0;i<l1;++i){
if(tolower(a[i])!=tolower(b[i]))
return false;
}
return true;
}
//Search a name and finding sum of scores of all searches
void search(ofstream& outFile, student student_array[],int
size){
double total_score=0;
outFile<<"Search Names:"<<endl;
outFile<<"ID # NAME\t\tSCORE"<<endl;
outFile<<"---------------------------"<<endl;
while(true){
cout<<"type student\'s name
you are searching for (type done to exit) : ";
string s;
cin>>s;
if(s=="done") break;
for(int i=0;i<size;++i){
if(compare(s,student_array[i].name)){
outFile<<(student_array[i].id)<<"
"<<(student_array[i].name)<<"
"<<(student_array[i].score)<<endl;
total_score+=student_array[i].score;
}
}
}
outFile<<"TOTAL SCORE:
"<<total_score<<endl;
}
int main() {
//Take input in string format
//Convert it into char[] as ifstream,ofstream only
accepts char* type not string type
string input,output;
cout<<"type input file name : ";
cin>>input;
cout<<"type output file name : ";
cin>>output;
char
input_file[input.length()+1],output_file[output.length()+1];
strcpy(input_file, input.c_str());
strcpy(output_file, output.c_str());
//read file
ifstream inFile(input_file);
//Exit if reading fails
if(inFile.fail()) {
cerr << "File open fails."
<< endl;
exit (1);
}
//Initialize the array of students with size 4
int size=4;
student *student_array;
student_array=new student[size];
int current=0;
while(!(inFile.eof()) ) {
//Get data
string nam,dat;
getline(inFile, nam);
getline(inFile,dat);
//convert dat to char[] as it
contains both id and score(double)
char data[dat.length()+1];
//convert nam to char[] as nam
contains firstname and lastname
//and we are using only
firstname
char name1[nam.length()+1];
strcpy(name1,nam.c_str());
//get firstname using strtok
char *token1=strtok(name1,"
");
string name(token1);//convert
char[] to string
strcpy(data,dat.c_str());
char *token = strtok(data, "
");
int id=atoi(token);//get id
token = strtok(NULL, " ");
double score=atof(token); //get
score
//if array is full then take
another array and update current array with double size
if(current==size){
student*
new_array;
new_array=new
student[2*size];
for(int
i=0;i<size;++i){
new_array[i]=student_array[i];
}
size*=2;
student_array=new_array;
}
//Store data in next empty index in
array
student_array[current].name=name;
student_array[current].id=id;
student_array[current].score=score;
++current;
//cout<<name<<"
"<<id<<" "<<score<<endl;
}
//current<=size so empty space is there so get
array of exact size
student_array=get_exact(student_array,current);
size=current;
//print(student_array,size);
inFile.close();
//open output file
ofstream outFile (output_file,
std::ofstream::out);
//get student details of maximum score
find_max(outFile,student_array,size);
//get sum of scores of all students
find_sum(outFile,student_array,size);
//search names of students and get details if
found
search(outFile,student_array,size);
return 0;
}
Output:
type input file name : ..\\input.txt
type output file name : ..\\output.txt
type student's name you are searching for (type done to exit) :
Emma
type student's name you are searching for (type done to exit) :
James
type student's name you are searching for (type done to exit) :
Jeff
type student's name you are searching for (type done to exit) :
done
If you have any doubts or finds any error kindly ask me. Thank you
c++ The program will be recieved from the user as input name of an input file...
Write a program that will first receive as input the name of an input file and an output file. It will then read in a list of names, id #s, and balances from the input file specified (call it InFile.txt) which you will create from the data provided below. The program will then prompt the user for a name to search for, when it finds the name it will output to a file (call it OFile.txt) the person’s id#, name,...
Write a complete C++ program that reads students names and their test scores from an input text file. The program should output each student’s name followed by the test scores and the relevant grade in an output text file. It should also find and display on screen the highest/lowest test score and the name of the students having the highest/lowest test score, average and variance of all test scores. Student data obtained from the input text file should be stored...
C++ Write a program that reads students’ names followed by their test scores from the given input file. The program should output to a file, output.txt, each student’s name followed by the test scores and the relevant grade. Student data should be stored in a struct variable of type StudentType, which has four components: studentFName and studentLName of type string, testScore of type int and grade of type char. Suppose that the class has 20 students. Use an array of...
C++ program that reads students' names followed by their test scores (5 scores) from the given input file, students.txt. The program should output to a file, output.txt, each student's first name, last name, followed by the test scores and the relevant grade. All data should be separated by a single space. Student data should be stored in a struct variable of type StudentType, which has four components: studentFName and studentLName of type string, an array of testScores of type int...
C++ (1) Write a program to prompt the user for an input and output file name. The program should check for errors in opening the files, and print the name of any file which has an error, and exit if an error occurs. For example, (user input shown in caps in first line, and in second case, trying to write to a folder which you may not have write authority in) Enter input filename: DOESNOTEXIST.T Error opening input file: DOESNOTEXIST.T...
Write a program that will take input from a file of numbers of type double and output the average of the numbers in the file to the screen. Output the file name and average. Allow the user to process multiple files in one run. Part A use an array to hold the values read from the file modify your average function so that it receives an array as input parameter, averages values in an array and returns the average Part...
The name of the C++ file must be search.cpp Write a program that will read data from a file. The program will allow the user to specify the filename. Use a loop that will check if the file is opened correctly, otherwise display an error message and allow the user to re-enter a filename until successful. Read the values from the file and store into an integer array. The program should then prompt the user for an integer which will...
In an external JavaScript file, you need to create two global variables for the Quarter name array and the Sales amount array. Create an anonymous function and connect it to the onclick event of the Add to Array button that adds the values from the fields (Quarter and Sales) to the respective arrays. Create an anonymous function and connect it to the onclick event of Display Sales button displays the contents of the arrays, displays the sum of all the...
C++ code The assignment has two input files: • LSStandard.txt • LSTest.txt The LSStandard.txt file contains integer values against which we are searching. There will be no more than 100 of these. The LSTest.txt file contains a set of numbers that we are trying to locate within the standard data set. There will be no more than 50 of these. Read both files into two separate arrays. Your program should then close both input files. All subsequent processing will be...