Hello lorenzamaryan,
In addition to what
ne555 has said I see a couple of things.
if (in_stream.fail())
. Using
if (!in_stream)
will do the same thing with the added advantage that it will check all of the state bits. If the "eof" bit is set it does not set the "fail" or "bad" bits.
Also do not use "exit()" use "return 1" instead since you are in "main".
jlb wrote: |
---|
You really should try to avoid the C function exit(), it doesn't know about C++ classes and can cause data loss if used inappropriately. Since this is C++ start thinking about using exceptions when you need to "abort" the program.
|
I also noticed that most people tend to write
sumx = sumx + x[i];
. Lets say the "sum" = 3 and "x[i]" = 1 with the result expected being 4. The first code would do this. What you have written
sumx += sumx + x[i];
would be 3 + 1 = 4 then add this to "sum",(3) which gives you a result of 7. All you need here is
sumx += x[i];
. And the same is true for the other 3 lines in the for loop.
Try that and see if it does not work better.
Andy
Edit:
If you include your input file, or a good sample, everyone can be using the same information to work with.