So in this small project for my school I needed to build a word guessing game everything works fine except the string university which always gives output incorrect, I tried changing it to universit and it works but university doesn't work. I cant figure out what the problem is.
Also are there any other alternatives to strcmp()?
#include<iostream>
#include<string.h>
#include<stdlib.h>
#include<time.h>
using namespace std;
int main()
{ //at first we are basically going to give an intro to the gameseco
cout << " HANGMAN : GUESS THE WORD AND NOT DIE!!\n\n\n";
cout << " Welcome to the game player!!\n\n";
cout << " Rules: Guess the word by typing a letter based on given hints.\n";
cout << " Multiple incorrect guesses will HANG YOU.\n\n";
//here we add different words in the game for the guessing system using a 2 dimensional
array
char word[20][20]={"higher", "secondary", "first", "year", "cotton", "university" , "computer" , "science", "project", "hangman",};
char newword[20], guess[10];
bool again = 1;
while(again == 1) {
again = 0;
srand(time(0));
int x=0, wrong=0, z = (rand() % 10) + 0;
strcpy(newword, word[z]); //so we used strcpy command to copy an element of the 2D array of randomly generated index to a new string
cout << "Enter your guess. \nHINT: Word Length(indicated by underscores) = "; //hint no. one the gives us the exact lenth of word
for(x=0; newword[x]!='\0'; x++){
cout<< "-";
}
cout << "\nWord starts with: '" << newword[0] << "'"; //hint no. two which gives the player an idea what the word might be
cout << "\n\n";
for(wrong=1; wrong<=6; wrong++) //the loop here checks whether the input given by the user is correct or not
{
cin >> guess; // the input is taken here
if(strcmp(guess , newword) == 0) //the input is compared with the word to be guessed
{ //using the strcmp() function from the string.h library
cout << "Correct!\n"; //if the guess is correct the program will print correct and correct and end
break; //correct guess will terminate the loop
}else if(wrong==1)
{
cout << "Opps! Wrong guess!\n\n"; //if player inputs wrong word a poll will appear for hanging the person
cout << "I=-=\n";
cout << "|\n";
cout << "|\n";
cout << "|\n";
cout << "|\n";
cout << "I\n";
}else if(wrong==2)
{
cout << "Opps! Wrong guess again!\n\n"; //each wrong word will result in the appearance of of a hanging person
cout << "I=-=\n";
cout << "| |\n";
cout << "|\n";
cout << "|\n";
cout << "|\n";
cout << "I\n";
}else if(wrong==3)
{
cout << "Opps! Wrong guess again!\n\n";
cout << "I=-=\n";
cout << "| |\n";
cout << "| O\n";
cout << "|\n";
cout << "|\n";
cout << "I\n";
}else if(wrong==4)
{
cout << "Opps! Wrong guess again!\n\n";
cout << "I=-=\n";
cout << "| |\n";
cout << "| O\n";
cout << "I--|--\n";
cout << "|\n";
cout << "I\n";
}else if(wrong==5)
{
cout << "Opps! Wrong guess again!\n\n";
cout << "I=-=\n";
cout << "| |\n";
cout << "| O\n";
cout << "I--|--\n";
cout << "| o\n";
cout << "I\n";
}else if(wrong==6)
{
cout << "Opps! Wrong guess again!\nLast Chance!!\n\n"; //unfortunately the player couldn't guess the word
cout << "I=-=\n";
cout << "| |\n";
cout << "| O\n"; //the hanging person compeletely appears and the program ends
cout << "|--|--\n";
cout << "| o\n";
cout << "I | |\n";
cout << "YOU ARE DEAD HAHAHA!!!";
}
}
cout << "Do you want to play again?\nPress 1 for yes, 0 for no."; //here it is asked if we want to play again
cin >> again;
}
return 0;
}
Aucun commentaire:
Enregistrer un commentaire