Read Characters From the User Untill Eof
Input file into array until eof. Assist!
I am having an incredibly difficult fourth dimension wrapping my mind effectually this concept. Any assist is greatly appreciated.
I am request for aid in learning how to read an input file INTO an assortment that is not specified on how many values there are, Then until EOF(stop of file). What I take is this and it doesn't seem to display the file:
| |
I only have MAX set up to 100 because thats the maximum corporeality of entries for the file, but at any given time, the file could accept anything below 100. And then I need to acquire how to read the input file into an array until eof.
Thanks and then much for reading and whatsoever help you tin can contribute!
Arrays must take a size known at compile time. Don't use an array.
Utilize ane of the the Standard Library containers, like
std::vector.
| |
http://www.cplusplus.com/reference/vector/
http://en.cppreference.com/w/cpp/container/vector
Last edited on
Thanks for the input.
This is for a projection and unfortunately were told we cannot apply vector :/
Since your professor doesn't want y'all to learn C++, y'all volition have to do it the C way and dynamically allocate the array. I recommend reading the file twice - the first time to fine out how many values at that place are and the 2d fourth dimension to store them into the array that you dynamically allocate.
I call back he wants us to completely understand concepts before moving into other things. Like vector(I take no clue what that is yet)
I changed it to this. And although I am getting the data read from the file, information technology continuously reads repeated data until it hits the 100 value marker I believe.
Still having problem reading until EOF.
| |
while(fin >> masterList[count]) count++;
would probably exist fine. I wouldn't bother with MAX unless you are reading exactly that number.
Similarly, I think count = 0; while(!masterList[count].empty()) { cout << masterList[count] << " " << endl; count++; }
would be better than looping through potentially empty strings.
Last edited on
Thecal wrote: |
---|
I think he wants us to completely understand concepts before moving into other things. Like vector(I take no inkling what that is nonetheless) |
The C++ customs widely agrees that
std::vectorshould be taught earlier arrays. Your professor obviously doesn't agree (or doesn't even know he is in the minority).
Thecal wrote: |
---|
I changed it to this. And although I am getting the data read from the file, it continuously reads repeated data until it hits the 100 value mark I believe. Still having trouble reading until EOF. |
Don't think about EOF. Pretend it doesn't exist. Erase it from your listen. It is not important.
| |
Last edited on
Information technology didnt seem to like that. Compiled, but did not display the input file. Heres what I accept:
| |
ignore the comments, I commented out something I was trying.
Thecal wrote: |
---|
It didnt seem to like that. Compiled, only did not display the input file. |
The code I showed to you lot does not print anything out. I expected you to write that function of the code yourself; it is simple and you have shown the ability to do it in the past.
The file has to exist opened twice, that'south why the if statement was duplicated.
I couted the array on the lawmaking I pasted.
No, on line 33 y'all access an invalid index and print its value. You are unlucky that your program continues instead of crashing.
Printing out the unabridged array involves using a loop.
Ah. Which is what I commented out. I apologize. let me endeavour that.
WIth your to a higher place lawmaking, you only open the file in one case. After line 22, you are at the stop of the file stream and it is in an invalid state. Y'all so endeavor to use the stream in this invalid state on line 25.
Also, y'all need to look upwardly how to dynamically classify memory using new[]
and delete[]
- you should not take any
variable.
Last edited on
I took a different arroyo. Tried using the book to walk me through it. Did exactly what was said. The lawmaking is:
| |
Howerver I am getting some crazy long numbers, where as I should get the data from the file.
Somehow just non grasping this.
Legitimately tried it. Not getting anything.
| |
Said I needed to define array, so I did that, then said that the assortment had no value, and so I set size to 100. finally compiled. Spitting out naught.
Do non modify the content of the if statements, you are changing the logic of the plan.
Lines 6 and seven should not be.
Last edited on
Topic archived. No new replies immune.
Source: https://www.cplusplus.com/forum/beginner/162125/
0 Response to "Read Characters From the User Untill Eof"
Post a Comment