What is wrong exactly in this program?

Hello, here I am today with another problem facing me with this question I've found, Lol I guess I better stop solving questions that are not given to me in the university, so here's the question:

Assume that the following data is stored in file “data.txt”:

2 5 7 1 12 -999

1 4 11 55 66 3 43 23 -999

9 87 56 34 22 12 21 -999

8 7 5 4 6 111 -999

The number -999 at the end of each line acts as a sentinel value and is not part of the data. Write a C++ program that reads these numbers from a file of unspecified length, calculates and displays the sum of all numbers in each line.



honestly, I cannot understand what do they mean with the " file of unspecified length" and how to do the sum for only each line, but here's a code of what I've tried to do , I get no errors, and 1 succeeded but the debug says
"I CANNOT SEE THE FILE "


I'd be delighted if I can get a few hints or corrections for the code, I guess just because I'm a freshman I'm all excited and solving all those questions and all :P , in advance thank you =)

here's the code :
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
40
41
42
43
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
	int n;
	int x,y;
	int sum=0;
	ifstream data;

	data.open("data.txt");
	

	if (! data.is_open () )
	{

		cout<<"I cannot see the file"<<endl;
	}
	else
	{
		while(! data.eof())
		{ 
			for(x=1;x<-999;x++)
			{
				for(y=1;y<=x;y++)
				{
			data>>n;
			sum=sum+n;
			cout<<"The sum is"<<sum+n<<endl;
				}
			}

		}
	}

	data.close();
	

	system("pause");



}

This loop

for(x=1;x<-999;x++)

is invalid because it will be iterated zero times.:)

I think that all can be done simpler.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int sum = 0;
int x;
int count = 0;
while( std::cin >> x )
{
   if ( x != -999 )
   {
      sum += x;
   }
   else
   {
      count ++;
      std::cout << "sum of numbers in " << count << " line is " << sum << std::endl;
      sum = 0;
   }
}   


Last edited on
I'm sorry, I can't really understand your code, and I've tried it but the debugging gives me a blank black window !
1
2
3
4
5
6
7
8
9
for(x=1;x<-999;x++)
			{
				for(y=1;y<=x;y++)
				{
			data>>n;
			sum=sum+n;
			cout<<"The sum is"<<sum+n<<endl;
				}
			}


The for loop is not going to be executed because x is not less than -999. That's the problem.
@abbzi
I'm sorry, I can't really understand your code, and I've tried it but the debugging gives me a blank black window !


You should include the code in your program instead of your loops.
okay from the very begging, can someone explain the question briefly ? I think the code is incorrect because of my misunderstanding to the question.
Assume that the following data is stored in file “data.txt”:

2 5 7 1 12 -999

1 4 11 55 66 3 43 23 -999

9 87 56 34 22 12 21 -999

8 7 5 4 6 111 -999

The number -999 at the end of each line acts as a sentinel value and is not part of the data. Write a C++ program that reads these numbers from a file of unspecified length, calculates and displays the sum of all numbers in each line.


Essentially it's saying all of these numbers are stored in a text file as shown. That's easy.

The statement at the bottom says that the "-999" value is supposed to represent the end of line, it should not be included when doing your calculations. Fairly Easy.

The next part of that statement says that this file could be infinitely large. This means you need a way of storing all of that data, I suggest vectors. Getting a little more complicated.

The last part says to take the data from each line and add it all together. Back to being easy.

For vectors, I suggest using this: http://cplusplus.com/reference/stl/vector/
If you can use an array, you're half way to using a vector.

For File I/O, I suggest: http://cplusplus.com/reference/iostream/fstream/
If you can cout/cin, you're halfway there with using fstream.

Edit: Have you managed to get your other program solved yet? I'd try sticking with one at a time, learn from each. Get better at each part, and make sure you understand everything you did and why.
Last edited on
Thank you all so much, I started to work a bit with the vectors from the links you've sent me previously, But it's kind of new because we still did not take that in programming 1, I guess next course or something, but it's pretty good to keep learning in advance, thanks again =)

And actually after spending a lot of time staring in my code, and going back to some tutorial pages I've finally solved the question right!
and here is how I changed everything, the solution was pretty simple,based on some basics I guess its just needed more effort and concentration.

this is the new code:

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
40
#include <iostream>
#include <fstream>
using namespace std;
void main()
{
ifstream INFILE;
int num;
int sum;
int count;
count =1;

INFILE.open("data.txt");


  if(!INFILE.is_open())
  {
cout << "Sorry,The file is not opened,Please try again.Thank you"<<endl;
  }

   while(count <= 4)
	   {
		   sum = 0;
INFILE >>num;
   while(num!=-999)
   {
	sum =num+sum;
	INFILE>>num;
   }
cout<<"The sum of the " <<count<<"  line =" <<sum<<endl;
count=count+1;
   }


INFILE.close();


system ("pause");


}



Thank you so much!
But I'm still trying with the code from the previous topic, things are refusing to be fixed over there lol,
Yeah, I overthought the application a little too much, sorry for the confusion, but glad you figured out the file stream.
No apologies =)
Thanks !
Topic archived. No new replies allowed.