Read lines from the text file but cant do sum of numbers

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  
#include "stdafx.h"
#include<iostream>
#include<fstream>
#include<string>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	ifstream read;
	read.open("input.txt");
	double x;
	string line;
	if (read.is_open()){
		while (!read.eof()){
			getline(read, line);
				cout << line<< " " << "\n";
			}
		}
	read.close();
	system("pause");
	return 0;
}
Last edited on
Convert the strings to numbers with atoi or stoi.

EDIT: Sorry, those functions are for integers, there are float equivalents.

I've played with your code and added a second reader to parse the string. I'm posting the code to show you the correct way to process streams.

Also, this version allows you to have multiple values per line, not just one number.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

using namespace std;

int main(int argc, char* argv[])
{
	ifstream r("/input.txt");

	string line;
	while (getline(r, line))
	{
		istringstream sr(line);

		double x;
		while (sr >> x)
			cout << x << endl;
	}

	system("pause");
	return 0;
}
Last edited on
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;
	}
You need to sum the values read from the file or line yourself. Which is it, file or line?
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
If you're doing even and prime tests on the numbers, you're probably expecting integers rather than floating point numbers.

You'll need some way to test if a number is even and you'll need some way to test if a number is prime before you can update their respective sums.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
I'm not sure what you mean. What is your program supposed to do?
i wrote u PM
Topic archived. No new replies allowed.