Reading several lines from file

Hi again,

Finally got my linker error solved and now am having trouble when reading data in from a file. The data file reads in an integer, a string, and then two more integers. It compiles okay, but when running the output all that is displayed is the first integer, first word in the text and then memory addresses from there on out. I assume this has something to do with how the string is being read in from the file. My professor also mentioned that since there are spaces and commas used in the .txt file that we will need to use a delimiter. Can anyone help? Here is the code that is giving me trouble:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
int loadVehicles(int numItems, int carType[], string carName[], float costMaterials[], float numHours[])
{
   numItems = 0;
   int count;
   ifstream inputFile;

   inputFile.open("cars.txt");

   for (count = 0; count < 100; count ++)
   {
      inputFile >> carType[count] >> carName[count] >> costMaterials[count] >> numHours[count];
   }

   inputFile.close();

   // Test
   cout << "The vehicles are: ";
   for (count = 0; count < 100; count ++)
   {
      cout << carType[count] <<" "<< carName[count] <<" "<< costMaterials[count] <<" "<< numHours[count];
      cout << endl << endl;
   }

   count = numItems;

   cout << numItems << endl << endl;

   return numItems;

}


Any help is greatly appreciated. I have already had success in such a short time here on the forum, and am extremely grateful for it. Thanks for helping me out guys!

Cheers,

Zach

P.S. Here is the text that the function is supposed to read in:

1 Aston Martin One-77, 189000 10
1 Lamborghini Murcielago, 22000 10
2 Ferrari F430 Spyder, 14500 10
2 Lamborghini Gallardo Spyder, 18600 10
3 Aston Martin Rapide, 18400 10
Last edited on
You need to use get line when you read into strings. Spaces act like separators, so the first word is the string, and it moves on to reading in another string into an int array. That's why you get an error.

Your code inside the for loop should look like this:

1
2
3
inputFile >> carType[count] ;
getline(inputFile, carName[count]) ;
inputFile >> costMaterials[count] >> numHours[count] ;
Alex didn't look at your text input example, his code wouldn't work correctly.

I would suggest using this:
1
2
3
4
5
inputFile >> carType[count] ;
inputFile.get()   ///this reads in and discards the space so it doesn't interact with getline
getline(inputFile, carName[count],' ') ;   ////the third argument sets the delimiter so getline doesnt
                                                              ////eat the string and the 2 remaining floats.
inputFile >> costMaterials[count] >> numHours[count] ;


Input can be a real pain in the butt, but its supremely customizable.
flclempire:

You are certainly correct on the input being a pain, sir. I replaced my code with your suggestion, but I am still getting the memory addresses after the first word is read in. Once it encounters the whitespace it begins to print out the address. The output looks something like this:

The vehicles are:
1 Aston 1.07575e-038 3.76244e-039

10289756 0 3.76298e-039

and so on and so forth.

I'm banging my head against the wall on this one. Is there a way to get the loop to ignore the white space and read into the string just as it would if it were input received from the user? I'm struggling here.

Zach
Oh, ha, I see. Sorry, I didn't realize that the car names could be more than one word. Its only taking in the first word and then trying to assign the second part of the car name to your float array.
Here:
1
2
3
4
inputFile >> carType[count] ;
inputFile.get()   ///this reads in and discards the space so it doesn't interact with getline
getline(inputFile, carName[count],',') ; 
inputFile >> costMaterials[count] >> numHours[count] ;


I didn't read your input text carefully enough, sorry. This delimits the comma. The >> operator takes care of the following white spaces for your floats.
Tell me if that works like you need it to.
Last edited on
Brilliant! It's now reading them all in, but as soon as the read is complete it's throwing up the memory addresses again. I'm headed to a tutoring session here in a little while--if I come up with something else I'll be sure to post.

Thanks again, I can't explain how much your help is appreciated.

Zach
Topic archived. No new replies allowed.