Reading data from a long string

Hi, I looked through the sections on getline() and ifstream/ofstream but couldn't couldn't figure out a solution to this question:

Given a string in a file:

Smith,John:20$1239Lee,Ann:45:$8544Howard,Bob:27$15900

Where "Smith, John" is the last and first name, "20" is age and "$1239" is the amount in their checking account. How do you read this line and sum up the total amount in all these customers' bank accounts? I got as far as this:

int main () {
int num;
string str;
ifstream accounts ("checking_accounts.txt");
getline (accounts, name, ":");
getline (accounts, age, "$");

}

And couldn't figure out how to get the dollar amount without reading the next person's name.

I'm trying to do this without using arrays/vectors, as my course has not covered these topics yet. Thanks for any feedback!
Last edited on
maybe you could use cin.get() with loops and a peek() function. I've never used peek(), so I don't know exactly how it works, but the reference section on this sight could explain how.
Hm. I would suggest just using operator >> here since you know the next part is a number and that operator will stop when it reads a non-number character.
If you know the part after the $ is going to be a number, you could simply read it into an int normally and it will read up to but not the next name.
firedraco, Zhuge +1

Whoever it was that generated that file screwed up.
Thanks!
Topic archived. No new replies allowed.