473,400 Members | 2,145 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,400 software developers and data experts.

this seems like an...Infinite Loop...

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::operator == (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[i].readdata(infile);

// if dummy data, 00/00 is read, then variable i will be assigned
// 10 which will stop the loop.

if (sf[i].getdate() == "00/00")
i = 10;
else
{
if (sf[i].datavalid() == 'T')
{
validsf[j] = sf[i];
sf[i].updatevalidcount();
sf[i].calctotalsf();
++j;
}
else
{
invalidsf[k] = sf[i];
sf[i].updateinvalidcount();
++k;
}
}
}

sortsf(validsf);
writeheadings(outfile);
writevalidsf(validsf, outfile);
writeinvalidsf(invalidsf, outfile);
validsf[i].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[i].getvalidcount(); i++)
for (int j; j = i + 1; j++)
if ((float(validsf[i].getft()) * 12 + validsf[i].getin()) >
(float(validsf[j].getft()) * 12 + validsf[j].getin()))
{
temp = validsf[i];
validsf[i] = 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[i].getvalidcount(); i++)
validsf[i].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[i].getinvalidcount(); i++)
outfile<< "Invalid amount of snowfall for the date, "
<< invalidsf[i].getdate() << endl;
}

Jul 22 '05 #1
5 1463
> for (int i = 0; i = invalidsf[i].getinvalidcount(); i++)

Are you sure you really mean to assign
'invalidsf[i].getinvalidcount()' to 'i' each time this
loop iterates?

HTH,
- J.
Jul 22 '05 #2
i found what's wrong with this.

in source code i changed every = to <.
i.e. for (int i = 0; i = invalidsf[i].getinvalidcount(); i++) to for
(int i = 0; i < invalidsf[i].getinvalidcount(); i++)

Jul 22 '05 #3
ti*******@hotmail.com wrote:
i found what's wrong with this.

in source code i changed every = to <.
i.e. for (int i = 0; i = invalidsf[i].getinvalidcount(); i++) to for
(int i = 0; i < invalidsf[i].getinvalidcount(); i++)


The question is, do you know WHY that solved the problem? You should
see if you can turn up the warning level of your compiler. Many will
diagnose that problem of using = where you should have ==.


Brian
Jul 22 '05 #4

<ti*******@hotmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
i found what's wrong with this.

in source code i changed every = to <.
But do you know *why* this fixed it? It's important that you do.
i.e. for (int i = 0; i = invalidsf[i].getinvalidcount(); i++) to for
(int i = 0; i < invalidsf[i].getinvalidcount(); i++)


An approach of blindly "change all x to y" is fraught with danger,
and does not help you really learn. E.g. sometimes the operator
in the conditional expression of a 'for' loop might be something
other than '<'. As a matter of fact, it might not contain an operator
at all. E.g.

for(int i = 0; foo(); ++i)
{
/* etc */
}

-Mike
Jul 22 '05 #5
In message <11**********************@z14g2000cwz.googlegroups .com>,
ti*******@hotmail.com writes
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?
Just a few comments, irrelevant code snipped...
static int validcount;
static int invalidcount;
Why static? What happens if you ever have two instances of class
snowfall?
void writedata(ofstream& outfile)

// outputs a data. ....
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);
None of these functions modify the object, so they should be declared
const. In fact, if validcount and invalidcount really are static, the
corresponding access functions can also be static.
};

int snowfall::operator == (char str[])

// overloading operator == to compare two strings
Should return bool.
{
return (strcmp(date, str) == 0) ? 1 : 0;
"?1:0" is redundant. The result of strcmp()==0 is already true or false.
}

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;
} Why not return bool?

....// setting a loop to go 10 times since there will be maximum of 10
// records.

for (int i = 0; i <= 10; i++)
No it doesn't, it goes 11 times.
{
sf[i].readdata(infile);

// if dummy data, 00/00 is read, then variable i will be assigned
// 10 which will stop the loop.


No, it won't, because the loop is up to 10 *inclusive*.

If i is a counter, then use it as a counter, not as an end-of-loop flag.
Using one variable with two simultaneous meanings is evil. This
illustrates why: if you decide to change the number of records, you need
to change that "10" in two places.

If you want to terminate the loop early, the way to do it is to use
break.

I'll leave the rest, as others have already spotted some of the other
mistakes.
--
Richard Herring
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

43
by: Gremlin | last post by:
If you are not familiar with the halting problem, I will not go into it in detail but it states that it is impossible to write a program that can tell if a loop is infinite or not. This is a...
10
by: Kiko Tores | last post by:
If there is something wrong , could you please point it out ( if you can give me an example as well) int find( int array, int size, int key ) { int begin=0, end=size-1, middle; while...
5
by: mailpitches | last post by:
Hello, Is there any way to kill a Javascript infinite loop in Safari without force-quitting the browser? MP
4
by: LOPEZ GARCIA DE LOMANA, ADRIAN | last post by:
Hi all, I have a question with some code I'm writting: def main(): if option == 1: function_a()
59
by: rami | last post by:
please everybody ,can anyone tell me how to do an infinite loop in C
13
by: Vector | last post by:
Is any infinite loop better than other? Is there any difference between there efficiency?
10
by: =?ISO-8859-1?Q?G=E9rard_Talbot?= | last post by:
www.authoring.stylesheets] Dear fellow CSS colleagues and web authors in alt.html discussion forum, I would like to ask you to help me confirm that there is a serious bug in IE 7 final release...
3
by: danielkun | last post by:
I'm building a simple slideshow and would like to exchange the FOR loop below into an infinite loop that will start over with picure 1 when picture 5 has been shown. Im still new to C and I hope...
14
by: Mohamed Mansour | last post by:
Hey there, this will be somewhat a long post, but any response is appreciated! I have done many PInvoke in the past from C++ to C#, but I did PInvoke within C# not C++/CLI. Can someone explain...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.