I am trying to read data from a .txt doc line by line with delimiters. we also have to populate a vector with the info. I have tried everything I know to do, but no matter what I do it says "ERROR: Cannot open file". I saved the info into a new txt file and now my output is showing only the zipcodes of the people.Can someone please help me? I have been trying to do this for 3 days. all help is appreciated. here is my code.
Um... I think something got cut off. The actual meat of your program where it's supposed to open your text file seems to be missing, and you're also missing a semicolon and a closing bracket for main. That would be helpful to see. :)
If you use Visual C++ 2010 Express edition you should create, add the file you want to read in "VisualStudio 2010\Projects\ NameOfYourProject\ NameOfYourProject" where "NameOfYourProject" is the project you work on
It looks like in your while loop you are just overwriting the input string instead of adding to it, that would be why the variable input would only hold the last part of the line in the txt file (which is the zipcode). I suggest using an array of strings to hold the inputs, or to use a different variable for each input.
Do you have much experience with classes? Using class objects to hold that information would be your best option.
For right now here's a working example of an array, plus the fix to your file-reading issues. I'm going to be late for work so I didn't have time to set up a double-array but that's what you would need if you wanted to get this fully working. input[0][0], input[0][1], input[0][2] etc.
I'm actually not too great with classes myself, if anyone has a way to get the info from the file into variables for several objects using a loop I'd love to see it.
Thank you! Actually, I don't feel very confident with program writing at all. and I think we started learning about classes a few weeks ago in my class.
now about the array, I do know that arrays and vectors are similar, right? since I have to incorporate a vector in here at the end, how would I go about doing that?
OK, I'm going to have to look up a couple of things there, but if you just did what I think you did gtkano, then you just helped me with several programs that I've been having issues with. Thank you!
Edit; Yep, that's it! follow gtkano's code, it does what you are wanting. Thank you gtkano!!!
I didn't even know that you could make objects with arrays.
@blaa15 - gtkano made each class object so that it contains an array in the name, it is better than my suggestion of using a double arrays because it, well, it's better structured. I suggest writing cout<<person[1].firstName <<person[1].lastName<<person[1].address<<endl; after the dataFile.close() to show how you can look up the information after the file is closed now.