Reading Line by Line From an Input File Into a 2d Array
Reading file into 2nd array, separated by comma
Hi, and so my assignment is to create a French to English translator. My first task is to accept a file and put the chiliad English and French words into a 2nd assortment. The file looks like this:
French k
bonjour,hello
oui,aye
not,no
merci,thanks
I've tried using getline to help me separate the two, but I'm non certain its actually storing anything into the array, because I will demand to sort the array later I create it. When I try to cout the array information technology gives me a memory accost, so I'thou confused on what I need to do.
| |
Last edited on
Line sixteen attempts to extract two whitespace-delimited tokens, but your instance only shows 1000, and then each "langArray" line.
The other issue is that yous don't take a continuous streak of comma-delimited tokens. Your bodily sequence, later the 1000, is { word comma word newline word comma word newline ... }
Then you demand to telephone call getline(inFile, line, ',') the first time to become the french word, then call getline(inFile, line) (without the comma delimiter) to get the english language word.
Also, VLAs (Variable-Length Arrays, your line xix) are not standard C++; prefer a vector if the size of the assortment is not known at compile-time.
Last edited on
Hullo false sushi,
This is not valid in C++ string langArray[2][num]; . "num" needs to exist defined as a constant or just utilise 1000. The only way to use this is with a dynamic assortment. Besides information technology should exist string langArray[chiliad][2]; .
I tried to figure out your for loops, but they are wrong and I need some more time to work on information technology.
In the inner for loop you are using the if argument to read the file, but you only read the kickoff office of a line, but not the 2d part. Reading the 2nd part is like the first read, but with out the 3rd parameter in the getline.
I volition load this into something I can test and let you lot know.
Andy
@Handy Andy
I would actually capeesh the help, I met with my tutor and he was saying it was alright just its obviously not. I'yard very new to c++ and this is a data structures and algorithms form thats boot me correct now. I've been looking things up for this and I don't go it.
Maybe your tutor has unlike ideas about how C++ should be taught and likewise what should be used.
Since you are at his mercy stick to his communication and what he wants.
You can learn things properly later.
Howdy simulated sushi,
Since I did non have plenty of your code I created a full program to exam the code. Most of what is in "main" can be copied to your part, merely some may have to reside outside the function.
The comments tell you what I added or inverse. I recollect I got everything.
This is based on what you started with:
| |
The for loop was a practiced showtime, merely you are over complicating the whole. The code to a higher place tin can be done in i for loop although information technology is different from a normal for loop.
The if argument is not needed because the reads are done in the for status and therefor the loop will stop when (eof) is set.
There are 2 different for loops and either will work. I left them both and so yous tin can run across the difference.
Lines 54 - 74 I used for testing and are not required or needed. Line 68 on may be useful to y'all in the future.
Andy
Edit:
This produces the output of:
File successfully processed! // <--- I recall you mean opened. If not it is in the incorrect place. i. bonjour hello two. oui aye 3. non no four. merci thanks For checking array contents. 1. bonjour hello two. oui yes 3. not no four. merci cheers Press Enter to continue:
Last edited on
I would recommend using a single dimensional array of a struct/course instead of the 2d array. Something like:
| |
| |
Unfortunately, this is non standard C++ - although some not-conforming compilers allow it. For standard C++, the size of the assortment has to exist known at compile time. If the size is only known at run-time (such as hither when reading a file), so using a vector instead is the way to go.
| |
Even so, every bit the aim is to create a translator then at some point you'll need to look up a French discussion to convert to English. The easiest way to facilitate a look-up like this is to employ a std::map. Consider:
| |
Topic archived. No new replies immune.
Reading Line by Line From an Input File Into a 2d Array
Source: http://www.cpp.re/forum/beginner/273705/
0 Response to "Reading Line by Line From an Input File Into a 2d Array"
Post a Comment