# Thread: series of lines in text Get the Integer and Sum

1. ## series of lines in text Get the Integer and Sum

Instructions : Read a series of lines from a text file, get the numeric values in each line, perform addition and display sum

and this is my code

Code:
```#include <iostream>
#include <fstream>
using namespace std;

int main() {
int sum = 0;
char x[50];
double d;

ifstream inFile;
inFile.open("input.txt");

while(inFile.getline(x,50)) {
cout<<x<<endl;
d=atof(x);
sum+=d;
cout<< sum << endl;
}
}

system("pause");
}```
and the text from the file is

hello51.20world10.00again
40programming
file 20editsearch50 project.15

the output should be

hello51.20world10.00again
61.2

40programming
40

file 20editsearch50 project.15
70.15

can u help me? just give me the algorithm to sum and the string function to use

2. There is not "a" string function to use. You'll probably have to go through your string, character-by-character, looking for characters that are digits. Then when you find one, you'll have to somehow get that number out of there and add it to your total. (You should also probably set total to zero before you start doing this.)

3. can u give me a sample what you mean? plz

4. You can't start atof at the beginning of the string (necessarily). You have to find your number first -- you need to go through the characters checking whether each character is a digit in order to know where you should call atof.

5. Originally Posted by tabstop
You can't start atof at the beginning of the string (necessarily). You have to find your number first -- you need to go through the characters checking whether each character is a digit in order to know where you should call atof.
Maybe, stringstreams would be simpler.
...Ignore when a character is there.
...Add to the sum when a double is there.

6. Can you use external libraries ? Maybe I'm too damn lazy but I see an application for boost::tokenizer there.

7. Originally Posted by Alexandre
Can you use external libraries ? Maybe I'm too damn lazy but I see an application for boost::tokenizer there.
where? need help

8. I don't think a student is meant to be messing with boost at this stage.

cctype (ctype.h) - C++ Reference
Perhaps more a case of using isalpha() and isnum() to test each char (in a loop) and go from there.