Getline - Reading In Formatted Data with Multiple Delimiters C

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

I m trying to read in data from command prompt with multiple delimiters, for example:

data 1, data2, data3.

I d like the code to read in the data before the first comma, the data after that and before the second comma, and finally the data after that but before the period. I have been using this:

getline(cin, VAR,  , );
cin.get();
getline(cin, VAR2,    );
getline(cin, VAR3,  , );
cin.get();
getline(cin, VAR4,  . );

And it does what I want, provided the information is entered correctly. However, how can I check and see if the information wasn t? Because if the user doesn t enter two commas and a period, the program gets stuck as I m assuming the cin stream gets broken after not finding the delimiter as specified in the getline() read. I ve tried this:

if (cin.fail()) {
        cout << "failure" << endl;
}

After a getline, but it never runs if the delimiter isn t input. What s the best way to do this, check for errors and see if data wasn t entered in the desired format?

Answers

You have to parse the input yourself.

For starters, your input does not consist of "data with multiple delimiters". Your input consists of a single line of text, that was entered, according to your description, at a command prompt of some kind.

If so, then the line of text should be read with a single std::getline() library function, since, by default, std::getline() uses

as a default delimiter.
std::string str;

std::getline(std::cin, str);

Now, you ve read your input into str. Now that this task is complete you can get down to business verifying whether str contains properly formatted input with the appropriate delimiters, or not.

To do that, you will use the rich set of methods that are available from your std::string object. For example one such method, find(), searches the string for a particular character, such as your first comma:

auto n=str.find( , );

The full documentation for find(), whose description you will find in your C++ book, will explain how to determine whether find() actually found the comma, or not. Based on that your program can either take the appropriate action (if the comma was not found) or use the substr() method, on this str, to extract the part of the string before the comma. That would be your first VAR. Then, use substr() again to extract the rest of the entered text, after the first comma.

You will also find a complete description of substr() in your C++ book.

After using substr() to extract the rest of the entered text, after the first comma, you will simply repeat the same overall process for extracting data2, and the following item of input, this time using a period.

And, at each step of the way you will have all the information your program will need to determine whether or not your input was formatted correctly.

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/42184312/reading-in-formatted-data-with-multiple-delimiters-c

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils