So, here is my problem. I need to do a ifstream of the variables down below, but I don't know how to do it since the variables are varying in length such 2 to 3 letter longs with spaces in between and there is a comma. Also I cannot change the variables, and it must be kept the same. If everything was standardized I could've done
fileIn >> name >> num >> item >> num >> cost >> totalPrice;
But since the names are varying in length such as 2 to 3 letters long, I thought I needed to do a getline, but I have no idea how to attach the rest of the information after the getline. Since if I did getline(fileIn, name); it would have gotten the entire line, which is wrong since I need to add up the item numbers and total price of the contributing farms.
I have no previous background in C++, so please dumb it down as you would explain it to a highschooler.
variable format is
farm name, item number, item, item cost, total price.
Actually I would do a getline. Then using string.erase find the first comma, Copy everything on the left to the first value and the rest of the line to the second value. Then look for the space and once again, copy the right to the next value and so forth.
Use the two-parameter form of getline(same reference) to read the rest of the line into a string. Then use a stringstream (internal stream) to stream this into the other variables which are only separated by spaces.
If there is a possibility that your data lines are followed by a blank line (as here) then you can rid the input buffer of the unwanted white space (including newline) by using ws (http://www.cplusplus.com/reference/istream/ws/)
Farm: Collins Farm
Number: 43900
Name: tomatoes
Unit cost: 0.67
Total price: 29413
Farm: Bart Smith Farms
Number: 34910
Name: cassavas
Unit cost: 0.99
Total price: 34560.9
If you wanted to take this further, then, since all your data fields are heavily related, you might want to (a) create a struct to hold them; (b) overload the stream operators << and >>, which makes subsequent coding a little tidier.