Hey everyone

Maybe someone can help me with this.
We are supposed to create a program that will read a .dat file and then output a new file containing the total number of rows in the data file, the sum of integers and the number of occurrences of the number zero. The dat file basically has 1000 rows and in each row there are 5 integers.

I did the first two parts but I'm not sure about the last part regarding the zeros.
I actually went through and counted the number of rows. There are 1000 of them. However, my program outputs only 406 instances of zero whereas I counted manually over 500 occurances of zero so something is wrong. I think that the program does not count more than 1 zero in each row but I could be wrong.

Clearly, something is wrong with the loop. I tried to do a while loop like this
while (one==0||two==0......)
{zero=zero + 1;}

but nothing changed and in fact my program did not seem to work this way. For some reason it only works somewhat right with if loops. I don't know how to apply other loops to this so that the program actually counts the right number of zeros.

Here is my code

``````#include <fstream>
#include <iostream>
#include <string>

using namespace std;

int main()
{
ifstream fileIn;
ofstream fileOut;
int one,two,three,four, five=0
int totalSum=0;
int zero=0;
int num_data_pts(0);

fileIn.open("somedata.dat");

fileIn>>one>>two>>three>>four>>five;

while(!fileIn.eof())
{
totalSum = totalSum + one + two + three + four + five;
num_data_pts = num_data_pts + 5;
fileIn>>one>>two>>three>>four>>five;

if(one==0)
{zero=zero+1;}
else if(two==0)
{zero=zero+1;}
else if (three==0)
{zero=zero+1;}
else if (four==0)
{zero=zero+1;}
else if (five==0)
{zero=zero+1;}
else
continue;

}//end while

fileIn.close();
fileOut.open("datainfo.txt");

fileOut <<"Total Sum of Integers is: "<<totalSum<<endl;
fileOut <<"Total Number of Rows is: "<<num_data_pts/5<<endl;//outputs number of
fileOut <<"Total Number of zeros is: "<<zero<<endl;//outputs total number of zeros
fileOut.close();
return 0;
}``````

If someone can help I would really appreciate it.

while (one==0||two==0......)
{zero=zero + 1;}

This will be an infinite loop if there is a 0 in the row.

if(one==0)
{zero=zero+1;}
else if(two==0)
{zero=zero+1;}
else if (three==0)
{zero=zero+1;}
else if (four==0)
{zero=zero+1;}
else if (five==0)
{zero=zero+1;}

When you use "else if" you are saying to only check for that scenario if the previous scenario was not true. Therefore, you are right when you said that your program will only detect one 0 per row. E.g. if the first number is 0, then the rest of the numbers won't even be checked. You need to have individual if statements for each case... not else ifs.