T
titan0111
Hi, it's me again.
Thank you guys for helping me with other error messages.
This is the source code.
It goes throught compiler fine but when I run it,
it seems to be in infinite loop.
Can you tell me what's causing it?
// This is a program which reads a data from a file, where each record
contains
// the amount of one snowfall. This amount will be supplied as
feet(integer),
// inches(float), and date(string, with format MM/DD). Then creates an
output file
// which lists the snowfalls from largest to smallest in table format.
At the end
// of the table, outputs the total amount of snowfall received.
#include<iostream>
#include<iomanip>
#include<cstring>
#include<fstream>
using namespace std;
class snowfall
{
private:
int ft;
float in;
float totalsfin;
char date[6];
static int validcount;
static int invalidcount;
public:
void readdata(ifstream& infile)
// reads a data from input.txt file
{
infile>> date >> ft >> in;
}
void writedata(ofstream& outfile)
// outputs a data.
{
outfile<< setw(5) << date << setw(5) << ft << " feet " << setw(3)
<< setfill(' ') << setw(5) << fixed << showpoint
<< setprecision(2) << in << " inches\n";
}
void updatevalidcount()
// validcount counter
{
++validcount;
}
void updateinvalidcount()
// invalidcount counter
{
++invalidcount;
}
void calctotalsf()
// calculates total snowfalls in inches by multiplying 12 to feet and
adding
// it to inches
{
totalsfin += float(ft)*12 + in;
}
int getft()
{return ft;}
float getin()
{return in;}
char* getdate()
{return date;}
int getvalidcount()
{return validcount;}
int getinvalidcount()
{return invalidcount;}
int operator == (char[]);
char datavalid();
void writetotalsf(ofstream& outfile);
};
int snowfall:perator == (char str[])
// overloading operator == to compare two strings
{
return (strcmp(date, str) == 0) ? 1 : 0;
}
char snowfall::datavalid()
// checking to see both feet and inches are greater than 0, since they
can't
// be negative numbers.
{
char valid;
if ((ft >= 0) && (in >= 0.0f))
valid = 'T';
else
valid = 'F';
return valid;
}
void snowfall::writetotalsf(ofstream& outfile)
// converts total snowfalls in inches to feet and inches. Then outputs
them.
{
int totalsfft = 0;
if (totalsfin >= 12.0f)
while (totalsfin <= 11.0f)
{
++totalsfft;
totalsfin -= 12.0f;
}
outfile<< "/nThe total amount of snowfalls: " << totalsfft
<< " feet " << totalsfin << " inches.\n";
}
int snowfall::validcount;
int snowfall::invalidcount;
void main()
{
snowfall sf[10];
snowfall validsf[10];
snowfall invalidsf[10];
void writetitle(ofstream&);
void writeheadings(ofstream&);
void sortsf(snowfall[]);
void writevalidsf(snowfall[], ofstream&);
void writeinvalidsf(snowfall[], ofstream&);
ifstream infile;
ofstream outfile;
infile.open("c:\\downloads\\input.txt");
outfile.open("c:\\downloads\\output.txt");
if (infile && outfile)
{
int j = 0;
int k = 0;
writetitle(outfile);
// setting a loop to go 10 times since there will be maximum of 10
// records.
for (int i = 0; i <= 10; i++)
{
sf.readdata(infile);
// if dummy data, 00/00 is read, then variable i will be assigned
// 10 which will stop the loop.
if (sf.getdate() == "00/00")
i = 10;
else
{
if (sf.datavalid() == 'T')
{
validsf[j] = sf;
sf.updatevalidcount();
sf.calctotalsf();
++j;
}
else
{
invalidsf[k] = sf;
sf.updateinvalidcount();
++k;
}
}
}
sortsf(validsf);
writeheadings(outfile);
writevalidsf(validsf, outfile);
writeinvalidsf(invalidsf, outfile);
validsf.writetotalsf(outfile);
}
else
{
if (!infile)
cout<< "Unable to open file input.txt.\n";
if (!outfile)
cout<< "Unable to open file output.txt.\n";
}
}
void writetitle(ofstream& outfile)
// outputs general title informations
{
outfile<< "Title\n\n";
}
void writeheadings(ofstream& outfile)
// outputs general heading informations
{
outfile<< "Headings\n\n";
}
void sortsf(snowfall validsf[])
// sorts the list of snowfalls from largest to smallest
{
snowfall temp;
for (int i; i = validsf.getvalidcount(); i++)
for (int j; j = i + 1; j++)
if ((float(validsf.getft()) * 12 + validsf.getin()) >
(float(validsf[j].getft()) * 12 + validsf[j].getin()))
{
temp = validsf;
validsf = validsf[j];
validsf[j] = temp;
}
}
void writevalidsf(snowfall validsf[], ofstream& outfile)
// sets a loop to go whatever number in validcount variable
{
for (int i=0; i = validsf.getvalidcount(); i++)
validsf.writedata(outfile);
}
void writeinvalidsf(snowfall invalidsf[], ofstream& outfile)
// sets a loops to go whatever number in invalidcount variable, if
there is
// an invalid data, then outputs its date followed by an error message.
{
if (invalidsf[0].getinvalidcount() != 0)
for (int i = 0; i = invalidsf.getinvalidcount(); i++)
outfile<< "Invalid amount of snowfall for the date, "
<< invalidsf.getdate() << endl;
}
Thank you guys for helping me with other error messages.
This is the source code.
It goes throught compiler fine but when I run it,
it seems to be in infinite loop.
Can you tell me what's causing it?
// This is a program which reads a data from a file, where each record
contains
// the amount of one snowfall. This amount will be supplied as
feet(integer),
// inches(float), and date(string, with format MM/DD). Then creates an
output file
// which lists the snowfalls from largest to smallest in table format.
At the end
// of the table, outputs the total amount of snowfall received.
#include<iostream>
#include<iomanip>
#include<cstring>
#include<fstream>
using namespace std;
class snowfall
{
private:
int ft;
float in;
float totalsfin;
char date[6];
static int validcount;
static int invalidcount;
public:
void readdata(ifstream& infile)
// reads a data from input.txt file
{
infile>> date >> ft >> in;
}
void writedata(ofstream& outfile)
// outputs a data.
{
outfile<< setw(5) << date << setw(5) << ft << " feet " << setw(3)
<< setfill(' ') << setw(5) << fixed << showpoint
<< setprecision(2) << in << " inches\n";
}
void updatevalidcount()
// validcount counter
{
++validcount;
}
void updateinvalidcount()
// invalidcount counter
{
++invalidcount;
}
void calctotalsf()
// calculates total snowfalls in inches by multiplying 12 to feet and
adding
// it to inches
{
totalsfin += float(ft)*12 + in;
}
int getft()
{return ft;}
float getin()
{return in;}
char* getdate()
{return date;}
int getvalidcount()
{return validcount;}
int getinvalidcount()
{return invalidcount;}
int operator == (char[]);
char datavalid();
void writetotalsf(ofstream& outfile);
};
int snowfall:perator == (char str[])
// overloading operator == to compare two strings
{
return (strcmp(date, str) == 0) ? 1 : 0;
}
char snowfall::datavalid()
// checking to see both feet and inches are greater than 0, since they
can't
// be negative numbers.
{
char valid;
if ((ft >= 0) && (in >= 0.0f))
valid = 'T';
else
valid = 'F';
return valid;
}
void snowfall::writetotalsf(ofstream& outfile)
// converts total snowfalls in inches to feet and inches. Then outputs
them.
{
int totalsfft = 0;
if (totalsfin >= 12.0f)
while (totalsfin <= 11.0f)
{
++totalsfft;
totalsfin -= 12.0f;
}
outfile<< "/nThe total amount of snowfalls: " << totalsfft
<< " feet " << totalsfin << " inches.\n";
}
int snowfall::validcount;
int snowfall::invalidcount;
void main()
{
snowfall sf[10];
snowfall validsf[10];
snowfall invalidsf[10];
void writetitle(ofstream&);
void writeheadings(ofstream&);
void sortsf(snowfall[]);
void writevalidsf(snowfall[], ofstream&);
void writeinvalidsf(snowfall[], ofstream&);
ifstream infile;
ofstream outfile;
infile.open("c:\\downloads\\input.txt");
outfile.open("c:\\downloads\\output.txt");
if (infile && outfile)
{
int j = 0;
int k = 0;
writetitle(outfile);
// setting a loop to go 10 times since there will be maximum of 10
// records.
for (int i = 0; i <= 10; i++)
{
sf.readdata(infile);
// if dummy data, 00/00 is read, then variable i will be assigned
// 10 which will stop the loop.
if (sf.getdate() == "00/00")
i = 10;
else
{
if (sf.datavalid() == 'T')
{
validsf[j] = sf;
sf.updatevalidcount();
sf.calctotalsf();
++j;
}
else
{
invalidsf[k] = sf;
sf.updateinvalidcount();
++k;
}
}
}
sortsf(validsf);
writeheadings(outfile);
writevalidsf(validsf, outfile);
writeinvalidsf(invalidsf, outfile);
validsf.writetotalsf(outfile);
}
else
{
if (!infile)
cout<< "Unable to open file input.txt.\n";
if (!outfile)
cout<< "Unable to open file output.txt.\n";
}
}
void writetitle(ofstream& outfile)
// outputs general title informations
{
outfile<< "Title\n\n";
}
void writeheadings(ofstream& outfile)
// outputs general heading informations
{
outfile<< "Headings\n\n";
}
void sortsf(snowfall validsf[])
// sorts the list of snowfalls from largest to smallest
{
snowfall temp;
for (int i; i = validsf.getvalidcount(); i++)
for (int j; j = i + 1; j++)
if ((float(validsf.getft()) * 12 + validsf.getin()) >
(float(validsf[j].getft()) * 12 + validsf[j].getin()))
{
temp = validsf;
validsf = validsf[j];
validsf[j] = temp;
}
}
void writevalidsf(snowfall validsf[], ofstream& outfile)
// sets a loop to go whatever number in validcount variable
{
for (int i=0; i = validsf.getvalidcount(); i++)
validsf.writedata(outfile);
}
void writeinvalidsf(snowfall invalidsf[], ofstream& outfile)
// sets a loops to go whatever number in invalidcount variable, if
there is
// an invalid data, then outputs its date followed by an error message.
{
if (invalidsf[0].getinvalidcount() != 0)
for (int i = 0; i = invalidsf.getinvalidcount(); i++)
outfile<< "Invalid amount of snowfall for the date, "
<< invalidsf.getdate() << endl;
}