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 that takes, as an argument, the name of a file, fileName . Your...

    Write a function that takes, as an argument, the name of a file, fileName . Your program should open (and read through) the file specified, and return the maximum value in the file. Name this function maxValueInFile(fileName). def openFile(): # Prompt for the file name filename =input("Enter a file name: " ) # Open the file for reading file = open(filename) # Prompt for the target value target =input("Enter a threshold value: ") # Initialize the counter counter = 0...

  • 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: Write a function that takes, as an argument, the name of a file, fileName, and...

    PYTHON: Write a function that takes, as an argument, the name of a file, fileName, and an integer n between -750 and 750 (inclusive). Your program should verify that n is an integer in the correct range. If it is not, it should return the string “Your integer is out of range.” If it is in the correct range, your program should open (and read through) the file specified, and return the number of values in the file that are...

  • 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

  • c++ Write a function Count_m_z that takes a string as an input parameter argument and counts...

    c++ Write a function Count_m_z that takes a string as an input parameter argument and counts the number of m, n, o, ... z characters in it. The function returns an integer value for the number of times those 14 lowercase letters appear in the input string. Your function should be named Count_m_z Your function should take one string parameter Your function should return the number of m, n, o, ... z characters as an integer Your function should not...

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

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