Hello,
im trying to make a program that reads from a file and sums up only the numbers.the numbers ar aranged like this in the file:
100er34 67 yyy 7
5 63 6
1
My problem is that the program sums up wrong,it sums some numbers twice when it see space.its supposed to sum like this:100+34+67+7+5+63+6+1=283 But it sums like this 100 + 34 +4?+67+7+7?+5+5?+63+3?+6+1 =297. Those with ? is unwanted the. What kan i do?
Thanks
Code://Oppgave 2 Øving 1 #include <iostream> #include <fstream> #include <cstring> #include <cstdlib> using namespace std; /* oppgave2.txt contains 100er34 67 yyy 7 5 63 6 1 */ int main(){ ifstream inputFile; char input[81]; char input2[81]; char input3[81]; char test[] = "0123456789"; char *ptr; int sum = 0; //cout<<"her"; inputFile.open("oppgave2.txt"); if(!inputFile){ cout<<"File error\n"; } else{ //inputFile>>input; while(!inputFile.eof()){ inputFile.getline(input,81); ptr = strpbrk (input, test); while(ptr != NULL ){ cout<<*ptr<<" "; sum += atoi(ptr); cout<<"sum: "<<sum<<" "; ptr = strpbrk(ptr+1,test); } } cout<<"\n sum:"<<sum; } inputFile.close(); return 0; }



LinkBack URL
About LinkBacks


