Create a function named ParseScores, which takes a string input filename as a pa
ID: 3599656 • Letter: C
Question
Create a function named ParseScores, which takes a string input filename as a parameter, an integer array, and size of the array. The function will read the student scores on each line of the file and store the sum of the values into another integer array. This function returns the number of entries read from the file. If the input file cannot be opened, return -1 and do not store anything in the array. Each line of the file contains , , . If given the data below: 67, 51, 85 78, 56, 75 96, 82, 60 Your function should return 3 and output to the array should contain: 203 209 238 You only need to write the function code. Our code will create and test your function. HINT: We have provided a function that may make the parsing easier: int split(string s, char sep, string words [1, int max_words);Explanation / Answer
if the provided function split() by the question , is right
here is your code
int ParseScores(string filename, int array[], int n) // specify the parameters
{
int c=0; // declared c for calculating the sum of each row
string words[3]; // words is a type of string , array of three strings
ifstream file; // create an object of the input stream , ie ifstream for reading
file.open(filename.c_str()); // open the file
string line; // line declared as string
int j=0; // j for get the number of rows
if(file.is_open()) // check whether the file is openable or not
{
while(!file.eof()) // till the end of the file
{
getline(file,line); // getline() method will take the entire line into line
split(line, ',', words, 3); // split function that is a user defined function given by your teacher
for(int i=0; i<3;i++) // create a for loop of 3 times , ie only 3 values per row
{
c=c+stoi(words[i]); //stoi string to integer
}
array[j]=c; // store the value c ie sum in the array
j++; // increment the value of j for next row
}
return j; // j has the number of rows , that is returned
}
else
{
return -1; // return if the file is not openable
}
}
still the working of the split() function is unknown there may be some errors then try this
you can done it without the help of the split() function ie
#include <iostream>
#include <fstream>
using namespace std;
int ParseScores(string filename, int array[], int n)
{
int c=0;
string words[3];
char a;
ifstream file;
file.open(filename.c_str());
string line;
int j=0,p;
if(file.is_open())
{
while(!file.eof())
{
for(int i=0;i<3;i++)
{
file>>p; // get the integer
file>>a; // just read the character ie ,
c+=p; // update c as the sum ie c=c+p p hold the integer value
}
/*
getline(file,line);
split(line, ',', words, 3);
for(int i=0; i<3;i++)
{
c=c+stoi(words[i]);
}
*/
array[j]=c;
j++;
}
return j;
}
else
{
return -1;
}
}
int main()
{
string filename;
int array[20],n;
cout<<"Enter file name ";
cin>> filename;
n = ParseScores(filename,array,20);
if(n>0)
{
for(int i=0;i<n;i++)
{
cout<<array[i]<<" ";
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.