Thread: Beginner Help Read Next Line of Input File

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    11

    Unhappy Beginner Help Read Next Line of Input File

    I have a very basic question. Here is my code:

    Code:
    #include <iostream>
    #include<fstream>
    #include <iomanip>
    using namespace std;
    
    void main( )
    {
            
    
    ifstream inFile;
    
    double ticketPrice;
    double numSold;
    double totalSale;
    
    inFile.open("ticketSales.txt");
    
    inFile >> ticketPrice >> numSold;
    
    while (!inFile.eof())
    	{
    	totalSale = numSold * ticketPrice;
    
    	cout << fixed << showpoint << setprecision(2);
    	cout << "Total sale amount: $" << totalSale << endl;
    	
    
    
    	}
    }

    So only the first line of the file is read and it just continues to show on the screen. It never reads the next line of the file. I have tried searching for help on FAQ's and haven't gotten help thus far. As far as I understand a counter wouldn't work because I don't know what the input file will actually have at all times. Here is an example of an input file:

    250 5750
    100 28000
    50 35750
    25 18750

    Thank you for your help!!
    Last edited by SkinnieMinnie; 11-24-2007 at 01:57 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    For starters, you only read from the file in one statement:
    Code:
    inFile >> ticketPrice >> numSold;
    You should be reading from the file in your loop, and it should not be controlled by eof() since eof() only returns true after you attempt to read when the end of file is reached.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    11
    That was all I needed. Thank you sooo much! I knew I was overlooking something sooo small! Thanks!!

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    I think if I ever write a C or C++ compiler, it will force main() to be defined properly.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Unfortunately, void main is legal on some systems.
    SkinnieMinnie: Unless you're writing a program for some very specific system, which I doubt, you'd better read the FAQ on main.

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    void main() has never been legal C afaik. If it's considered legal, it's only been done so by an extension of the system that you are on, and most definitely nothing to do with the language. Hence, I will probably never allow it in any compiler I write.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, I'll agree that, at least, a warning is in place.

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Another thing I would probably do is set the default warning level much higher than most compilers.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    void main() has never been legal C afaik.
    Of course it has... in a freestanding environment. But then in such a situation pretty much any designated function can be used to substitute main, as long as it is defined as such by the implementation.

    EDIT:
    hmm... since this is actually the C++ board... note that my claim that void main() may be legal in a freestanding environment applies to C, at least from my reading of C99. The C++ standard does not seem so clear on whether it is ever permitted.
    Last edited by laserlight; 11-25-2007 at 12:48 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    For completeness:
    Quote Originally Posted by ISO/IEC 9899:TC2 WG14/N1124
    5.1.2.1 Freestanding environment
    1 In a freestanding environment (in which C program execution may take place without any
    benefit of an operating system), the name and type of the function called at program
    startup are implementation-defined. Any library facilities available to a freestanding
    program, other than the minimal set required by clause 4, are implementation-defined.
    Quote Originally Posted by N2315=07-0175, whatever that means
    3.6.1 Main function [basic.start.main]
    1 A program shall contain a global function called main, which is the designated start of the program. It is implementation-
    defined whether a program in a freestanding environment is required to define a main function. [ Note: in a freestanding
    environment, start-up and termination is implementation-defined; start-up contains the execution of constructors for
    objects of namespace scope with static storage duration; termination contains the execution of destructors for objects
    with static storage duration. — end note ]
    Last edited by robwhit; 11-25-2007 at 01:12 AM.

  11. #11
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Bah, foiled again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Replies: 16
    Last Post: 01-04-2007, 03:38 PM
  3. unable to read double A[0] and A[1] when n=1
    By sweetarg in forum C Programming
    Replies: 2
    Last Post: 10-25-2005, 12:35 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. Greenhand want help!
    By leereg in forum C Programming
    Replies: 6
    Last Post: 01-29-2002, 06:04 AM