#include <iostream>
#include <string>
usingnamespace std;
int main()
{
cout << "*******************************************************************************" << endl;
cout << "*\t" << "\t" << "\t" << "\t" << "\t" << "\t" << "\t" << "\t" << "\t *" << endl;
cout << "*\t" << "\t" << "\t" << "\t" << " HANGMAN" << " *" << endl;
cout << "*\t" << "\t" << "\t" << "\t" << "\t" << "\t" << "\t" << "\t" << "\t *" << endl;
cout << "*******************************************************************************" << endl;
cout << "NOTE" << endl;
cout << "The alphabets yout type have to be lowercase" << endl << endl;
cout << "Enter the word for other player to guess" << endl;
string word;
rewrite:
getline(cin, word);
cout << endl;
for (int i = 0;i < word.length();i++)
{
if (word.at(i) >= 65 && word.at(i) <= 90)
{
cout << "Your input contained an upper-case letter" << endl;
cout << "Please type again without an upper-case letter" << endl;
goto rewrite;
}
}
string copy = word;
string Underscore;
for(int i=0; i!=word.length(); i++)
{
if(word.at(i) == ' ')
{
Underscore += " ";
}
else
{
Underscore += "_";
}
}
for(int i=0; i!=50; ++i)
{
cout << endl;
}
string guess;
int wrong=0;
while(1)
{
if(wrong == 6)
{
cout << "You Lose! The word was: " << word << endl;
break;
}
cout << Underscore << endl;
cout << "There are " << word.length() << " letters with spaces" << endl;
cout << "You have " << 6 - wrong << " more tries left" << endl;
if(Underscore == word)
{
cout << "You win!" << endl;
break;
}
cout << "Guess a letter or a word" << endl;
getline(cin, guess);
if(guess.length() > 1)
{
if(guess == word)
{
cout << "WOW!!! You got it on the first try. You win!" << endl << endl;
break;
}
else
{
cout << "Wrong word " << endl;
wrong++;
}
}
elseif(copy.find(guess) != -1)
{
while(copy.find(guess) != -1)
{
Underscore.replace(copy.find(guess), 1, guess);
copy.replace(copy.find(guess), 1, "_");
}
}
else
{
cout << "That's wrong" << endl;
wrong++;
}
cout << endl;
}
system("pause");
return 0;
}
My friend wrote the code from line 91 to line 98. Can someone explain me this part?
I cant understand whats happening. Whats the meaning of != -1 in line 91 and 93.
How do you write std::string::npos without the colon? I dont know the meaning of :: yet. We haven't studied :: yet and we're not supposed to use this syntax.
C++ does allow declaring a hierarchy of names. This allows organizing names like objects in a directory. There exists a top level name called "std". Names like "string", "cin" and "cout" are subordinated to "std". Those doublecolon ("::") symbol acts as separator between two naming components, f.e. "std::string". "npos" is a constant declared as subordinated symbol to "std::string" so "std::string::npos" is its full name. "std::string::npos" may usually (but there may exists implementations defining it in some other way) be defined as '-1'.
The statement in line 3 of your code does instruct the compiler to lookup for unknown names below the top level namespace "std".
In general it is good practice to not use this usingnamespace std; statement to avoid unwanted insertion of names. Instead use fully qualified names like "std::can", ...