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

using namespace std;

//Types and Arrays
string word[16] = {"GOLDFISH", "COMPUTER", "ANONYMOUS", "JACKET", "SHARP", "SERVICE", "EFFORT", "CHARACTER", "CHANGE", "WITHOUT", "PRODUCT", "UNFOLDING", "MUSIC", "MOMENT", "LIFETIME", "PROVIDE"}; // words that will be randomized.
string hint[16] = {"Small colorful fish.", "A machine for performing calculations automatically.", "Having no known name or identity or known source.", "A short coat.", "Abrupt, extremely steep.", "Work done by one person or group that benefits another.", "Attempt: earnest and conscientious activity intended to do or accomplish something.", "An imaginary person represented in a work of fiction (play, film or story)", "Make different; Cause a transformation.", "Lacking something.", "Merchandise, commodities offered for sale.", "A developmental process.", "An artistic form of auditory communication incorporating instrumental or vocal\ntones in a structured and continuous manner.", "A particular point in time.", "The duration of the life of someone or something.", "Give what is desired or needed, especially support, food or sustenance."}; // hints and definitions courtesy of google define.
string guess[50]; // THIS IS WHERE I DECLARE THE PART THAT HAS THE PROBLEM
static const char Y= 'Y';
static const char N= 'N';
char ans;

// Functions
int instructions();
int game();



int main()
{
	cout << "\tWelcome to Word Jumble By Khoanyneosr!\n";
	cout << "\nWould you like to see the instructions?"
		 << "\n\t1) Yes = Y"
		 << "\n\t2) No = N\n- ";
		 bool rep= true;

   while (rep)  {
      cin >> ans;
	  cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
		
      switch (toupper(ans)) {
         case Y:
			 cin.clear();
			 cin.sync();
            instructions();
            rep= false;
            break;
         case N:
			 cin.clear();
			 cin.sync();
            game();
            rep= false;
            break;
         default:
			 cout << "\n\nInvalid input.\n\n- ";
			 cin.clear();
            rep= true;
            break;
       }
    }
	return 0;
}
int instructions()
{
	system("cls");
	cout << endl << endl << "\tINSTRUCTIONS" << endl;
	cout << endl << "1 - You will be presented with a word. Your job is to guess it." << endl;
	cout << endl << "2 - At the beginning of the game your given 10 points." << endl;
	cout << endl << "3 - You will be given 1 hint. Everytime you have to revisit\nthe hint 1 point will be deducted." << endl;
	cout << endl << "4 - For everytime you guess wrong 1 point will be deducted." << endl;
	cout << endl << "5 - When your point value reaches 0, the game is over." << endl;
	cout << endl << "You can exit at anytime by pressing Q." << endl;
	cout << endl << endl << "\tGood Luck!" << endl << endl << endl;

		system("pause");
		return game();

}
int game()
{
		system("cls");

		srand(time(0));
     string s = word[ rand() % 16 ];
     random_shuffle(s.begin(), s.end());
     cout << endl << "Scrambled word: " << s << '\n';


			cout << endl << "what is your guess?\n- "; // THIS IS THE PART!
			cin >> guess[50];

			cout << endl << guess[50] << endl << s << endl; // THIS IS WHERE IT ENDS!

		return 0;
}

That's my code. I need to figure out how to create an If else statement to compare the input of the user in the GAME(); function and the actual word that the program randomized. For some reason when i try to put the IF (GUESS[50] = S) it says
"theres no way to compare type char and type string." So i've been swapping around types but i can't figure it out. What should i do? Thanks in advance

Recommended Answers

All 2 Replies

guess is declared to have a size of 50, but ranges from 0 - 49, right? You're trying to read into guess[50] which should be outside of the range.

Another thing, why declare a size of 50 if you're only apparently using one string to hold the guess? Keep in mind that string holds 50 individual strings and not a string of length 49.

1) Be very careful, 'word' may be a reserved name on some systems.

2) You have declared an array of strings for 'guess' but I think you just want it to be one string? As Chilton pointed out, you have done this cin >> guess[50]; when I think you just wanted cin >> guess[0]; which really means you should have done:

string guess;
...
cin >> guess;

David

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.