Beginner Help Read Next Line of Input File

This is a discussion on Beginner Help Read Next Line of Input File within the C++ Programming forums, part of the General Programming Boards category; I have a very basic question. Here is my code: Code: #include <iostream> #include<fstream> #include <iomanip> using namespace std; void ...

  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 12:57 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,401
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,211
    I think if I ever write a C or C++ compiler, it will force main() to be defined properly.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,422
    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,211
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,422
    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,211
    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
    21,401
    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-24-2007 at 11:48 PM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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 12:12 AM.

  11. #11
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    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, 02: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, 09:46 AM
  5. Greenhand want help!
    By leereg in forum C Programming
    Replies: 6
    Last Post: 01-29-2002, 05:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21