Thread: Infinite Loop HELP!!

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    4

    Unhappy 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. #2
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595

    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
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    4

    Unhappy 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. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    4
    and this is the txt file that the program required to open...

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    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. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    4

    Smile

    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-14-2009, 11:24 PM
  2. Cosine fucntion and infinite loop.
    By youareafever in forum C Programming
    Replies: 2
    Last Post: 11-07-2008, 04:45 AM
  3. Infinite Loop with GetAsyncKeyState
    By guitarist809 in forum Windows Programming
    Replies: 1
    Last Post: 04-18-2008, 12:09 PM
  4. Switch statement = infinite loop
    By Lucid003 in forum C++ Programming
    Replies: 10
    Last Post: 10-10-2005, 12:46 AM
  5. stays in loop, but it's not an infinite loop (C++)
    By Berticus in forum C++ Programming
    Replies: 8
    Last Post: 07-19-2005, 11:17 AM