0

everyone. I'm working on a language translator program, my first task is to create a 2D array of French and English words from a file of 1000 translations. The file looks something like this:

1000
bonjour,hello
oui,yes
non,no
merci,thanks

this is what I've tried:

string langArray[2][num];
    for(int i = 0; i < 2; i++)
    {
        for(int j = 0; j < 1000; j++)
        {
            inFile >> langArray[i][j];
            cout << langArray[i][j] << " " << endl;
        }
    }

but its storing the whole line and not the words separately, so I need to figure out how to store the French words on one line of the array and the English words in the second line without that pesky comma.

Alexander
  • 127
  • 1
  • 10
  • 1
    Use `std::getline()` with a `','` delimiter instead of `inFile >> ` – πάντα ῥεῖ Oct 26 '20 at 17:16
  • @user4581301 asking on SO is typically my last resort, I keep finding people using vectors or people wanting to put things into 2D arrays that are separated by spaces rather than commas, which would be a lot easier but I can't modify the file – Alexander Oct 26 '20 at 17:18
  • How to do that with a 2d array is a detail that's easy to extrapolate. If your problems tend more to appear how to manage the 2d array correctly, use a `std::vector>` instead, that will be way easier to handle. Also your code looks like you want to use a dictionary (aka. `std::map`). – πάντα ῥεῖ Oct 26 '20 at 17:20
  • Here's the base of my reseach I've done, if that helps more: regarding specific cases: https://www.google.com/search?rlz=1C1CHBF_deDE833DE833&sxsrf=ALeKk02BMF9ANRzRI4RTOt3GGoaCQxm9yQ%3A1603729729102&ei=QfmWX5TWBc3osAfpnbLgBg&q=site%3Astackoverflow.com+%22c%2B%2B%22+read+comma+separated+file&oq=site%3Astackoverflow.com+%22c%2B%2B%22+read+comma+separated+file&gs_lcp=CgZwc3ktYWIQAzoECAAQR1C_hrABWMOzsAFgvL-wAWgAcAJ4AYAB1QGIAa4ZkgEGOS4xNS4xmAEAoAEBqgEHZ3dzLXdpesgBCMABAQ&sclient=psy-ab&ved=0ahUKEwjU-sXm1tLsAhVNNOwKHemODGwQ4dUDCA0&uact=5 – πάντα ῥεῖ Oct 26 '20 at 17:24
  • @πάνταῥεῖ I'm not allowed to use vectors or maps unfortunately, I should've specified that in the question. It would probably make this easier if I could. – Alexander Oct 26 '20 at 17:26
  • 1
    So do as I recommended in my 1st comment, or aren't you allowed to use `std::getline()` as well? Your problem is that `std::istream& operator>>(std::istream&, std::string)`reads up to the 1st whitespace character met, and not considers a `','` as a delimiter. – πάντα ῥεῖ Oct 26 '20 at 17:31

0 Answers0