wolnio
wolnio

Reputation: 103

How to read a number with commas from a file in C++?

Basically, I know how to read a number from a CSV file. But now a number that I've got to read has a different structure. Until now my numbers weren't separated by commas:

1000000;

I have to read a number that looks like that:

1,000,000;

In both examples, there are the same numbers (million). In the second one, the commas are there for better visibility.

So my question is, how can I read that number and save it to an integer? I thought that I should use RegEx, but I'm not good with it, so it's just an idea.

Upvotes: 3

Views: 199

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726479

You can use locale to process thousands separator, like this:

std::cin.imbue(std::locale(""));
int k;
std::cin >> k;

Demo.

Upvotes: 4

Related Questions