# Infinite Loop HELP!!

This is a discussion on Infinite Loop HELP!! within the C++ Programming forums, part of the General Programming Boards category; How can i get rid of the infinite loop in this program... Code: #include <iostream.h> #include <fstream.h> #include <stdlib.h> #include ...

1. ## Infinite Loop HELP!!

How can i get rid of the infinite loop in this program...

Code:
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <iomanip.h>

int main()
{
ifstream myFile;
double accNum, gallons;
char code;
double totals, discount, gst, pst, finCost;
myFile.open("gallons2.txt",ios::nocreate);

if (myFile.fail())
{
cout<<"File cannot be opened"<<endl;
exit(-1);
}

myFile>>accNum>>code>>gallons;

while (!myFile.eof())
{
switch (code)
{
case 'H':
totals = (0.0005 * gallons) + 5;
if ((totals > 75) && (totals < 1000))
discount = totals * 0.1;
else if (totals > 20)
discount = totals * 0.05;
else
discount = 0;
break;

case 'C':
totals = (0.00025 * (gallons - 4000000)) + 1000;
discount = 0;
break;

case 'I':
discount = 0;
if (gallons <= 4000000)
totals = 1000;
else if ((gallons > 4000000) && (gallons <= 10000000))
totals = 2000;
else
totals = 3000;
break;
default:
cout<<"Error Input";
break;
}
gst = (totals - discount) * 0.07;
pst = (totals - discount) * 0.075;
finCost = (totals - discount) + gst +pst;
cout<<accNum<<code<<gallons<<totals<<discount<<gst<<pst<<finCost;
}

cout<<"Account"<<endl;
cout<<"Number  "<<"Code  "<<"Consumption  "<<"Total  "<<"Discount  "<<"GST  "<<"PST  "<<"Final Cost  "<<endl;
myFile.close();
return 0;
}

2. ## Code Tags

I am posting this because you did not use code tags on this thread. In the furture please use Code Tags. They make your code MUCH easier to read and people will be much more likely to help you if you do. And they'll be happy about helping you

For example:

Without code tags:

for(int i=0;i<5;i++)
{
cout << "No code tags are bad";
}

With Code Tags:
Code:
for(int i=0;i<5;i++)
{
cout << "This code is easy to read";
}
This is of course a basic example...more complicated code is even easier to read with code tags than without.

I've added code tags for you this time. They can be added by putting [code] at the beginning of your code and [/code] at the end. More information on code tags may be found at the link in my signature. I also suggest you take a look at the board guildlines if you have not done so already. Any further questions or ways I can help please feel free to PM me.

Good Luck,
Kermi3

3. ## Ok, the loop is done, but...

but still, the result is very very strange...

here is my cpp file and the txt file that the program need to open with...

4. and this is the txt file that the program required to open...

5. I'm not seeing any problems. And it compiled fine. I'm not a huge fan of your lack of white space in your output but I'm sure you didn't come here to be criticized.

6. Yeah,

now i am able to compile it and with the "setw" "setprecision" "setiosflags" i am able to make space between those number.

but i don't know why that my last result display 2 times. instead of once.