Answer to Question #6975 in C++ for MUNA

Question #6975
using loop
Write a C++ program that declares a secret word of 5 character and ask the user to find out that word . if the word entered by the user a character is in its correct position then it will appear , otherwise a star will be shown in its location. For example if the secret word is write and the user entered the word think then the program will display **i** . Then if the user enter the word arrive then the program will display *ri** ( the position of the character I was discovered in the pervious step) This process will continue till the user finds the correct word .
1
Expert's answer
2012-03-01T09:24:10-0500
#include<iostream>




using namespace std;




int main () {

char secret[] = "world";

char word[6];

char tmp[] = "*****";

bool flag = true;




while(1){

flag = true;

cout << "What is the secret word? ";

cin >> word;

cout << endl;

for(int i = 0; i < 5; i++){

if(word[i] == secret[i])

tmp[i] = secret[i];

else

flag = false;

}

for(int i = 0; i < 5; i++)

cout << tmp[i];

cout << endl << endl;

if(flag)

break;

}

cout << "You have found the secret word!" << endl;




return 0;

}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS