Thread: Filling a vector from file... having a hard time...

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    56

    Filling a vector from file... having a hard time...

    This is pretty much what was given to us, I cannot get this to work... it just hangs when i open the input file.
    Code:
    void FillParallelVectors (string& CustomerName,
    						  int& AccountNumber,
    						  double& BeginningBalance,
    						  int& Count,
    						  int& NumberOfItems,
    						  vector <int>& CheckNumberVector,
    						  vector <double>& AmountVector)
    {
    	ifstream AccountInfoFile;
    	OpenInputFile (AccountInfoFile);
    	Count = 0;
    	NumberOfItems = 0; 
    	AccountInfoFile >> CustomerName >> AccountNumber >> BeginningBalance;
    	AccountInfoFile >> CheckNumberVector [Count] >> AmountVector [Count];
    	while (!AccountInfoFile.eof ())
    	{
    		Count ++;
    		if (Count >= CheckNumberVector.size ())
    		{
    			CheckNumberVector.resize (2 * CheckNumberVector.size ());
    			AmountVector.resize (2 * AmountVector.size ());
    		}
    	    AccountInfoFile >> CheckNumberVector [Count] >> AmountVector [Count];
    		NumberOfItems ++;
    	}
    	AccountInfoFile.close ();
    }

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    If it just hangs when you open the input file, perhaps the problem is in the OpenInputFile code that you haven't posted.

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    56
    Code:
    void OpenInputFile (ifstream& File)
    {
      string FileName;
      cout << "Please enter the drive and full path name for the input file: ";
      cout << endl << "No spaces in folder or file names ==> " ;
      cin  >> FileName;
      File.open (FileName.c_str ());   // output file
      if (File.fail ())
       {
          cout << FileName << " could not be opened." << endl;
          exit (-1);
        }
    }
    Last edited by criticalerror; 12-11-2003 at 06:44 PM.

  4. #4
    Registered User
    Join Date
    Dec 2003
    Posts
    56
    I am posting the source that I have written so far... it's far from complete, I am pretty much lost as to why it won't fill the vectors, as I am using the code almost line for line from what my instructor gave us.

  5. #5
    Registered User
    Join Date
    Dec 2003
    Posts
    56
    Utilities
    (required to run this program, place it in the same directory)

  6. #6
    Registered User
    Join Date
    Dec 2003
    Posts
    56
    contents of one of the input files:

    Customer_One 1234 11/26/03 2000.98
    102 50.00
    101 2.97
    104 15.98
    110 306.23
    109 12.50
    107 45

  7. #7
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Quick glance shows that when you read in the data from the file, you read in CustomerName, then AccountNumber, then BeginningBalance. However, in your sample input, the first line has CustomerName, then AccountNumber, then a date, then BeginningBalance.

    I would remove the date from the input file to test your current code, then add code to read in the date. It is harder since it is a number, then character ('/'), then number, etc.

  8. #8
    Registered User
    Join Date
    Dec 2003
    Posts
    56
    aw man... thanks, that would have totally escaped me... I am past the hanging, I'll have to implement the change.

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. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. inputting time in separate compilation
    By sameintheend01 in forum C++ Programming
    Replies: 6
    Last Post: 03-13-2003, 04:33 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM