- Line 9 will not compile. why do you think is the reason?
- why are Lines 15-26 inside the switch-case? wouldn't it be better in a separate function?
- in fact, there is no need for a switch case: const string& wordpicked = words[pickword];
(also, if you want to be helped, please avoid multi-posting)
#include <cstdlib>
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
string words[]={cat, dog, mom, dad} ;
srand((unsigned)time(0)) ;
int pickword=rand()%4 ;
switch (pickword)
{
case 1:
int guessnumber ;
cout<<"______" <<endl ;
cout<<"| |" <<endl ;
cout<<"|" <<endl ;
cout<<"|" <<endl ;
cout<<"|" <<endl ;
cout<<"|" <<endl ;
cout<<"|" <<endl ;
cout<<"***" <<endl ;
char letter ;
cout<<"Guess a letter." <<endl ;
cin>> letter ;
if (letter=c)
{
cout<<"c**" <<endl ;
goto guess2 ;
}
if (letter=a)
{
cout<<"*a*" <<endl ;
goto guess3 ;
}
if (letter=t)
{
cout<<"**t" <<endl ;
goto guess4 ;
}
else
{
guessnumber++ ;
}
guess2:
cin>> letter ;
if (letter=a)
{
cout<<"ca*" <<endl ;
goto guess5 ;
}
if (letter=t)
{
cout<<"c*t" <<endl ;
goto guess6 ;
}
else
{
guessnumber++ ;
goto guess2 ;
}
guess3:
cin>> letter ;
if (letter=c)
{
cout<<"ca*" <<endl ;
goto guess5 ;
}
if (letter=t)
{
cout<<"*at" <<endl ;
goto guess7 ;
}
else
{
guessnumber++ ;
goto guess3 ;
}
guess4:
cin>> letter ;
if (letter=c)
{
cout<<"c*t" <<endl ;
goto guess6 ;
}
if (letter=a)
{
cout<<"*at" <<endl ;
goto guess7 ;
}
else
{
guessnumber++ ;
goto guess4 ;
}
guess5:
cin>> letter ;
if (letter=t)
{
cout<<"cat You win!" <<endl ;
break ;
}
else
{
guessnumber++ ;
goto guess5 ;
}
guess6:
cin>> letter ;
if (letter=a)
{
cout<<"cat You win!" <<endl ;
break ;
}
else
{
guessnumber++ ;
goto guess6 ;
}
guess7:
cin>> letter ;
if (letter=t)
{
cout<<"cat You win!" <<endl ;
break ;
}
else
{
guessnumber++ ;
goto guess7 ;
}
break ;
default:
cout<<"This word is unknown." <<endl ;
}
Thats for the word cat. All I have to do is add the steps for hanging the man, copy and paste in every else for each guess and I have finished for the word cat.
- in C++, = is assignment, == is comparison
- your 67 lines above is not a good approach - you will have to write crazy amounts of code every time someone adds a word
suggestion:
1. write a new small piece of code
2. make sure that piece of code compiles
3. if it does what you expect, goto 1
4. else debug until that new small piece of code works
this type of procedure is called "unit testing" and will give you a better chance of writing something that compiles and works
now you have a while() loop prompting the user for characters
there are two ways to break out of the while() loop: winning and losing (what are the conditions?)
test for these conditions inside the while until you either win or lose
I dont get it srry. Sometimes I don't understand things when I should, even though I'm really smart. Its annoying, and makes you feel stupid. Please help and explain.
Well put it this way. I get the part where it asks for a character to spell the word, but how to say if you get all the letters you win. How do you say "I got all the letters I need to put them together and make the word and I win" Just basically help. I'm probably confusing you by now.
#include <iostream>
#include <string>
#include <cstdlib> // guess I need it for rand stuff
usingnamespace std;
string words[]={"cat", "dog", "mom", "dad", "grandma", "grandpa", "everything", "elephant"} ;
string pickedword;
string guessword;
void display()
{
cout<<"______" <<endl ;
cout<<"| |" <<endl ;
cout<<"|" <<endl ;
cout<<"|" <<endl ;
cout<<"|" <<endl ;
cout<<"|" <<endl ;
cout<<"|" <<endl ;
cout<<"***" <<endl ;
cout<< endl;
cout<< guessword << endl;
cout<< endl;
cout<< "1- guess a letter" << endl;
cout<< "2- guess a word" << endl;
cout<< "3- give up and quit" << endl;
}
void SetupWord()
{
srand((unsigned)time(0)) ;
int pickword=rand()%8 ;
pickedword = words[pickword]
guessword.assign("*", pickedword.length());
}
int main()
{
int guessnumber = 0;
int userchoice = 0;
// set up the first word
SetupWord();
while(userchoice < 3)
{
display();
cin >> userchoice;
switch(userchoice)
{
case 1:
//pick a letter
// basic algorithm...
// use a cin to get a char;
// loop against the picked word until I find all occurrences
// at each found letter use guessword[] with the found position and replace with char.
// increment the guess count.
// if guessword == pickedword we are successful at completing the word in x guesses.
// else we let another round of the loop go by
break;
case 2:
// guess a word
// cin to get the word guess
// if usrguessword == pickedword we win in x guesses and we set up for another run
// else we increment guess count, sorry try again let the loop go around again
break;
}// switch of userchoice
} // while user choice
return 0
} // end of main
this is the basics of what you are trying to do. I don't know if you will understand it or not.
And I need a little help with the switch case. What should go there? Its 6:56 AM here so give me a break . Thanks. I understand what im trying to do, but i can't seem to put it into c++ format.