Sum of numbers from string

closed account (EwqkoG1T)
Hello all ,this is my 1'st post here
So ,I have the following problem to solve
Given the file "stringfile.txt" I need to make the sum of all the NUMBERS in the string,for each row , and show on the screen the biggest sum!.For example , the 1st row of the file contains

;AC<A310067@5BC62>58=74441B;8C:;=@48;A0207;1A9??2?9332B?=@036@7A=?0662@?:02A:89;3<25;B67;06<0?CA30>2

so the program should take 310067 and 5 and 62.....etc and put them in a sum , and show the biggest sum from all the rows on the screen!
I guess this is a simple problem for most of u but I'm a begginer and I don't really know that many C++ instructions and can't figure out from other examples , how to do this problem.
This is what I wrote so far

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main ()
{
char s[256];
long x;
fstream f("stringfile.txt",ios::in);
f>>s;
{ other instructions I don't know
}

system("PAUSE");

}
...I tried with atoi but keep recieving an error , probably bcuz I'm not using it correctly...
So please if someone could do the complete program , and make it look noob-friendly :D.Ty!



Since you need to sum by "line" you should look up std::getline(). You can then parse the entire line string for sequences of "numbers" (perhaps by checking for isdigit()) and add them up until you parse the entire line. Once you complete the line you can check if the sum for that line is larger than the "high sum". Continue for all lines in the file.

EDIT: if you store the digits in a string you can pass it to atoi like : int number = atoi(mystring.c_str());
Last edited on
Topic archived. No new replies allowed.