Question

In: Computer Science

C++ Only Create a function named PrintStudents, which takes a string input filename and an integer...

C++ Only

Create a function named PrintStudents, which takes a string input filename and an integer minimum score value and a string output file name as a parameters. The function will read the student scores and names from the file and output the names of the students with scores greater than or equal to the value given. This function returns the integer number of entries read from the file. If the input file cannot be opened, return -1 and do not print anything to the file.

Read each line from the given filename, parse the data, process the data, and write the required information to the file.

Each line of the file contains <FIRST-NAME LAST-NAME>, <SCORE>, <SUBJECT> . Read and parse the data, then write to the output file the names and classes for scores matching the criteria.

Example: With the following data and value of 80:

Constance Shelton, 67, APPM 2002
Charlotte Edwards, 85, CSCI 1300
Alyssa Hill, 78, MATH 1000
Pat Owens, 75, HUMN 1342
Shannon Jimenez, 96, LING 2000
Kristen Swanson, 80, PSYC 1001
Jim Schwartz, 60, CVEN 3241

Your function should return 7 and output to the file should contain:

Charlotte Edwards, CSCI 1300
Shannon Jimenez, LING 200
Kristen Swanson, PSYC 1001

You only need to write the function code for PrintStudents. The split() function is provided, and functions as it does in the homework assignments, to make parsing the output easier. Recall that split takes a string s and splits it at the input delimiter sep, to fill the array words[] up to a capacity of max_words. The return value of split is the number of actual words the string is split into.

int split(string s, char sep, string words[], int max_words);

Solutions

Expert Solution

THE BELOW CODE REPRESENTS THE CODE FOR C++;

#include <iostream>

#include <fstream>

#include <string>

using namespace std;

// function declaration

int PrintStudents(string inputFile, int min_score,string outputFile);

int split(string s,char sep, string words[], int max_words );

int main() {

// test the function

int n = PrintStudents("students.txt",80,"outStudents.txt");

if(n != -1)

cout<<"\n No. of students : "<<n<<endl;

else

cout<<"\n No such file exists";

return 0;

}

// function to write the details of the students whose score >= min_score

// inputs : input file, minimum score and output file

// output : number of records read, -1 if error

int PrintStudents(string inputFile, int min_score,string outputFile)

{ // open the input file

ifstream fin(inputFile.c_str());

// if input file can be opened

if(fin.is_open())

{ // open the output file for writing

ofstream fout(outputFile.c_str());

int n=0,score;

string line;

string words[3];

// read till the end of file

while(!fin.eof())

{

getline(fin,line);

n++;

// get the score from the record read

score = split(line,',',words,3);

// check if score>=min_score, then write to output file

if(score >= min_score)

fout<<words[0]<<", "<<words[2]<<"\n";

if(fin.eof())

break;

}

// close the files

fin.close();

fout.close();

return n; // return the number of records read

}else

{

fin.close();

return -1; // file not found

}

}

// function to split a string on a given separator and return the score

// inputs : string , word separator, array of words and max_words

// output is the score

int split(string s,char sep, string words[], int max_words )

{

int index=0,i=0 ;

//loop to extract the words from the line

while(s.find(sep,index+1)!= -1){

words[i] = s.substr(index,s.find(sep,index+1)-index);

i++;

index = s.find(sep,index+1)+2;

}

words[i] = s.substr(index); // extract the last word

// convert to integer and return the score

return(stoi(words[1]));

}

INPUT:

OUTPUT:

CONSOLE:


Related Solutions

Function named FunCount takes three arguments- C, an integer representing the count of elements in input...
Function named FunCount takes three arguments- C, an integer representing the count of elements in input list IP- input list of positive integers. Item- an integer value. Function FunCount returns an integer representing the count of all the elements of List that are equal to the given integer value Key. Example: Don’t take these values in program, take all inputs from user C = 9, IP= [1,1,4,2,2,3,4,1,2], Item = 2 function will return 3 IN C PROGRAMMING
C++ Write a program that takes a string and integer as input, and outputs a sentence...
C++ Write a program that takes a string and integer as input, and outputs a sentence using those items as below. The program repeats until the input string is "quit". If the input is: apples 5 shoes 2 quit 0 the output is: Eating 5 apples a day keeps your doctor away. Eating 2 shoes a day keeps your doctor away.
Write a function that takes a C string as an input parameter and reverses the string.
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
in c++ Write a function that takes a C string as an input parameter and reverses...
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
Write a C function str_to_float() that takes a numeric string as input and converts it to...
Write a C function str_to_float() that takes a numeric string as input and converts it to a floating point number. The function should return the floating point number by reference. If the conversion is successful, the function should return 0, and -1 otherwise (e.g. the string does not contain a valid number). The prototype of the function is given below int str_to_float(char * str, float * number);
Matlab Create/write a function that takes an input of x and y data, and a string...
Matlab Create/write a function that takes an input of x and y data, and a string (either linear? or quadratic), sets up a linear system of equations (Ax = b), and solves and plots the model.
In MATLAB define a function named primes that takes a non-negative integer, ?, as its only...
In MATLAB define a function named primes that takes a non-negative integer, ?, as its only argument and returns a row vector containing the first ? prime numbers in order. Assume that the first prime number is 2.  Other than the zeros function, the ones function, and the colon (:) operator, you may not use any Matlab built-in array functions.
in c++, please provide only a function. the function should be named "tally." it takes no...
in c++, please provide only a function. the function should be named "tally." it takes no parameters but returns an int. the first int should be 0, next being 1, then 2, etc.. will rate if correct. again should only be 1 function.
C++ Please Define a function named "isAscending" that accepts a string as an input parameter and...
C++ Please Define a function named "isAscending" that accepts a string as an input parameter and returns "true" if all the characters included in the string are ordered in ascending order of their ASCII codes or the input string is a null string, and returns "false" otherwise. For example, if the string "ABXab" is passed to the function, it returns "true" because the ASCII code of 'B' is greater than 'A', 'X' is greater than 'B', 'a' is greater than...
Define a function file_to_hist() which takes a string representing a filename, opens the file, reads its...
Define a function file_to_hist() which takes a string representing a filename, opens the file, reads its contents, closes the file,* and returns a histogram based on the letter frequencies in the given file. If no such file exists, your function should return an empty histogram (i.e., an empty dictionary {}). So for example, if the file nash.txt was in the same directory as char_hist3.py and had the following contents: I have never seen a purple cow, And I never hope...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT