I can't get past this one roadblock in my program. In my program i need to have a for loop function that will read read data from a given data file into arrays (1d array students names and 2d array student scores) however, within the data file, there are commas that serve as delimiters and i must find a way to bypass them.
I was wondering if someone could give me a fix to this?
here is the data file (.txt)
Mary Peterson, 95, 93, 77, 94, 77,
Jake Andersen, 90, 90, 95, 93, 49,
Susan Cooper, 79, 94, 44, 90, 73,
Mike Smith, 95, 93, 30, 79, 97,
Jim Blair, 53, 45, 97, 39, 59,
Clark Lee, 70, 95, 45, 39, 77,
Kennedy Davis, 77, 34, 55, 74, 93,
Kim Bronson, 93, 94, 99, 77, 97,
Sunny Hill, 79, 95, 59, 93, 95,
Sam Benson, 95, 75, 49, 75, 73,
thanks! I'm really new to the language and my professor hasn't gone over the uses of #include <stdlib.h> and #include <sstream>, if you wouldn't mind explaining where and why you used those preprocessors that would be great
#include <stdlib.h> here is only used for system("pause"); so that the console windows doesn't close automatically. If you don't use Visual Studio you might not need it.
#include <sstream> is needed to use istringstream. istringstream is a class used to easily get input from a string.
no not yet, i'm trying to extract the data from the txt file into the arrays.
also another quick question, what did sep do at line 39? i can see you made it to char, and then at line 46 you have it get rid of the comma. i don't really understand how that really worked out.
It's actually quite simple once you understand the principle string input = "Mary Peterson, 95, 93, 77, 94, 77,";
first you read the name until you find a comma getline(iss, name, ',');
name contains Mary Peterson
then you read the number until the next comma and store it in number. iss >> score[i];
then you have to remove the comma with iss >> sep;
To read the file you basically read the file line by line and extract the data from the line as shown above. Do you know how many lines the file contains ?