File stream problem. PLEASE HELP

This is a discussion on File stream problem. PLEASE HELP within the C++ Programming forums, part of the General Programming Boards category; I tried the following and it didn't work and I can't figure this out Code: double count, SalesID, modelCode ifstream ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    3

    File stream problem. PLEASE HELP

    I tried the following and it didn't work and I can't figure this out

    Code:
    double count, SalesID, modelCode
    ifstream in;
    
    while (!fin.eof())
    {
    fin >> count
    sum+=count
    }
    That's basically what I did but what the problem is there like 60 rows and 4 columns in the file. I only want to tally numbers from one column. For example one column is amount sold. I only want to assign numbers from that column to "count". Anyone PLEASE help

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    eof will only return true after a read has failed. So the last count is wrong.

    Also, it's better to use:
    Code:
    while(fin >> count)
    Edit: When I looked at your code I assumed you left out some lines. If you didn't, then don't forget to initialise your variables :P.
    Last edited by EVOEx; 02-27-2009 at 02:43 PM.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    3
    Why? What exactly does that do?

  4. #4
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Posts
    324
    Since you know the format of the file, why not read in a full line at a time, extract the necessary data, ignoring the rest of what's in the line?

    I believe cin.getline() and cin << extraction operator should be sufficient for that.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    3
    A little bit more explantion may be necassary

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Replies: 9
    Last Post: 07-01-2002, 07:50 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

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