Oct 21, 2015 at 4:39am UTC
I am trying to make it so that when this program reads in an empty data set it will display "No Statistics after it displays "fish collected". However I am having an issue where, if the program reads an empty file it just stops on the while loop and doesn't display anything after that. Any help would be much appreciated!
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
int main()
{
int fishCount = 0;
int code;
int chemo = 0;
int greene = 0;
int hopkins = 0;
int toddy = 0;
int auburn = 0;
int tripp = 0;
int jordan = 0;
double heaviest = 0;
double lightest = INFINITY;
double fishWeight;
double fishweightTot = 0;
ifstream fin;
ofstream fout;
fin.open("fishweight.txt");
fout.open("histogram.txt");
cout << fixed << left << setw(10) << "Lake ID" << setw(12) << "Lake Name" << "Fish Weight" << endl << "---------------------------------" << endl;
while (!fin.fail() == !fin.eof())
{
fishCount++;
fin >> code >> fishWeight;
fishweightTot = fishweightTot + fishWeight;
if (fishWeight > heaviest)
heaviest = fishWeight;
if (fishWeight < lightest)
lightest = fishWeight;
if (code == 1000)
{
chemo++;
cout << setprecision(1) << fixed << left << setw(10) << "1000" << setw(12) << "Chemo" << fishWeight << endl;
}
else if (code == 1010)
{
greene++;
cout << setw(10) << "1010" << setw(12) << "Greene" << fishWeight << endl;
}
else if (code == 1050)
{
hopkins++;
cout << setw(10) << "1050" << setw(12) << "Hopkins" << fishWeight << endl;
}
else if (code == 1100)
{
toddy++;
cout << setw(10) << "1100" << setw(12) << "Toddy" << fishWeight << endl;
}
else if (code == 1250)
{
auburn++;
cout << setw(10) << "1250" << setw(12) << "Auburn" << fishWeight << endl;
}
else if (code == 1300)
{
tripp++;
cout << setw(10) << "1300" << setw(12) << "Tripp" << fishWeight << endl;
}
else if (code == 1350)
{
jordan++;
cout << setw(10) << "1350" << setw(12) << "Jordan" << fishWeight << endl;
}
}
cout << "----------------------------" << endl << "John Doe" << endl;
cout << "Fish Collected: " << fishCount << endl;
if (fishCount == 0)
{
cout << "No Statistics";
system("pause");
return 1;
}
else
{
cout << "Average Fish Weight: " << fishweightTot / fishCount << endl;
cout << "Heaviest Fish: " << heaviest << endl << "Lightest Fish: " << lightest << endl;
cout << endl << setw(12) << "Lake" << "Fish Count" << endl << "-----------------------" << endl;
}
if (chemo > 0)
{
cout << setw(12) << "Chemo";
for (; chemo > 0; chemo--)
cout << "*";
cout << endl;
}
if (greene > 0)
{
cout << setw(12) << "Greene";
for (; greene > 0; greene--)
cout << "*";
cout << endl;
}
if (hopkins > 0)
{
cout << setw(12) << "Hopkins";
for (; hopkins > 0; hopkins--)
cout << "*";
cout << endl;
}
if (toddy > 0)
{
cout << setw(12) << "Toddy";
for (; toddy > 0; toddy--)
cout << "*";
cout << endl;
}
if (auburn > 0)
{
cout << setw(12) << "Auburn";
for (; auburn > 0; auburn--)
cout << "*";
cout << endl;
}
if (tripp > 0)
{
cout << setw(12) << "Tripp";
for (; tripp > 0; tripp--)
cout << "*";
cout << endl;
}
if (jordan > 0)
{
cout << setw(12) << "Jordan";
for (; jordan > 0; jordan--)
cout << "*";
cout << endl;
}
system("pause");
return 0;
}
Last edited on Oct 21, 2015 at 4:39am UTC
Oct 21, 2015 at 8:31am UTC
Use code tags.
[code] your code here
[/code] . It will allow us to reason about your code and refer to parts of it by line numbers.
\
while (!fin.fail() == !fin.eof())
This is so wrong... If both fail and eof are set at the same time (which is how it commonly happens), your loop is infinite. Main rule of dealing with streams: do not touch eof. It is rarely needed. Loop on input operation.
1 2 3
while (fin >> code >> fishWeight)
{
fishCount++;
Edit: I should refresh page before answering.
Last edited on Oct 21, 2015 at 8:32am UTC
Oct 21, 2015 at 1:15pm UTC
Thank you very much! Sorry I didn't use the code tag, this is my first post.