• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Some help with intro C++ needed (please)!

Antoneo

Diamond Member
Hey guys, I've been stumped for a bit on this and would greatly appreciate it if someone can help me out 🙂.

Ok, let's say that there is a text file that contains the following:

123 Apples, Oranges
456 Red, Yellow
789 Email, AIM

Assume that I have correctly connected the i/o filestreams and the rest of the program is there. I just need the logic? or functions to get it working.

How do I read in only the number part of the data in the textfile? Ie. How do I sum the three numbers (123, 456, 789) while ignoring the rest of the text?

Headers to use are:
<string>
<fstream>
<iostream>
Nothing too fancy. Thanks!

EDIT: Perhaps 2 dimensional arrays are useful here?
 
if all you need are the numbers you could use a loop that ifstream's the number, then runs a getline() to discard the rest of the line. Just do that for each line you need to read in. Of course, if you need to save the rest of the data, just store the return value from each getline() call😛

edit: replaced cin with ifstream, forgot we are working with files😉
 
Back
Top