Question

C++ Write a function parseScores which takes a single input argument, a file name, as a string. Your function should read each line from the given filename, parse and process the data, and print the r...

C++

Write a function parseScores which takes a single input argument, a file name, as a string. Your function should read each line from the given filename, parse and process the data, and print the required information. Your function should return the number of student entries read from the file. Empty lines do not count as entries, and should be ignored. If the input file cannot be opened, return -1 and do not print anything.

    Your function should be named parseScores
    Your function should have one input argument: a file name, as a string.
    Your function should return an int, representing the number of students' entries read from the file.
        If the given file name can not be opened, your function should return -1.
        Empty lines in the file should not be counted as entries.
    Each line of the file contains four elements, separated by commas (,)
        SCORE,SCORE,SCORE,STUDENT NAME
        After parsing each line, you should print:

        STUDENT NAME: AVG SCORE

        Where AVG SCORE is the average of the three scores for STUDENT NAME. The three scores may be treated as floating point numbers.

Note: 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)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

SOURCE CODE:

//Please read comments carefully and alter the program if required thank you
//This function parses the input file with given filename.
//Pleae make modifications if any required.
//Assuming that split function exists and required header string,iostream,ifstream are included this works fine.
//Please read comments for detail description
int parseScores(string filename)
{
//Values declared and initialized requirements mentioned.
string words[4]; //To store words from split function.
int i,val,count=0; //To store the no. words returned and count of lines parsed.
float avgscore=0; //This is to store avergae score calculated.
ifstream f(filename); //filename given to read into f
if (f.is_open()) { //checks if it is opened if no returns -1.
string line; //variable to read line
while (getline(f, line)) { //reads a line by line until end.
val=split(line,',',words,4); //split function provided in question.
if(val==4) //if given line is correct then it executes.
{
for(i=0;i<3;i++) //this averages the score
{
  
avgscore=avgscore+stoi(words[i]); //converts string to integer and then sums it up
}
avgscore=avgscore/3;
cout<<words[4]<<" : "<<"avgscore"; //prints name and avgscore.
count++; //calculates the count of totla lines parsed
}
else
{
return count; //if line found in non-format terminates function by returning parsed lines
}
}
}
else
{
return -1; //if file open fails.
}
f.close(); //closes the file.
return count; //returns the count.
}

Add a comment
Know the answer?
Add Answer to:
C++ Write a function parseScores which takes a single input argument, a file name, as a string. Your function should read each line from the given filename, parse and process the data, and print the r...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Only. Don't use other libraries other than #include <iostream> and using namespace std; Write a...

    C++ Only. Don't use other libraries other than #include <iostream> and using namespace std; Write a function maxTemp which takes a filename as string argument and returns the maximum temperature as float type for the week. Input file will contain temperature readings for each day of the week. Your function should read each line from the given filename, parse and process the data, and return the required information. Your function should return the highest temperature for the whole week. Empty...

  • write a function which takes a string argument and a character argument. It should return a...

    write a function which takes a string argument and a character argument. It should return a truth value (int 0 or 1), 0 if the string does not contain the character, and 1 if the string does contain the character. Do not use a built-in library for this. Again, call this function and its variables whatever you deem appropriate. The main function of the program should accept a single character followed by Enter. Then, it will read lines until the...

  • python 3 HW11.2. Read a file and print its lines The function takes a single string...

    python 3 HW11.2. Read a file and print its lines The function takes a single string parameter, which is the name of a file. Complete the function to open the file for reading, read the lines of the file, and print out each line. The function takes a single string parameter, which function to open the file for reading, read the lines student.py def print_lines of file (filename): 1

  • •Write use pytohn a function File2List(filename) that: –Read line by line from a file –For each...

    •Write use pytohn a function File2List(filename) that: –Read line by line from a file –For each line of words, –change all characters to lowercase –remove all the punctuations –remove whitespaces except the blank – ‘ ’ –split the words within the line by the blank and form a list of separated words –Merge all the list of separated words together and the function should return a wordlist of individual words

  • Help please Write a program named one.c that takes a single command-line argument, the name of...

    Help please Write a program named one.c that takes a single command-line argument, the name of a file. Your program should read all the strings (tokens) from this file and write all the strings that are potentially legal words (the string contains only upper-case and lower-case characters in any combination) to the file words. Your program should ignore everything else (do not write those strings anywhere 1. As an example, running /a.out dsia would result in the generation of the...

  • Capitalization JAVA In this program, you will read a file line-by-line. For each line of data...

    Capitalization JAVA In this program, you will read a file line-by-line. For each line of data (a string), you will process the words (or tokens) of that line one at a time. Your program will capitalize each word and print them to the screen separated by a single space. You will then print a single linefeed (i.e., newline character) after processing each line – thus your program will maintain the same line breaks as the input file. Your program should...

  • help Question 12 (20 points) Write a function named inverse that takes a single parameter, a...

    help Question 12 (20 points) Write a function named inverse that takes a single parameter, a dictionary. In this key is a student, represented by a string. The value of each key is a list of courses, each represented by a string, in which the student is enrolled. dictionary each The function inverse should compute and return a dictionary in which each key is a course and the associated value is a list of students enrolled in that course For...

  • Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text...

    Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text file by removing all blank lines (including lines that only contain white spaces), all spaces/tabs before the beginning of the line, and all spaces/tabs at the end of the line. The file must be saved under a different name with all the lines numbered and a single blank line added at the end of the file. For example, if the input file is given...

  • Write a complete C++ program that reads students names and their test scores from an input...

    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...

  • For your second program, please read the data from the input file directly into an array....

    For your second program, please read the data from the input file directly into an array. (You may safely dimension your array to size 300.) Then close the input file. All subsequent processing will be done on the array. Your program should have two functions besides main(). The first function will print out the contents of the array in forward order, 10 numbers per line, each number right justified in a 5 byte field. The second function will print out...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT