I have code if this problem but i am not getting right output. Please i need hel
ID: 3817337 • Letter: I
Question
I have code if this problem but i am not getting right output. Please i need help with right coding and right output.
Here is my code:
#include<iostream>
#include<string> /* Change header file string.h to string */
#include<cstdlib>
using namespace std;
int main()
{
string origWord="";
string letter="";
char dashReplaced='N';
char gameOver='N';
int numIncorrect=0;
bool checkLetter[26] = {0}; //Array to check if a letter is entered or not
string displayWord="-----";
//get original word
while(origWord.length()!=5)
{
cout<< "Enter a 5-letter word in uppercase: ";
getline(cin,origWord);
} //end while
//system("CLS"); //clear screen
//start guessing
cout << endl << "Guess this word: "<< displayWord<<endl;
while(gameOver=='N')
{
/* Initialize dashReplaced to N at each iteration */
dashReplaced = 'N';
cout << endl << "Enter an uppercase letter: ";
cin>>letter;
//search for the letter in the original word
for(int x=0;x<5;x+=1)
{
//if the current character matches
//the letter, replace the corresponding dash in the
//displayWord variable and then set the dashReplaced
// variable to 'Y'
if(origWord.substr(x,1)==letter)
{
displayWord.replace(x,1,letter);
dashReplaced='Y';
} //end if
} //end for
//if a dash was replaced, check whether the displayWord
//variable contains another dash
if(dashReplaced=='Y')
{
//if the displayWord variable does not contain any dashes
//the game is over
int index = letter[0] - 'A';
checkLetter[index] = 1;
if(displayWord.find("-",0)==-1)
{
gameOver='Y';
cout<<endl<<"Yes, the word is "<<origWord<<endl;
cout<<"Great guessing!"<<endl;
}
else //otherwise continue guessing
{
cout << "Correct letter guessed !" << endl;
cout << endl << "Guess this word: "<<displayWord<<endl;
dashReplaced='N';
} //end if
}
else //processed when dashReplaced contains 'N'
{
//add 1 to the number of incorrect guesses
int index = letter[0] - 'A';
if(checkLetter[index] == 1)
{
cout << "Letter already entered ! Please enter a different letter." << endl;
continue;
}
checkLetter[index] = 1;
cout << "Wrong guess, " << 9 - numIncorrect << " tries remaining." << endl;
numIncorrect+=1;
//if the number of incorrect guesses is 10, the
//game is over
if(numIncorrect==10)
{
gameOver='Y';
cout<<endl<<"Sorry, the word is "<<origWord<<endl;
break;
} //end if
cout<<endl<<"Guess this word: "<<displayWord<<endl;
} //end if
} //end while
return 0;
} //end of main function
Explanation / Answer
Your code was almost correct, it was not working for correctly guessed letter, I have made chnages so that it works.
#include<iostream>
#include<string> /* Change header file string.h to string */
#include<cstdlib>
using namespace std;
int main()
{
string origWord="";
string letter="";
char dashReplaced='N';
char gameOver='N';
int numIncorrect=0;
bool checkLetter[26] = {0}; //Array to check if a letter is entered or not
string displayWord="-----";
//get original word
while(origWord.length()!=5)
{
cout<< "Enter a 5-letter word in uppercase: ";
getline(cin,origWord);
} //end while
//system("CLS"); //clear screen
//start guessing
cout << endl << "Guess this word: "<< displayWord<<endl;
while(gameOver=='N')
{
/* Initialize dashReplaced to N at each iteration */
dashReplaced = 'N';
cout << endl << "Enter an uppercase letter: ";
cin>>letter;
//add 1 to the number of incorrect guesses
int index = letter[0] - 'A';
if(checkLetter[index] == 1)
{
cout << "Letter already entered ! Please enter a different letter." << endl;
continue;
}
checkLetter[index] = 1;
//search for the letter in the original word
for(int x=0;x<5;x+=1)
{
//if the current character matches
//the letter, replace the corresponding dash in the
//displayWord variable and then set the dashReplaced
// variable to 'Y'
if(origWord.substr(x,1)==letter)
{
displayWord.replace(x,1,letter);
dashReplaced='Y';
} //end if
} //end for
//if a dash was replaced, check whether the displayWord
//variable contains another dash
if(dashReplaced=='Y')
{
//if the displayWord variable does not contain any dashes
//the game is over
int index = letter[0] - 'A';
checkLetter[index] = 1;
if(displayWord.find("-",0)==-1)
{
gameOver='Y';
cout<<endl<<"Yes, the word is "<<origWord<<endl;
cout<<"Great guessing!"<<endl;
}
else //otherwise continue guessing
{
cout << "Correct letter guessed !" << endl;
cout << endl << "Guess this word: "<<displayWord<<endl;
dashReplaced='N';
} //end if
}
else //processed when dashReplaced contains 'N'
{
cout << "Wrong guess, " << 9 - numIncorrect << " tries remaining." << endl;
numIncorrect+=1;
//if the number of incorrect guesses is 10, the
//game is over
if(numIncorrect==10)
{
gameOver='Y';
cout<<endl<<"Sorry, the word is "<<origWord<<endl;
break;
} //end if
cout<<endl<<"Guess this word: "<<displayWord<<endl;
} //end if
} //end while
return 0;
} //end of main function
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.