0

C++ noob here. I'm working on a program which needs to include the ability to read data from an input stream, then separate the data into two separate arrays based on the type of data read (an integer array and a string array).

The data in the input files is presented in the following format (without the spaces in between the lines):

5000 Leather Seats

1000 DVD System

800 10 Speakers

etc.

I need to separate the prices (numeric values at the beginning of each line) from the description of each (the rest of the contents of each line) into two separate strings ("prices" and "options"). Could someone point me in the right direction to find the most efficient way of reading the appropriate data from each line into its respective array?

Community
  • 1
  • 1
Ben Bowald
  • 19
  • 3
  • 3
    You should find plenty of examples of this kind of a task [in a good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Sam Varshavchik Apr 02 '18 at 04:57
  • This is not based on the type of data read, but on input format. – n. m. could be an AI Apr 02 '18 at 04:58
  • https://stackoverflow.com/questions/7868936/read-file-line-by-line first answer option 2 gets you going and includes all of the bits and bobs needed to implement the rest. – user4581301 Apr 02 '18 at 05:24

1 Answers1

0

Probably the easiest way to do this is use getline to get the full line as a string, then uses substrings to split it up, then add to the arrays. for example:

string s;
getline(cin,s);
string price = s.substr(0, s.find(" "));
string type = s.substr(s.find(" ")+1, s.length());

add price and type to respective arrays.

nikosm
  • 106
  • 1