Hi guys I have som issues...I read by lines from a text file which contains numbers(int,double). But it's okay my task is to do sum of even numbers in each lines.Here is my code thanks a lot. Problem is that I cant do sum of even numbers cauz I read lines which are string ..and I dont have access to each number
thanks a lot mate, now if I want to use read values from text file I have an access but in which loop cauy that part of code isn't very clear for me cauz I need to do sum of even numbers in each line
1 2 3 4 5 6 7 8 9
istringstream sr(line);
double x;
while (sr >> x)
cout << x << endl;
}
here is my task what I have to do :
- sum of all even numbers in each line of the file
- sum of prime numbers in each line of the file
I know all of that algorithm but IDK how to have access separately to each number in lines ..srry for my bad english
int _tmain(int argc, _TCHAR* argv[])
{
ifstream read;
read.open("input.txt");
int x;
string line;
int even= 0;
int nEven= 0;
if (read.is_open()){
while (getline(read, line)){
istringstream sRead(line);
while (sRead>> x){
if (x % 2 == 0){
even+= x;
}
else{
nEven+=x;
}
}
cout << "Sum of even numbers is: " << even << endl;
cout << "Som of not even numbers is: "<< nEven << endl;
}
}
read.close();
system("pause");
return 0;
}
here is my code but the problem is it just counts only numbers in the first line correctly ..Numbers on the next line are added to the first line... I am lost need a little bit help thanks a lot