0

I'm new to coding. I got this assignment that I almost completed but stuck on the last step. My teacher gave us positive integer file. A task is to find a total for each line and output total for a line and also total for all of the numbers in the file. I got line total working fine, but when I try to do a total of all the numbers I get the wrong answer.

Please review my code and tell me what am I doing wrong.

Here's the code:

#include <iostream>
#include <string>
#include <fstream>
#include<iomanip>
using namespace std;

int main()
{
ifstream infile; ofstream outfile;
int counter, counter2, line, sum, num;
double total;
infile.open("E:\\Lab assignments\\numberfile.txt");
if (!infile)
{
cout << "File not found, closing program" << endl;
system("pause");
exit(1);
}
outfile.open("E:\\Lab assignments\\outfile.txt");
if (!outfile)
{
cout << "Output file could not be created, closing program" << endl;
system("pause");
exit(1);
}


while (!infile.eof())

for (counter = 1; counter <= 8; counter++)
{
sum = 0;
if (counter != 9)
{
for (line = 1; line < 8; line++)
{
infile >> num;
outfile << right;
outfile << setw(6);
outfile << num << " ";
sum = sum + num;

outfile << " ";


}
outfile << "Total per line = " << sum;
outfile << endl;



}




}
total = 0;
for (counter2 = 0; counter2 <= 8; counter2++)
{

{
infile >> sum;
total = total + sum;
}

}
outfile << "All numbers total= " << total;

infile.close();
outfile.close();
return 0;


}

and file with numbers:

346 130 982 90 656 117 595
415 948 126 4 558 571 87
42 360 412 721 463 47 119
441 190 985 214 509 2 571
77 81 681 651 995 93 74 
310 9 995 561 92 14 288
466 664 892 8 766 34 639
151 64 98 813 67 834 369
halfer
  • 18,701
  • 13
  • 79
  • 158
Bob Smith
  • 17
  • 1
  • Possibly unrelated to your problem, but maybe not: Read [Why is iostream::eof inside a loop condition considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – Some programmer dude May 03 '18 at 07:35
  • 1
    Also, please [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask), read e.g. [this Stack Overflow question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/), and edit your question to include the expected and actual output. And please [learn how to debug your programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) as it should help you find and fix problems like this. – Some programmer dude May 03 '18 at 07:37
  • code that is reading and writing to/from files is easiest debugged by not writing/reading to/from a file ;). Start by hardcoding the input into your code, this would also enable us to reproduce your problem. See also [mcve] – 463035818_is_not_a_number May 03 '18 at 07:45
  • Inside the first loop, `counter != 9` is always true. – molbdnilo May 03 '18 at 07:46
  • When your first loop has read to the end of the file, what do you expect the second loop to read? (You only need one loop.) – molbdnilo May 03 '18 at 07:49
  • 1
    This seems to still need an [mcve]. – halfer May 13 '18 at 09:51
  • Compile with all warnings and debug info: `g++ -Wall -Wextra -g` with [GCC](http://gcc.gnu.org/). Improve the code to get no warnings. [use the `gdb` debugger](https://sourceware.org/gdb/current/onlinedocs/gdb/) to understand the behavior of your program. Improve it. Repeat all till satisfied. – Basile Starynkevitch May 13 '18 at 09:52

1 Answers1

1

Computation of total sum and sum for each line in the same loop will significantly simplify your task and it will remove hidden problems in your code. Be simple :)