okay so im writing some code for this program that selects a random word based on which difficulty you choose. thats not important. what is supposed to happen is the user should be able to guess a letter and the program should search the randomly selected word and determine whether or not the chosen word contains the guessed letter.
now i have experience in visual basic. and i understand the concepts of each different types of loops and sorting procedures, the only problem is that my teacher is in the hospital and i am unaware of how to apply these concepts to C++
if i could get some help from sombeody that would be fkn amazing, K~thx
(also im not exactly sure which version of C++ this is, ill look into this and get back to you, im almost certain its like C++.net or something, its microsoft visual studio . net 2003 so good luck xD)
Step 1: Take the random word and load it into a char array or string your choice really.
Step 2: Prompt for and recieve user input.
Step 3: Send each letter the user inputs through a recursive loop comparing it to each letter in the choosen word and determine whether or not the chosen word contains the guessed letter.
xD sounds simple enough but i am unsure of how to tell the computer which letter in the selected word to compare it to. like; how would i get it to move along each char in the string? in visual basic this involves things like "word.length" and what not.
wait; looked at the code, conferred with peers, i need an array where each letter within the word is an element, so i can then make a loop to compare the letter to each element.
only problem. . . . . . my teacher wasnt able to tells us how to create arrays in C++,
i know how to do these is Visual Basic. but since this school sux and only hosts programming 3&4 like once every 2-3 years we have no text books. if some1 could maybe post some generic code for creating an array that would be greatly appreciated! =D
arg. it didnt work, but since my code is vastly over complicated i believe im doing something wrong. . . here's the spot that is giving me trouble. . .
if ((difficulty=='e') || (difficulty=='E'))
{
wordlength=2;
char blanks[2] = {'-','-'};
//trying to set like a system of blanks so you know which letters you've guessed correctly
if (whichword==1) //whichword is a random number which chooses your word
{
word="hi";
char easyword[2]={'h','i'};
}
else if (whichword==2)
{
word="at";
char easyword[2]={'a','t'};
}
else if (whichword==3)
{
word="go";
char easyword[2]={'g','o'};
}
cout << "Please guess a letter.";
cin >> guess;
for (int y=1; y<=wordlength; y++)
{
//error C2065: 'easyword' : undeclared identifier (VVV that line)***********************
if (guess == easyword[y])
correct+=1;
}
}
its probably obvious that i dont know what im doing, but i usually tend to overcomplicate my code, i get the same outcome most of the time though.
if some1 would generously take the time and possibly write me some psuedo-code and provide a layout of how i should go about this it would be both awesome and greatly appreciated.
You are declaring the variable easyword inside a set of braces {}. This means that when the end of those braces is reached, that variable will disappear. So when the compiler reaches the loop at the end, it doesn't know what that variable is. The solution is just to put the declaration of easyword before the if statements.
thank you very much, that makes actual sense. so declare the array along with the other variables up at the top, and then inside each set of brace{} i assign the characters to the array? so like. . . .
srand(int(time(o)));
char easyword[2];
(other variables)
if ((difficulty=='e') || (difficulty=='E'))
{
wordlength=2;
char blanks[2] = {'-','-'};
//trying to set like a system of blanks so you know which letters you've guessed correctly
if (whichword==1) //whichword is a random number which chooses your word
{
word="hi";
char easyword[2]={'h','i'}; //but when it gets here
}
else if (whichword==2)
{
word="at";
char easyword[2]={'a','t'}; //here
}
else if (whichword==3)
{
word="go";
char easyword[2]={'g','o'}; //or here,
}
cout << "Please guess a letter.";
cin >> guess;
for (int y=1; y<=wordlength; y++)
{
if (guess == easyword[y]) //will the characters assigned still be there when it gets to here?
correct+=1;
}
}
technically you could use a std::string object instead of an array. It has the ability to return specific characters given an index and has a built-in function that tells you its length. Makes it a little bit easier to work with strings, assuming you're even allowed to use them for the assignment.
two important functions to note:
operator[] Get character in string (public member function)
(This makes it act like an array of chars as well as having built in functions of a class)
length Return length of string (public member function)
(This means you dont need to keep track of array length in a separate variable if you dont feel like it)
I feel stupid writing so much on my own thread. . . . but i changed it again so simplify it all.
okay scratch the random word. ill just preset the word array to awesome.
this is what i have so far now that i started over, it works, to some degree but i need to figure out a way to leave the letters previously guess that were correct up on the board, each time it runs through the loop any letter that wasnt the letter in the array becomes a blank.
do i need to set each element in the array as a variable; and instead of displaying blanks and correct guesses display the variable? (all variables start as blanks until guessed correctly)
this is the code i wrote this hour xD
int wordlength=7;
int flag=54321;
char guess;
char wordchose[7]={'a','w','e','s','o','m','e'};
string word="awesome";
while (flag!=12345)
{
cout << "the word is " << word << endl;
cout << "please guess a letter!";
cin >> guess;
Also if for some reason you REALLY need to work with a standard c string (char array)
std::string has a function to get its contents in that form. http://www.cplusplus.com/reference/string/string/c_str/
const char* c_str ( ) const;
you would call it as word.c_str();
Since it is const char* array, you cannot modify the contents, only use them or assign them to another variable.
Sounds good, let me test this out tomorrow in class. I don't have the software on my home computer so I am only able to test this while I'm in class xD
(And technically this isn't an assignment, its just a game I'm working on because I'm done with all my assignments. I just wanted something to impress my teacher with when she gets back from the hospital.)
So really, I can use any format I choose, as long as it will get the job done I'm MORE than satisfied. I'll take time to research those links and test out the different formats before I post again.
Thx kotoro, slicedpan, & Computergeek01. At the least now I'm able to grasp these concepts and apply them accordingly.
looks good, the string functions similar to the array, i just need to re-think this code a bit.
im thinking about making 7 char variables and starting tham all as '_' ,
then when the user guesses a letter correctly the variable in that position becomes the letter and i display the 7 variables in order to show which letters have been guessed so far.
ill work on this more once i get into class. O.~ thx for the help
WOOT! Got it to work! And now I understand arrays and strings better. Thank You everyone who helped!
This is my code if anyone is interested xD basically, it selects a random 11 letter word out of 20 that I chose. Next I think I'll attempt to apply a difficulty setting which would alter the length of the word.
FINAL QUESTION(s): Is there a simpler/shorter way to have it randomly choose a word without having to make multiple if statements or stream data from a file?
(Also, is there a way to remove this thread from the forums? There is no need for it any longer.)
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main()
{
srand(int(time(0)));
int correctletters=0;
int whichword;
char guess;
char blanks[11]={'-','-','-','-','-','-','-','-','-','-','-'};
string word;
whichword=1+rand() % (20-1+1);
if (whichword==1)
word="afterburner";
if (whichword==2)
word="equilibrium";
if (whichword==3)
word="opinionated";
if (whichword==4)
word="jeopardized";
if (whichword==5)
word="predictable";
if (whichword==6)
word="rectangular";
if (whichword==7)
word="revolutions";
if (whichword==8)
word="formulation";
if (whichword==9)
word="squishiness";
if (whichword==10)
word="sympathetic";
if (whichword==11)
word="technically";
if (whichword==12)
word="extradition";
if (whichword==13)
word="participate";
if (whichword==14)
word="paratrooper";
if (whichword==15)
word="moonshiners";
if (whichword==16)
word="loudspeaker";
if (whichword==17)
word="lieutenancy";
if (whichword==18)
word="incoherence";
if (whichword==19)
word="hypothermia";
if (whichword==20)
word="fortunately";
while (correctletters!=word.length())
{
cout << "please guess a letter!";
cin >> guess;
cout << endl;
for (int x=0; x<word.length(); x++)
{
if (guess==word[x])
{
blanks[x]=guess;
correctletters++;
}
cout << blanks[x] << " ";
}
cout << endl << "You have " << correctletters << " of the " << word.length() << " letters correct!" << endl;
}
cout << endl << "Congratulations!!! My program works, I'ma Beast, You're Not," << "\t" << "=P" << endl;
return(0);
}
If you refer to the guides listed on this website, using the wonderful search feature, you would have found this one. http://www.cplusplus.com/doc/tutorial/files/
I believe it will have what you are searching for.