Input file not reading with function

This is a discussion on Input file not reading with function within the C++ Programming forums, part of the General Programming Boards category; I have to have this in at 12EST and my group has abandoned me completely for this project. I have ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    16

    Exclamation Input file not reading with function

    I have to have this in at 12EST and my group has abandoned me completely for this project. I have it down except that the information in my .txt file won't load correctly and I have no idea how to format it. Here is the code to read the file:

    Code:
    void initializeVectors(vector<string>& pID, vector<string>& pName, vector<int>& itemsOrdered, vector<int>& itemsInStore, vector<int>& piecesSold,
    				 vector<double>& manufacturePrice, vector<double>& sellPrice, int listSize)
    {
    	int count;
    	ifstream infile;
    	infile.open("C:\\Documents and Settings\\Andrea\\My Documents\\hardwareStore.txt");
    	for(count=0; count<listSize; count++)
    	{
    		infile>>pID[count];
    		infile>>itemsOrdered[count];
    		infile>>itemsInStore[count];
    		infile>>piecesSold[count];
    		infile>>manufacturePrice[count];
    		infile>>sellPrice[count];
    	}
    	for(count=0; count<listSize; count++)
    	{
    		infile>>pName[count];
    	}
    
    }
    Is it my code? Should I just separate the data with spaces in Notepad? Any help would be appreciated!

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    If there are listsize items in the file separated by spaces then that code will read them in. The code assumes that the ID, # ordered, # in store, #sold, man. price and sell price are in that order and separated by spaces, tabs or newlines. It also assumes that all the names are in order after the data above. It also assumes the names are all single words with no spaces.
    Code:
    1 5 7 2 3.99 5.99
    2 1 10 5 10.99 20.00
    3 4 11 1 0.99 2.99
    bait
    rod
    tackle

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    16
    Is there any way to use spaces for the input? I figured out my problem. The second string has a space "Cordless Drill" and I'm not sure how to get it to work with that. I tried the get function and it compiles with errors then. Is there any other way to get both words "Cordless Drill"?

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    Use getline instead of operator>>. This assumes the name is at the end of the line, since it will take everything up to the end of the line and place it in the string.

    Also, since you are using operator>> for the other values above, then you have to use inFile.ignore() to ignore any newline between the price data and the names. In this case you probably only need to call it once between the two loops. This is necessary because operator>> will leave the newline at the end of the line and getline will try to read it in.

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    16
    Thank you so much for your help!

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. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

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