C++ Read String in File Into 2d Array
Reading file into 2d array, separated by comma
Hi, then my assignment is to create a French to English translator. My get-go job is to have a file and put the 1000 English language and French words into a second array. The file looks like this:
French 1000
bonjour,hello
oui,yes
non,no
merci,thanks
I've tried using getline to aid me separate the two, but I'm not sure its actually storing anything into the assortment, because I volition demand to sort the array afterward I create information technology. When I try to cout the array information technology gives me a memory address, so I'yard confused on what I need to practise.
| |
Last edited on
Line 16 attempts to extract ii whitespace-delimited tokens, but your example only shows 1000, and and then each "langArray" line.
The other upshot is that you don't have a continuous streak of comma-delimited tokens. Your actual sequence, after the thousand, is { word comma word newline word comma discussion newline ... }
So you lot need to call getline(inFile, line, ',') the first time to get the french word, and and so call getline(inFile, line) (without the comma delimiter) to get the english word.
Also, VLAs (Variable-Length Arrays, your line 19) are not standard C++; prefer a vector if the size of the array is not known at compile-time.
Last edited on
Hello fake sushi,
This is not valid in C++ string langArray[2][num];
. "num" needs to exist defined as a constant or simply use 1000. The only way to use this is with a dynamic array. Also information technology should be string langArray[one thousand][ii];
.
I tried to figure out your for loops, but they are wrong and I demand some more time to work on it.
In the inner for loop y'all are using the if argument to read the file, simply you merely read the commencement office of a line, but not the second office. Reading the second office is like the starting time read, merely with out the 3rd parameter in the getline.
I volition load this into something I can examination and let yous know.
Andy
@Handy Andy
I would really capeesh the aid, I met with my tutor and he was saying it was alright but its obviously non. I'thou very new to c++ and this is a information structures and algorithms class thats kicking me right now. I've been looking things upward for this and I don't become it.
Perchance your tutor has different ideas well-nigh how C++ should be taught and also what should be used.
Since yous are at his mercy stick to his advice and what he wants.
You lot can learn things properly afterwards.
Howdy imitation sushi,
Since I did not have plenty of your code I created a full programme to test the code. Most of what is in "main" can be copied to your function, but some may have to reside outside the function.
The comments tell you what I added or changed. I remember I got everything.
This is based on what you started with:
| |
The for loop was a adept kickoff, simply you lot are over complicating the whole. The code higher up tin be washed in 1 for loop although it is different from a normal for loop.
The if argument is not needed considering the reads are done in the for condition and therefor the loop will end when (eof) is set.
In that location are two different for loops and either will work. I left them both so you tin see the difference.
Lines 54 - 74 I used for testing and are not required or needed. Line 68 on may be useful to you in the future.
Andy
Edit:
This produces the output of:
File successfully processed! // <--- I think you mean opened. If not information technology is in the wrong place. one. bonjour hello ii. oui aye 3. non no four. merci thanks For checking assortment contents. one. bonjour how-do-you-do 2. oui yes iii. not no 4. merci cheers Press Enter to continue:
Concluding edited on
I would recommend using a single dimensional array of a struct/grade instead of the 2d array. Something like:
| |
| |
Unfortunately, this is not standard C++ - although some non-conforming compilers permit it. For standard C++, the size of the assortment has to be known at compile fourth dimension. If the size is only known at run-time (such equally here when reading a file), and so using a vector instead is the way to go.
| |
Yet, as the aim is to create a translator then at some point y'all'll need to wait upwards a French give-and-take to convert to English language. The easiest style to facilitate a await-up like this is to apply a std::map. Consider:
| |
Topic archived. No new replies immune.
pattersonitth1992.blogspot.com
Source: http://www.cpp.re/forum/beginner/273705/
0 Response to "C++ Read String in File Into 2d Array"
Post a Comment