Add values

Pages: 12
Ah alright thanks catfish didn't realize it was only binary.
Last edited on
@giblit: http://www.cplusplus.com/forum/beginner/121876/#msg664286
The second code segment is offensive to me. I hope I do not need to lecture you on why, I would assume you know and just forgot ;)
Catfish,
Is integer x supposed to represent both of my numbers?
slour wrote:
Is integer x supposed to represent both of my numbers?

Yes, more generally, it's supposed to represent all numbers on a line.
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
30
31
32
33
34
35
36
37
38
39
int main(){

		ifstream indata;
		ofstream outfile;
		float num1 = 0, num2 = 0, num3=0, x;
		string line;

		indata.open("intinput.txt");
		outfile.open("results.txt");

		if (!indata) {
			cerr << "Error file cannot be opened!" << endl;
			exit(1);

		}
		indata >> num1 >>num2;
		while (getline(indata, line, '\n'));
		{
			stringstream num1(line);
			while (num1 >> x)
				num3 += x;
		}
		while (getline(indata, line, '\n'));
		{
			stringstream num2(line);
			while (num2 >> x)
				num3 += x;
		
			outfile << "The number added is:" << num3+num3 << endl;
			
		}


		indata.close();
		outfile.close();
		cout << "End of file reached..." << endl;
		return 0;

}

This is what I have now, I think i'm on the right track, but all i'm getting is one line of return and it is 0. Where am I going wrong?
> while (getline(indata, line, '\n'));
First, note the semicolon at the end. That's the body of the loop.
Second, ¿what do you expect line 23 to do? You've already exhausted the file in line 17
For line 23 I thought I needed to go again to pull in the second value to add the two together. So i take it that is not the correct thing to do then?
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
30
31
32
33
34
35
36
37
int main(){

		ifstream indata;
		ofstream outfile;
		float num1, num2, num3=0, x=0;
		string line;

		indata.open("intinput.txt");
		outfile.open("results.txt");

		if (!indata) {
			cerr << "Error file cannot be opened!" << endl;
			exit(1);

		}
		indata >> num1 >>num2;
		while (getline(indata, line, '\n'))
		{
			stringstream num1(line);
			while (num1 >> x)
				num3 += x;
		
			stringstream num2(line);
			while (num2 >> x)
				num3 += x;
		
			outfile << "The number added is:" << x+x << endl;
			
		}


		indata.close();
		outfile.close();
		cout << "End of file reached..." << endl;
		return 0;

}

So I made the changes and am getting results for each line now, but they are not correct. Anymore suggestions?
Last edited on
I'll give you a hint: you never needed to modify the code I gave you.

I'll give you an explanation: it reads the input file line by line, and each time it reads a line it extracts each number from the line as x (so x becomes a, b, c, d, e, ..., although in your case just a and b).

I'll give you another hint: you don't need to read anything outside the while() loop.
1
2
3
4
5
6
7
8
		indata >> num1 >>num2;
		while (getline(indata, line, '\n'))
		{
			stringstream num1, num2(line); '\n';
			while (num1,num2>>x)
				num3 += x;
		
			outfile << "The number added is:" << num3 << '\n';

so like this? Because my output doesn't add up like it should.

My first value is coming up 0(zero), my second is the addition of the first line with the first digit of the second line, and I can't seem to find where I'm having my problem at. Any help would be greatly appreciated.
Last edited on
Does anyone have a link to where I can read about counters?
Look at line 4 very clearly...I have no idea what that is supposed to do.

Basically what you want to do is create a stringstream from the line you read in then stream each set of numbers and add them.


Catfish already showed you how to use it but here it is again:


1
2
3
4
5
6
7
8
while( getline( input , line ) ) //loop while it reads in lines
{
    stringstream numbers( line ); //add the line to a stream
    int x , sum = 0; //sum is the sum of each line
    while( numbers >> x ) //convert each line into integers
        sum += x; //add the values to the sum 
    out << sum << '\n'; //output the sum of each line
}


Topic archived. No new replies allowed.
Pages: 12