Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Ok, so I am suppose to open, read, and print information from a data file to the

ID: 3643080 • Letter: O

Question

Ok, so I am suppose to open, read, and print information from a data file to the screen (I can do that). Then I am suppose to take only certain information from that file to use for another function in my program. This is what I am using to read and print the file to the screen:

while(getline(fin, line))
{
cout<<line<<endl;
}

I know that I need to break each line into "words". I can't seem to get it right.

For example, if each line of data in the file states a course number, credit hour, and letter grade, and I only want to pull out the letter grade so I can convert it into a number grade, how can I do this?






Explanation / Answer

This should help you out. It splits a sentence into words and stores the words into a vector. #include #include #include #include using namespace std; vector &split(const string &s, char delim, vector &elems) { stringstream ss(s); string item; while(getline(ss, item, delim)) { elems.push_back(item); } return elems; } vector split(const string &s, char delim) { vector elems; return split(s, delim, elems); } int main() { string s = "hi my name is john smith"; vector v = split(s, ' '); for(int i=0; i
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote