5 Replies - 1701 Views - Last Post: 06 October 2013 - 08:45 AM Rate Topic: -----

#1 kyaaaaa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 06-October 13

C++ Guess a word game / Randomize question

Posted 06 October 2013 - 08:23 AM

im a beginner in C++ and i need my questions in the program to be randomized and i dont know what im going to do! could someone good at it help me please! my project is on tuesday =_= ... and im out of time! my teacher gave us only 5days to complete it >_</> ... and i dunno what to do~

#include<iostream>
#include<string>

using namespace std;
main()
{
cout<<"***************GUESS MY WORD GAME*************** \n";

char ans;
cout<<endl;
do
{
cout<< "PRESS Y to start! ^_^/>/>\n";

cout<< "\n\n Guess my SECRET word or face my wrath!!]\nINSTRUCTION: You can PRESS H for the given hints later when the game starts\n";

cout<<" THE LOOP WILL NOT STOP UNTIL YOU PRESS Y ........XD \n ";
cout<<"ENTER or DIE! :> ";
cin >> ans;

}
while((ans !='Y')&&(ans !='y')); //prompt the user to PRESS Y button to start! if the user PRESS any key except the Y button... the program will appear again and again until the Y button is PRESSED

system("cls");

{
string SECRET_WORD = "hair"; // this is the word that the user is challanged to guess...

string SECRET_GUESS;// the users guess 


cout << "its in you everywhere!" << endl;
char H;
cout<<endl;
cout<<"Press H for Hint(X to cancel it)"<<endl;
cin>>H;
cout<<endl;
if (H == 'H' || H == 'h')//if the user enter H button the hint will appear!
{
cout<<"You touch it everytime you scratch your head"<<endl;
cout<<"Enter you Magic Guess ";// prompts the user to enter his/her SECRET_GUESS
cout<<endl;
cin >> SECRET_GUESS;
cout<<endl; 
if(SECRET_WORD == SECRET_GUESS)// if the secret word is equal to secret guess it will print out Correct the secret word was hair
cout << "Correct the secret word was " << SECRET_WORD << endl;// right answer
else
cout << "wrong answer! please try again!" ;//wrong answer

cout<<endl;
system("pause");}
if ((H == 'x' || H == 'X'))// if the user PRESS X it will automatically cancel the hint
{
cout<<endl;
cout<<"Enter you Magic Guess ";// prompt the user to enter his/her secret guess
cin >> SECRET_GUESS;
if(SECRET_WORD == SECRET_GUESS)// likewise
cout << "Correct the secret word was " << SECRET_WORD << endl;
else// likewise
cout << "wrong answer! please try again!" << endl;
}

system("pause");
{cout<<"invalid command! please read the instruction once more!";

}
} 
system("cls");
{
string SECRET_WORD = "medicine";
string SECRET_GUESS;// the users guess 


cout << "You need it everytime you got sick !" << endl;
char H;
cout<<endl;
cout<<"Press H for Hint(X to cancel it)"<<endl;
cin>>H;
cout<<endl;
if (H == 'H' || H == 'h')//if the user enter H button the hint will appear!
{
cout<<" it makes you better! "<<endl;
cout<<"Enter you Magic Guess ";// prompts the user to enter his/her SECRET_GUESS
cout<<endl;
cin >> SECRET_GUESS;
cout<<endl; 
if(SECRET_WORD == SECRET_GUESS)
cout << "Correct the secret word was " << SECRET_WORD << endl;// right answer
else
cout << "wrong answer! please try again!" ;//wrong answer

cout<<endl;
system("pause");}
if ((H == 'x' || H == 'X'))// if the user PRESS X it will automatically cancel the hint
{
cout<<"Enter you Magic Guess ";// prompt the user to enter his/her secret guess
cin >> SECRET_GUESS;
if(SECRET_WORD == SECRET_GUESS)// likewise
cout << "Correct the secret word was " << SECRET_WORD << endl;
else// likewise
cout << "wrong answer! please try again!" << endl;
system("pause");}
} 
system("cls"); 
{
string SECRET_WORD = "hair"; // this is the word that the user is challanged to guess...

string SECRET_GUESS;// the users guess 
cout << "its in you everywhere!" << endl;
char H;
cout<<endl;
cout<<"Press H for Hint(X to cancel it)"<<endl;
cin>>H;
cout<<endl;
if (H == 'H' || H == 'h')//if the user enter H button the hint will appear!
{
cout<<"You touch it everytime you scratch your head"<<endl;
cout<<"Enter you Magic Guess ";// prompts the user to enter his/her SECRET_GUESS
cout<<endl;
cin >> SECRET_GUESS;
cout<<endl; 
if(SECRET_WORD == SECRET_GUESS)// if the secret word is equal to secret guess it will print out Correct the secret word was hair
cout << "Correct the secret word was " << SECRET_WORD << endl;// right answer
else
cout << "wrong answer! please try again!" ;//wrong answer

cout<<endl;
system("pause");}
if ((H == 'x' || H == 'X'))// if the user PRESS X it will automatically cancel the hint
{
cout<<endl;
cout<<"Enter you Magic Guess ";// prompt the user to enter his/her secret guess
cin >> SECRET_GUESS;
if(SECRET_WORD == SECRET_GUESS)// likewise
cout << "Correct the secret word was " << SECRET_WORD << endl;
else// likewise
cout << "wrong answer! please try again!" << endl;
system("pause");}

}
} 
system("cls");
{
string SECRET_WORD = "phone"; // this is the word that the user is challanged to guess...

string SECRET_GUESS;// the users guess 


cout << "You use it everytime you want to comnunicate with me!" << endl;
char H;
cout<<endl;
cout<<"Press H for Hint(X to cancel it)"<<endl;
cin>>H;
cout<<endl;
if (H == 'H' || H == 'h')//if the user enter H button the hint will appear!
{
cout<<" text ! "<<endl;
cout<<"Enter you Magic Guess ";// prompts the user to enter his/her SECRET_GUESS
cout<<endl;
cin >> SECRET_GUESS;
cout<<endl; 
if(SECRET_WORD == SECRET_GUESS)// if the secret word is equal to secret guess it will print out Correct the secret word was hair
cout << "Correct the secret word was " << SECRET_WORD << endl;// right answer
else
cout << "wrong answer! please try again!" ;//wrong answer

cout<<endl;
system("pause");}
if ((H == 'x' || H == 'X'))// if the user PRESS X it will automatically cancel the hint
{
cout<<"Enter you Magic Guess ";// prompt the user to enter his/her secret guess
cin >> SECRET_GUESS;
if(SECRET_WORD == SECRET_GUESS)// likewise
cout << "Correct the secret word was " << SECRET_WORD << endl;
else// likewise
cout << "wrong answer! please try again!" << endl;
system("pause");}
} 
system("cls"); 
{
string SECRET_WORD = "water"; // this is the word that the user is challanged to guess...

string SECRET_GUESS;// the users guess 
cout << "You can`t live without it!" << endl;
char H;
cout<<endl;
cout<<"Press H for Hint(X to cancel it)"<<endl;
cin>>H;
cout<<endl;
if (H == 'H' || H == 'h')//if the user enter H button the hint will appear!
{
cout<<" you drink it! "<<endl;
cout<<"Enter you Magic Guess ";// prompts the user to enter his/her SECRET_GUESS
cout<<endl;
cin >> SECRET_GUESS;
cout<<endl; 
if(SECRET_WORD == SECRET_GUESS)// if the secret word is equal to secret guess it will print out Correct the secret word was hair
cout << "Correct the secret word was " << SECRET_WORD << endl;// right answer
else
cout << "wrong answer! please try again!" ;//wrong answer

cout<<endl;
system("pause");}
if ((H == 'x' || H == 'X'))// if the user PRESS X it will automatically cancel the hint
{
cout<<"Enter you Magic Guess ";// prompt the user to enter his/her secret guess
cin >> SECRET_GUESS;
if(SECRET_WORD == SECRET_GUESS)// likewise
cout << "Correct the secret word was " << SECRET_WORD << endl;
else// likewise
cout << "wrong answer! please try again!" << endl;
system("pause");}
} 
system("cls"); 


cout<<"**********GAME OVER**********";
cout<<endl;
cout<<endl;

system("pause");
return EXIT_SUCCESS;
}


This post has been edited by Skydiver: 06 October 2013 - 08:27 AM
Reason for edit:: Put code in code tags. Learn to follow instructions.


Is This A Good Question/Topic? 0
  • +

Replies To: C++ Guess a word game / Randomize question

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3569
  • View blog
  • Posts: 11,089
  • Joined: 05-May 12

Re: C++ Guess a word game / Randomize question

Posted 06 October 2013 - 08:30 AM

Yikes, you have all that duplicated code. Didn't you learn about functions?

Also, that code formatting is terrible. You need to pick an indent style and apply it consistently.
Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is offline

  • member icon


Reputation: 4066
  • View blog
  • Posts: 12,548
  • Joined: 25-December 09

Re: C++ Guess a word game / Randomize question

Posted 06 October 2013 - 08:37 AM

My first suggestion is that you find an indentation style you like and use it consistently, then maybe you might be able to follow your logic.

Next in a C++ program main() must be defined to return an int, and you should return an int from this function.


int main()
{

   return(0);
}


Next when posting code please use code tags.

:code:

Jim
Was This Post Helpful? 0
  • +
  • -

#4 kyaaaaa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 06-October 13

Re: C++ Guess a word game / Randomize question

Posted 06 October 2013 - 08:38 AM

sorry about that sir.. im just a newbie at programming.. please help me if you know the technique on how to randomize it..
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3569
  • View blog
  • Posts: 11,089
  • Joined: 05-May 12

Re: C++ Guess a word game / Randomize question

Posted 06 October 2013 - 08:45 AM

Anyway, with regards to your particular problem about randomly picking a word, look at the rand() function. Using the results of the function, use that as an index into an array of structures that contains the word and the hint. Then pass that structure to your function for asking for the user's guess.

To get you started consider this pseudo code:
struct WordHint
{
    std::string word;
    std::string hint;
};

GuessWord(WordHint wordHint)
{
    while(true)
    {
        cout << "I'm thinking of a word. Read my mind: ";
        cin >> guess;
        if (guess == wordHint.word)
        {
            cout << "You are psychic! Have you a future with the Psychic Friends Network."
            break;
        }

        if (guess == "H")
            cout << wordHint.hint;

        if (guess == "uncle")
        {
            cout << "You gave up too soon."
            break;
        }
    }
    cout << "The word is " << wordHint.word;
}

main()
{
    WordHint wordsAndHints[] =
        {
            { "obamacare", "Thanks, Obama." },
            { "hair",      "One of the best musicals ever." },
        };

    do
    {
        index = rand() % CountOf(wordsAndHints);
        GuessWord(wordsAndHints[index]);
        playAgain = GetYesNoInput();
    } while (playAgain);
}


Was This Post Helpful? 1
  • +
  • -

#6 jimblumberg  Icon User is offline

  • member icon


Reputation: 4066
  • View blog
  • Posts: 12,548
  • Joined: 25-December 09

Re: C++ Guess a word game / Randomize question

Posted 06 October 2013 - 08:45 AM

You need fix the issues pointed out.

Quote

Next in a C++ program main() must be defined to return an int, and you should return an int from this function.


Then you need to properly format your code, the code you posted is pretty much unreadable.

Then you need to answer the questions asked:

Quote

Yikes, you have all that duplicated code. Didn't you learn about functions?


Also does that code even compile, probably not? If not post the complete error messages exactly as they appear in your development environment.

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1