1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
#include <iostream>
#include <ctime>
#include <string>
#include<conio.h>
using namespace std;
const int maxtries=5;
//PRE-DEFINED FUNCTION(S):
int letterFill (char, string, string&);
//MAIN
int main(){
string name;
char answer, letter;
int wrongguess=0;
string word;
string words[] ={
"singapore",
"china",
"japan",
"vietnam",
"philippines",
"australia",
"indonesia",
"malaysia",
"taiwan",
"switzerland",
};
menu:
srand(time(NULL));
int n=rand()% 10;
word=words[n];
string unknown(word.length(),'*');
system("CLS");
cout << "\n\n\t\t\tWelcome to Guess the Country!";
cout << "\n\n\t\t Each letter is represented by a star.";
cout << "\n\n\t\tYou have to type only one letter in one try.\n\n\n";
cout<<"Press any key to proceed to the game..";
getch();
system("CLS");
cout << "\n\n\t\tYou have " << maxtries << " tries to try and guess the word.\n\n";
cout << "\t\t\t NOTE: USE SMALL LETTERS ONLY! ";
cout << "\n\t\t ======================================";
while (wrongguess < maxtries){
cout << "\n\n\t\t\t\t" << unknown;
cout << "\n\n\t\tGuess a letter: ";
cin >> letter;
if (letterFill(letter, word, unknown)==0){
system("CLS");
cout << endl <<"\t\tWhoops! That letter isn't in there! Please enter another one." << endl;
wrongguess++;
}else{
system("CLS");
cout << endl << "\t\tYou're guess is correct! Please enter another letter." << endl;
}
cout <<"\t\tYou have " << maxtries - wrongguess;
cout << "guesses left." << endl;
if (word==unknown)
{
system("CLS");
cout << "\n\n\t\tCongratulations! You've guessed the correct Country!!! ";
cout << "\t\t\t\t\t Your answer was "<<word<<"\n\n";
answer:
cout << "Do you want to try again? (y/n): ";
cin>>answer;
if (answer=='y' || 'Y'){
goto menu;
}else if (answer == 'n' || 'N'){
cout<<"Thank you! Please press any key to terminate the game.";
getch();
return 0;
}else{
cout<<"Error! Please enter the correct letter! ";
goto answer;
}
}
}
if(wrongguess == maxtries){
system("CLS");
cout << "\nSorry, you have just exceeded the total amount of guess available." << endl;
cout << "The word was : " << word << endl;
cout << "Do you want to try again? (y/n): ";
cin>>answer;
if (answer=='y' || 'Y'){
wrongguess=0;
goto menu;
}else if (answer == 'n' || 'N'){
cout<<"Thank you! Please press any key to terminate the game.";
getch();
return 0;
}
else{
cout<<"Error! Please enter the correct letter! ";
goto answer;
}
}
getch();
return 0;
}//END OF MAIN
//FUNCTION(S):
int letterFill (char guess, string secretword, string &guessword){
int i;
int matches=0;
int len=secretword.length();
for (i = 0; i< len; i++){
if (guess == guessword[i] || guess == guessword[i] - 32)
return 0;
if (guess == secretword[i] || guess == secretword[i] - 32)
{
guessword[i] = guess;
matches++;
}
}
}
|