2 Replies - 1482 Views - Last Post: 05 January 2010 - 05:14 PM Rate Topic: -----

#1 ndiamond  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 05-January 10

How to read in text from a file and store into a 2D string array?

Posted 05 January 2010 - 04:27 PM

Hi All ! I came across this code for word jumble game, i've edited it a bit to my liking but I cannot for the life of me figure out how to read in the words and hints from a text file into a string array callec WORDS, rather than having them set at design-time. Could someone help me out on how to do this! thanks so much!

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
	enum fields {WORD, HINT, NUM_FIELDS};
	const int NUM_WORDS = 5;
	const int MAX_GUESSES = 5;
	const string WORDS[NUM_WORDS][NUM_FIELDS] =
	{
	{"wall", "Do you feel you're banging your head against something?"},
	{"glasses", "These might help you see the answer."},
	{"labored", "Going slowly, is it?"},
	{"persistent", "Keep at it."},
	{"jumble", "It's what the game is all about."}
	};

	string guess;
	int points = 0;
	char another;


	do
	{
		system("cls");
		srand(time(0));
		int choice = (rand() % NUM_WORDS);
		string theWord = WORDS[choice][WORD]; // word to guess
		string theHint = WORDS[choice][HINT]; // hint for word

		string jumble = theWord; // jumbled version of word
		int length = jumble.size();

		for (int i=0; i<length; ++i)
		{
			int index1 = (rand() % length);
			int index2 = (rand() % length);
			char temp = jumble[index1];
			jumble[index1] = jumble[index2];
			jumble[index2] = temp;
		}
		cout << "\t\t\tWelcome to Word Jumble!\n\n";
		cout << "Unscramble the letters to make a word.\n";
		cout << "Enter 'hint' for a hint.\n";
		cout << "Enter 'quit' to quit the game.\n\n";
		cout << "The jumble is: " << jumble;
   
		for(int nIndex = 0; nIndex < MAX_GUESSES; nIndex++)
		{
			cout << "\n\nGuess " << nIndex+1 << ": ";
			cin >> guess;

			if (guess == "hint")
			{
				cout << theHint;
				points--; //subtract 1 point
			}//end if
			else if (guess == theWord)
			{
				cout << "\nThat's it! You guessed it!\n";
				points += guess.size(); //points = # of letters in word
				break;
			}//end else if
			else
			{
				cout << "Sorry, that's not it.";
			}//end else
		}//end for
		cout << "\n\nYour Total Points Are: " << points;
		cout << "\n\n\nWould You Like To Play Again? (y/n): ";
		cin  >> another;
	}while(another == 'y' || another == 'Y');

	system("cls");
	cout << "Thanks for playing!";

	return 0;
}//end main



Is This A Good Question/Topic? 0
  • +

Replies To: How to read in text from a file and store into a 2D string array?

#2 tommynator128  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 8
  • View blog
  • Posts: 127
  • Joined: 05-January 10

Re: How to read in text from a file and store into a 2D string array?

Posted 05 January 2010 - 04:42 PM

if you wan't to read them out of a txt file i would create a file like this
wall
Do you feel you're banging your head against something?
glasses


and so on....
then i would read in until a /n character comes and split the strings there like this
string line;
ifstream myfile ("test.txt");
if (myfile.is_open())
{
	while (! myfile.eof() )
	{
		getline (myfile,line);
		cout << line << endl;
	}
	myfile.close();
}


This post has been edited by tommynator128: 05 January 2010 - 04:45 PM

Was This Post Helpful? 0
  • +
  • -

#3 Paul-  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 61
  • View blog
  • Posts: 260
  • Joined: 11-December 09

Re: How to read in text from a file and store into a 2D string array?

Posted 05 January 2010 - 05:14 PM

Here's a suggestion, following tommynator128's file format:
...
	ifstream infile;
	infile.open("test.txt", ifstream::in);

	do {
		string theWord, theHint;
		getline(infile, theWord);
		getline(infile, theHint);
...
	} while (...);
...
	infile.close();
	return 0;


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1