Thread: Program Looping Problem

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    41

    Program Looping Problem

    I wrote a simple routine to read and load values from my currency exchange rates.csv file, but it appears to keep reading the first record. Because of this, my check for EOF never gets satisfied. What is the secret to advancing to the next record and read the rest of the file? I am including my code for your review.
    Code:
    float Curr_Conv_Factor;      /* Individual currency conversion factor read from
                                       file to be used to fill the array */
       float Currency_Conv[24][24]; /* Array containg currency conversion factors */
       int   Row;                   /* Row position in currency conversion array */
       int   Column;                /* Column poistion in currency conversion array */
       fstream infile;              /* Define/create object type to be used for accessing
                                       the contents of the file. */
    
    
    main()
    {
            infile.open( "currency exchange rates.csv", ios::in );
    
           while( !infile.eof() )
          {
          for (Row = 0; Row < 24; Row++)
         {
         for (Column = 0; Column < 24; Column++)
         {
         infile >> Curr_Conv_Factor;
         Currency_Conv[Row][Column] = Curr_Conv_Factor;
         printf("\n Currency Conversion amount in %i, %i is", Row, Column);
         printf("%f", Currency_Conv[Row][Column]);
         infile >> Curr_Conv_Factor;
         }
         }
     }
    Code Tags Added by Kermi3

  2. #2
    Registered User
    Join Date
    Dec 2004
    Location
    Mercer, PA
    Posts
    25
    haha - use code tags bud...
    what compiler are you using?
    The reason I ask this is because there is a serious bug with Visual C++ 6 Enterprise. I ended up having to change compilers to Dev-cpp to do the job.

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    41

    Program Looping Problem

    I am using Dev-C++ 4. I see that my code is still not conforming to the standards of using code tags, sorry about that!

  4. #4
    Registered User
    Join Date
    Dec 2004
    Location
    Mercer, PA
    Posts
    25
    Do you think you could give us a sample of how data is structured in your file... I know its csv but still visuals make it easier for the rest of the board.
    One way to advance is to select a certain byte in a file - however you are using sequential access files - not random access. I almost always work with data in random access and make structures to store the data. You would have to open your file in binary mode then use fileIn.seekg(int) to change file get position and fileIn.seekp(int) to change put position.
    To find current position you can use int fileIn.tellp() and int fileIn.tellg()
    hope this helps a little...

  5. #5
    Registered User
    Join Date
    Dec 2004
    Location
    Mercer, PA
    Posts
    25
    If you just select the code and then press the little # button on the editor pallet it will auto-insert code tags around your code.
    Nice function! :-D

  6. #6
    Registered User
    Join Date
    Nov 2004
    Posts
    41

    Program Looping Problem

    Thanks for the help about the code tags. I am including 48 records from my *.csv file or two rows worth.

    1.0000000,0.7785980,0.3613370,1.8514000,0.8285000, 0.1208240,0.1747490,1.2982000,0.1285890,0.0222220, 0.0095940,0.2631580,0.0881680,0.7086020,0.1596320, 0.6046680,0.1658810,0.0009350,0.0095350,0.1447010, 0.8553590,0.0306420,0.0249000,0.0005220
    1.2843600,1.0000000,0.4640870,2.3778600,1.0640900, 0.1551820,0.2244400,1.6673600,0.1651550,0.0285410, 0.0123220,0.3379890,0.1132390,0.9101000,0.2050250, 0.7766110,0.2130520,0.0012010,0.0122460,0.1858480, 1.0985900,0.0393550,0.0319810,0.0006710

  7. #7
    Registered User
    Join Date
    Dec 2004
    Location
    Mercer, PA
    Posts
    25
    Ok - when I compile it all I get is 0.0000000
    I'v narrowed it down to something..
    If you declare your Curr_Conv_Factor as int and then get that value - it will work. But if your leave it float it will not. Why I dont know...
    But there is a work around. You could treat the whole file as text in your program. Buffer the numbers up until the ',' then you could use atof() function
    Example...

    Code:
    char x[] = "3.45643";
    double y = 0;
    y = atof(x);

  8. #8
    Registered User
    Join Date
    Nov 2004
    Posts
    41

    Program Looping Problem

    I changed the Curr_Conv_Factor to int like you suggested and recompiled and ran it. I still get only the first record read. I am new to this, this is why I am not sure why I have the problem.

  9. #9
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Your problem is that you are using the extraction operator to read into a numeric variable, but your input contains non-numeric characters. Your program is not reading the same value over and over, it reads it once and encounters the comma at which point it stops reading (due to error) and each time through the loop your variable doesn't change.

    Here's a simple (albeit not too elegant) fix that will get you around your woes:
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    int main() {
      float Curr_Conv_Factor; /* Individual currency conversion factor read from
                                 file to be used to fill the array */
      float Currency_Conv[24][24]; /* Array containg currency conversion factors */
      int Row; /* Row position in currency conversion array */
      int Column; /* Column poistion in currency conversion array */
      std::fstream infile; /* Define/create object type to be used for accessing
                         the contents of the file. */
      
      infile.open( "in.csv", std::ios_base::in );
      std::string field;
    
      while( infile.good() ) {
        // check if the file is still good in your inner loops too
        for (Row = 0; Row < 24 && infile.good(); Row++) {
          for (Column = 0; Column < 24 && infile.good(); Column++) {
            if (Column == (24 - 1))
              std::getline(infile, field);// last field on a line isn't comma-delimited
            else
              std::getline(infile, field, ',');// but the rest are
    
            Curr_Conv_Factor = std::atof(field.c_str());// convert string to floating point numeric
            
            Currency_Conv[Row][Column] = Curr_Conv_Factor;
            std::printf("\n Currency Conversion amount in %i, %i is ", Row, Column);
            std::printf("%f", Currency_Conv[Row][Column]);
          }
        }
      }
      
      return 0;
    }

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You're not dealing with all those comma's at all well.

    Code:
    for (Row = 0; Row < 24; Row++)
    {
     for (Column = 0; Column < 24; Column++)
     {
      infile >> Curr_Conv_Factor;
      Currency_Conv[Row][Column] = Curr_Conv_Factor;
      printf("\n Currency Conversion amount in %i, %i is", Row, Column);
      printf("%f", Currency_Conv[Row][Column]);
      infile.get(); // burn a comma
     }
    }

  11. #11
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    While my solution is useful for more general comma-delimited file parsing, Salem's is simple and perfect for your specific problem (comma-delimited numerics). The only thing you need to add to it is a check in your for loops that the file is still good, otherwise it'll just keep looping even though you've past the end of the file.

  12. #12
    Registered User
    Join Date
    Nov 2004
    Posts
    41
    I have the code just like LuckY has listed above, but I have several errors now. Should I provide a list of the errors for further help?

  13. #13
    Registered User
    Join Date
    Nov 2004
    Posts
    41

    Program Looping Problem

    The code is as follows:


    Code:
    float Curr_Conv_Factor;      /* Individual currency conversion factor read from
                                       file to be used to fill the array */
       float Currency_Conv[24][24]; /* Array containing currency conversion factors */
       char  Field;
       int   Row;                   /* Row position in currency conversion array */
       int   Column;                /* Column position in currency conversion array */
       std::fstream infile;         /* Define/create object type to be used for accessing
                                       the contents of the file. */
    
    
    main()
    {
      std::fstream infile; /* Define/create object type to be used for accessing
                         the contents of the file. */
      
      infile.open( "currency exchange rates.csv", std::ios_base::in );
      std::string field;
    
      while( infile.good() ) {
        /* check if the file is still good in your inner loops too  */
        for (Row = 0; Row < 24 && infile.good(); Row++) {
          for (Column = 0; Column < 24 && infile.good(); Column++) {
            if (Column == (24 - 1))
              std::getline(infile, field);/* last field on a line isn't comma-delimited */
            else
              std::getline(infile, field, ','); /* but the rest are */
    
            Curr_Conv_Factor = std::atof(field.c_str()); /* convert string to floating point numeric */
            
            Currency_Conv[Row][Column] = Curr_Conv_Factor;
            std::printf("\n Currency Conversion amount in %i, %i is ", Row, Column);
            std::printf("%f", Currency_Conv[Row][Column]);
          }
        }
      }
      
      return 0;
    }
    Last edited by AQWst; 12-20-2004 at 10:19 PM.

  14. #14
    Registered User
    Join Date
    Dec 2004
    Location
    Mercer, PA
    Posts
    25
    haha - your code tags still are not working - I notice you have an opening code tag but no closing. At the end of your code make sure you add [/CODE]

  15. #15
    Registered User
    Join Date
    Nov 2004
    Posts
    41
    I editted my program portion of the message. Can someone please help me with it? I am not sure what is wrong. I think I have everything the way that was entered by LuckY's response.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-25-2008, 12:31 AM
  2. [NEED HELP] I just can't solve this program problem!
    By frodonet in forum C Programming
    Replies: 7
    Last Post: 09-23-2007, 02:44 PM
  3. Inheritance and Dynamic Memory Program Problem
    By goron350 in forum C++ Programming
    Replies: 1
    Last Post: 07-02-2005, 02:38 PM
  4. Looping Problem
    By ccmac in forum C++ Programming
    Replies: 1
    Last Post: 04-22-2002, 02:46 PM
  5. Console Program Problem
    By Breach23 in forum C++ Programming
    Replies: 3
    Last Post: 10-19-2001, 12:35 AM