6.3w+
社区成员
#include <iostream>
#include <string>
#include <cctype>
#include <cstdlib>
using namespace std;
int main()
{
int i;
int chance=10;
int rigth=0;
string word="hello";
string temp;
char letter;
temp=word;
for(i=0;i <word.length();i++)
{
temp.replace(i,1,1,'*');
}
do
{
cout <<"Please enter a letter:" <<endl <<endl <<chance <<"chance left" <<endl;
cin>>letter;
chance--;
for(i=0;i <temp.length();i++)
{
while(!isalpha(letter)&&chance>0)
{
fflush(stdin);
cout <<"Not a letter,please enter a letter!" <<endl<<endl<<chance <<"chance left" <<endl;
cin>>letter;
chance--;
if(letter==temp.at(0)&&chance>0)
{
cout <<endl;
cout <<"This letter have been chosen.Please enter an unchosen letter" <<endl <<endl <<endl <<chance <<"chance left" <<endl;
cin>>letter;
chance--;
i=-1;
}
}
}
for(i=0;i <word.length();i++)
{
if(letter==word.at(i)&&chance>0)
{
temp.replace(i,1,1,letter);
rigth++;
if(rigth==word.length())
{
cout <<"You get it!" <<"[" <<word <<"]" <<endl;
break;
}
}
else
{
if(chance>0)
cout <<"Wrong letter!" <<endl;
}
}
}
while(rigth <word.length()&&chance>0);
cout <<"You lose it!" <<"[" <<word <<"]" <<endl;
return 0;
}
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
const int chance = 10;
const string word("hello");
const unsigned loops = word.length();
string temp("*****");
unsigned right = 0;
char letter;
for(int i = chance; i > 0; i--)
{
cout << "Please enter a letter(" << i << " chance left):" << endl;
cin >> letter;
for(unsigned j = 0; j < loops; j++)
{
if((letter == word.at(j)) && letter != temp.at(j))
{
temp[j] = letter;
right++;
break;
}
}
cout << "Result:" << temp << endl;
if(right == loops)
{
cout << "U r right!" << endl;
break;
}
}
if(right != loops)
cout << "U r wrong!" << endl;
return 0;
}
Please enter a letter:
10chance left
34
Not a letter,please enter a letter!
9chance left
54
Not a letter,please enter a letter!
8chance left
34
Not a letter,please enter a letter!
7chance left
65
Not a letter,please enter a letter!
6chance left
..........
#include <iostream>
#include <string>
#include <cctype>
#include <cstdlib>
using namespace std;
int main()
{
int i;
int chance=10;
int rigth=0;
string word="hello";
string temp;
char letter;
temp=word;
for(i=0;i <word.length();i++) {
temp.replace(i,1,1,'*');
}
do{
cout <<"Please enter a letter:" <<endl <<endl <<chance <<"chance left" <<endl;
cin>>letter;
chance--;
for(i=0;i <temp.length();i++){
while(!isalpha(letter)&&chance>0){
fflush(stdin);
cout <<"Not a letter,please enter a letter!" <<endl <<endl <<chance <<"chance left" <<endl;
cin>>letter;
chance--;
if(letter==temp.at(i)&&chance>0){
cout <<endl;
cout <<"This letter have been chosen.Please enter an unchosen letter" <<endl <<endl <<endl <<chance <<"chance left" <<endl;
cin>>letter;
chance--;
i=-1;
}
}
}
for(i=0;i <word.length();i++) {
if(letter==word.at(i)&&chance>0){
temp.replace(i,1,1,letter);
rigth++;
if(rigth==word.length()) {
cout <<"You get it!" <<"[" <<word <<"]" <<endl;
break;
}
}
else{
if(chance>0)
cout <<"Wrong letter!" <<endl;
}
}
}while(rigth <word.length()&&chance>0);
cout <<"You lose it!" <<"[" <<word <<"]" <<endl;
return 0;
}