I changed the code a bit. Now i can read the numbers but i get wring sum.
Because the program sums 100 + 34 +4?+67+7?+7+5+5?+63+3?+6+1 =297 while it should be 100+34+67+7+5+63+6+1=283??. I can se that for every space it adds the last number? how to i stop that??
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;
}