0

How would I implement this line in C++

scanf("%lf, %lf, %lf", &a, &b, &c)

ie: I want to get file data input that is separated by commas

Pyrons
  • 325
  • 1
  • 5
  • 12

3 Answers3

2

Solution 1:

std::cin >> a;
std::cin.ignore();
std::cin >> b;
std::cin.ignore();
std::cin >> c;

ignore() ignores a single character.

Solution 2:

Use a dummy variable for the comma:

char comma;
std::cin >> a >> comma >> b >> comma >> c;
Emil Laine
  • 41,598
  • 9
  • 101
  • 157
0
char comma1, comma2;
if (file_stream >> a >> comma1 >> b >> comma2 >> c &&
    comma1 == ',' && comma2 == ',')
    ...a b and c read & parsed successfully...
else
    ...error...

Remy Lebeau comments below that he thinks the functionally equivalent code below is easier for beginners to understand - I disagree but readers can make up their own mind:

char comma1, comma2;
file_stream >> a >> comma1 >> b >> comma2 >> c;
if (!file_stream.fail() && (comma1 == ',') && (comma2 == ','))
    ...a b and c read & parsed successfully...
else
    ...error...

If your "file data" is arriving on stdin as you've indicated in a comment, you can replace file_stream above with std::cin. If you want to use an actual file stream:

if (std::ifstream file_stream(filename))
    if (file_stream >> a >> ...as above...

Alternatively, this old answer codes up a Str class that allows usage like this:

if (file_stream >> a >> Str(",") >> b >> Str(",") >> c)
    ...a b and c read & parsed succesfully...
Community
  • 1
  • 1
Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
  • @RemyLebeau: sorry - I consider your edit a style choice I happen to disagree with - have reverted. Proof it works just fine as posted [here](http://ideone.com/F5yTY2). Cheers. – Tony Delroy Mar 18 '15 at 04:51
  • You misspelled `cooma2` in the last condition of the `if`statement. And the extra parenthesis make it clearer which conditions are actually being checked, especially around the `file_stream` operators, that the `file_stream` itself (which is the result of the last `>>` operator in the chain) is what is being checked for success/fail. And since the OP is clearly a novice, clearer code is better to learn from. I would probably break the `>>` operators out of the `if` statement: `file_stream >> a >> comma1 >> b >> comma2 >> c; if (!file_stream.fail() && (comma1 == ',') && (comma2 == ',')) ...` – Remy Lebeau Mar 18 '15 at 04:53
  • @RemyLebeau: sure - comment appreciated for readers' sake, and I'll add in your preferred style as an option for their consideration - they can make their own choice. – Tony Delroy Mar 20 '15 at 07:02
-2

Do you want to read data from file instead of standard stream? Then use fscanf

sameerkn
  • 2,209
  • 1
  • 12
  • 13
  • `fscanf()` is C. The question asked for C++. – Remy Lebeau Mar 18 '15 at 04:47
  • 1
    @RemyLebeau: I don't know what broken C++ compiler you're using... the Standard requires `fscanf`. – Ben Voigt Mar 18 '15 at 04:48
  • AFAIK, the C++ standard only requires that `fscanf()` and other C functions be wrapped in the `std` namespace, not that they actually be present. In any case, `std::fscanf()` would still not be the "C++ way" of parsing a formatted file. `std::(i)fstream` would be. – Remy Lebeau Mar 18 '15 at 04:50