3 Replies - 4031 Views - Last Post: 26 November 2012 - 11:18 AM Rate Topic: -----

#1 lateshapowell3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 17-October 12

word scramble game

Posted 21 November 2012 - 11:08 AM

//LaTesha Powell
//December 5 2012
//Word Guess Game

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

using namespace std;

int main()
{
	const string WORD;
	const int NUM_WORDS = 20;
	const int MAX_GUESSES = 20; 
	string WORDS;
	int points = 0;
	
	ifstream inputFile;
	string guess;
	string filename;
	
	char again;
	char dummyvar;

	//Get the filename from the user.
	cout << "Enter the filename: ";
	cin >> filename;

	//Open the file
	inputFile.open(filename.c_str());

	//if the file successfully opened, process it.
	if(inputFile)
	{
		
		while(inputFile >> WORDS)
		{
		


		}

			

		//Close the file.
		inputFile.close();

	}
	else 
	{ 
		//Display an error message.
		cout << "Error opening the file.\n";
	}

	do
	{
		srand(time(0));  
	   int choice = (rand() % NUM_WORDS);  
       string theWord = WORDS [choice][WORD]; // word to guess 


		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 '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 == theWord)  
		  {  

            cout << "\nThat's it! You guessed it!\n";  

          }//end if statement 

          else 
			{  
				cout << "Sorry, that's not it.";  

            }//end else statement  

        }//end for  

		 cout << "\n\n\nWould You Like To Play Again? (y/n): ";  
		 cin  >> again;  
     }
	 while(again == 'y' || again == 'Y');  
	 
	 system("Pause");
	 system("cls");  

		cout << "Thanks for playing!";  

	return 0;  

 }//end main 

	
	

In this program I am supposed to have a scrambled word get displayed to the user and then have them guess the word or give up. If they guess the word they win the bet and 1x their bet. If they give up they lose the bet completely. I am trying to use parallel arrays, but the topic of arrays is fairly new to me so I am having some trouble.

Is This A Good Question/Topic? 0
  • +

Replies To: word scramble game

#2 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,176
  • Joined: 08-January 12

Re: word scramble game

Posted 21 November 2012 - 11:23 AM

What trouble are you having? Neither are we psychics, nor are we standing behind you. So, if you get any errors, post them. If you have any doubt/error, then ask about it.
Was This Post Helpful? 0
  • +
  • -

#3 lateshapowell3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 17-October 12

Re: word scramble game

Posted 26 November 2012 - 11:13 AM

View Postaresh, on 21 November 2012 - 11:23 AM, said:

What trouble are you having? Neither are we psychics, nor are we standing behind you. So, if you get any errors, post them. If you have any doubt/error, then ask about it.

//LaTesha Powell
//December 5 2012
//Word Guess Game

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

using namespace std;

int main()
{
	
	const int NUM_WORDS = 20;
	string WORDS;
	
	
	ifstream inputFile;
	string guess;
	string filename;
	
	char again;
	char dummyvar;

	//Get the filename from the user.
	
	//Open the file
	inputFile.open("Word_list.txt");

	//if the file successfully opened, process it.
	if(inputFile)
	{
		
		while(inputFile >> WORDS)
		{
		
			

		}

		//Close the file.
		inputFile.close();

	}
	else 
	{ 
		//Display an error message.
		cout << "Error opening the file.\n";
	}

	do
	{
		srand(time(0));  
	        int choice = (rand() % NUM_WORDS);  
                string theWord = WORDS; // word to guess 


		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" << endl << endl;  
			cout << "Unscramble the letters to make a word.\n" << endl << endl;  
		        cout << "Enter 'quit' to quit the game.\n\n" << endl << endl;  
			cout << "The jumble is: " << WORDS << endl << endl;

			cin >> guess;

        
		{  

         
		  if (guess != WORDS)  
		  {  

            cout << "\nThat's it! You guessed it!\n";  

          }//end if statement 

          else 
			{  
				cout << "Sorry, that's not it.";  

            }//end else statement  

        }//end for  

		 cout << "\n\n\nWould You Like To Play Again? (y/n): ";  
		 cin  >> again;  
     }
	 while(again == 'y' || again == 'Y');  
	 
	 system("Pause");
	 system("cls");  

		cout << "Thanks for playing!";  

	return 0;  

 }//end main 

	
	


The trouble I am having now is that it just prints the same word to guess from over again instead of another word. there are 20 words in my text document, but it skips to the last word to use and never chooses another one. Even if you type in yes you want to play agian it prints back out the same word. I believe the problem may be somewhere in here
do
	{
		srand(time(0));  
	   int choice = (rand() % NUM_WORDS);  
       string theWord = WORDS; // word to guess 


		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" << endl << endl;  
			cout << "Unscramble the letters to make a word.\n" << endl << endl;  
		        cout << "Enter 'quit' to quit the game.\n\n" << endl << endl;  
			cout << "The jumble is: " << WORDS << endl << endl;

			cin >> guess;

        
		{  

but I do not know how to remedy the situation with the program there.

I have attached my wordlist if you would like to take a look at it and a screen shot of what the screen looks like when I execute the code more than once.

Attached image(s)

  • Attached Image
  • Attached Image

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#4 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,176
  • Joined: 08-January 12

Re: word scramble game

Posted 26 November 2012 - 11:18 AM

WORDS is a string. So, every time you read a word into it, it overwrites the last word, and hence you have only 1 word to guess from. You can use an array of words for your purpose. Also, srand() should be called only once in your entire program, so it is not something you should have in a loop. Last but not the least,
if (guess != WORDS) 
{ 
    cout << "\nThat's it! You guessed it!\n"; 
}//end if statement

I guess you meant if (guess == WORDS)?
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1