Read Word file into String Array

Hello, working on a school project and asking for sanity check of my code. I have a word file single column of 3 characters each that I have to read into a string array with a max size of 2000. Is my code logical? We have not covered anything beyond functions and arrays. This is a function that will be called to compare against a player entering up to 10 characters to print the legal 3-letter words to the screen. I've got several other functions as well but trying to work through the whole program. thanks in advance!

I added the cout in the code just to see if the contents would be displayed to the screen but have not tested it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  //read legal words function definition
void readLegalWords();
{
	wordsArray[2000];
	ifstream legalWords;
	legalWords.open("words.txt");
	if(!legalWords) //test if file is open
	{
		cout<<"error opening words file."<<endl;
		break;
	}

	int index = 0; //counter

	while(getline(legalWords, wordsArray[index]))
	{
		getline(legalWords, wordsArray[index]);
		index++;
		cout<<wordsArray[index];
	}
	legalWords.close();//close the file
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <string>
#include <iostream>
#include <fstream>

void readLegalWords();
{
	std::string wordsArray[2000]; //You need to specify the type of array
	std::ifstream legalWords;
	legalWords.open("words.txt");
	if (!legalWords) //test if file is open
	{
		std::cout << "error opening words file." << std::endl;
                //no need for a break here, after the program prints, it will leave these brackets.
	}

	int index = 0; //counter

	while (getline(legalWords, wordsArray[index]))
	{
		getline(legalWords, wordsArray[index]);
		std::cout << wordsArray[index];
	        index++; 
                //if you put index++ before the print, you are trying to print the non-existent
                // next word that you have not stored yet.
	}
	legalWords.close();//close the file
}


This should work fine, although I don't know how you plan to use it in the rest of your program.
Great! I feel a bit better about this project. Complicated for a beginner. The program ultimate goal is to prompt a player to enter 3-10 lowercase letters and compare it to the above array and print the possible 3-letter words to screen. Will probably ask for more code checks as I code more of this project. Again, thanks!
You need to pass in the wordsArray as a parameter. If it's a local variable then you'll read the words in, and then throw them away. Since you're using an array instead of a vector, you'll also need to tell it how many words were read:

The code actually stores every other word. Line 18 reads a word, then line 20 reads the next work and stores it in the same position in the array, overwriting the one stored at line 18.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Read the words file and populate wordsArray, which must be large enough to
// hold them all. Returns the number of words read or -1 on error.
int readLegalWords(std::string wordsArray[]);
{
	std::ifstream legalWords;
	legalWords.open("words.txt");
	if (!legalWords) //test if file is open
	{
		std::cout << "error opening words file." << std::endl;
		return -1;
	}

	int index = 0; //counter

	while (getline(legalWords, wordsArray[index]))
	{
		std::cout << wordsArray[index];
	        index++; 
	}
	legalWords.close();//close the file
	return index;
}

Topic archived. No new replies allowed.