Thread: hangman game, copied staight from book.. 31 errors?!?!?!?!

  1. #1
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373

    Question hangman game, copied staight from book.. 31 errors?!?!?!?!

    WHY WON'T IT WORK?!?!?!

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <string>
    #include <ctime>
    #include <type.h>
    using namespace std;
    const int NUM = 2;
    const string wordlist[NUM] = {"apiary", "cereal};
    int main()
    {
    srand(time(0));
    char play;
    char gen;
    cout << "Are you a sir or mam?\n";
    cin gen;
    cout << "Okay, ";
    cout << gen;
    cout << "Play my wordgame? <y/n> ";
    cin play;
    play = tolower(play);
    while (play == 'y')
    {
    string target = wordlist[rand() % NUM];
    int length = target.length();
    string attempt(length, '-');
    string badchars;
    int guesse = 6;
    cout << "Guess my secret word. It has " << length
         << " letters, and you guess\n";
         << "one letter at a time. You get " << guesses
         << " wrong guesses.\n";
    cout << "Yor word: " << attempt << endl;
    while (guesses > 0 && attempt != target)
    {
    char letter
    cout << "Guess a letter: ";
    cin letter;
    if (badchars.find(letter) != string::npos
        || attempt.find(letter) != string::npos)
        {
        cout << "You already guessed that!\n";
             continue;
        }
        int loc = target.find(letter);
        if (loc == string::npos)
        {
        cout << "Oh! Bad luck, and guess!\n";
        --guesses;
        badchars += letter;
        }
        else
        {
        cout << "Good choice, ";
        cout << gen;
        attempt[loc]=letter;
        loc = target.find(letter, loc + 1);
        while (loc != string::npos)
        {
         attempt[loc=letter;
         loc = target.find(letter, loc + 1);
        }
    }
    cout << "Your word: " << attempt << endl;
    if (attempt != target)
    {
    if (badchars.length() > 0)
       cout << "Bad chaoices: " << badchars << endl;
     cout << guesses << " bad guesses left.\n";
    }
    }
    if (guesses > 0)
        cout << "Correct!\n";
    else
        cout << "Nope! The word is: " << target << ".\n";
    
    cout << "Will you play another?: ";
    cin >> play;
    play = tolower(play);
    }
    
    
    
    cout << "Bye!\n";
    
    system ("PAUSE");
    return 0;
    }
    AAARGH!
    This war, like the next war, is a war to end war.

  2. #2
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373
    it's supposed to look like this:

    Are you a sir or mam?
    sir
    Okay, sir.
    Play my word game?y
    <instructions>
    Your yord: -----
    Guess a letter:
    l
    Oh, bad choice!
    etc
    This war, like the next war, is a war to end war.

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    82
    Check for typos:

    const string wordlist[NUM] = {"apiary", "cereal};
    should be
    const string wordlist[NUM] = {"apiary", "cereal"};

    int guesse = 6;
    should be
    int guesses = 6;

    char letter
    should be
    char letter;

    cin letter;
    should be
    cin >> letter;

    These are just the ones I see the first way through. Compare with the program in your book, checking every character.
    Claus Hetzer
    Compiler: Borland 5.5 (on Windows)
    Solaris CC (on Unix)
    Known Languages: C++, MATLAB, Perl, Java

  4. #4
    Registered User Kirdra's Avatar
    Join Date
    Aug 2002
    Posts
    105
    ::Edit::

    Nevermind, Prelude is correct.
    Last edited by Kirdra; 10-24-2002 at 11:54 PM.

  5. #5
    Bios Raider biosninja's Avatar
    Join Date
    Jul 2002
    Location
    South Africa
    Posts
    765
    This code works (modified yours) :

    Code:
    #include <iostream>
    #include <stdlib>
    #include <string>
    #include <time>
    using namespace std;
    const int NUM = 2;
    const string wordlist[NUM] = {"apiary", "cereal"};
    
    int main()
    {
       srand(time(0));
       char play;
       char gen;
       cout << "Are you a sir or mam?\n";
       cin >> gen;
       cout << "Okay, ";
       cout << gen;
       cout << "Play my wordgame? <y/n> ";
       cin >> play;
      play = tolower(play);
      while (play == 'y')
      {
         string target = wordlist[rand() % NUM];
         int length = target.length();
         string attempt(length, '-');
         string badchars;
         int guesses = 6;
         cout << "Guess my secret word. It has " << length
                 << " letters, and you guess\n"
                 << "one letter at a time. You get " << guesses
                 << " wrong guesses." << endl;
         cout << "Yor word: " << attempt << endl;
         while (guesses > 0 && attempt != target)
         {
              char letter;
              cout << "Guess a letter: ";
              cin >> letter;
    
              if (badchars.find(letter) != string::npos
             || attempt.find(letter) != string::npos)
             {
             cout << "You already guessed that!\n";
             continue;
             }
         int loc = target.find(letter);
         if (loc == string::npos)
         {
              cout << "Oh! Bad luck, and guess!\n";
              --guesses;
              badchars += letter;
        }
        else
        {
             cout << "Good choice, ";
             cout << gen;
             attempt[loc]=letter;
             loc = target.find(letter, loc + 1);
             while (loc != string::npos)
        {
         attempt=loc=letter;
         loc = target.find(letter, loc + 1);
        }
    }
    cout << "Your word: " << attempt << endl;
    if (attempt != target)
    {
         if (badchars.length() > 0)
         cout << "Bad chaoices: " << badchars << endl;
         cout << guesses << " bad guesses left.\n";
    }
    }
    if (guesses > 0)
        cout << "Correct!\n";
    else
        cout << "Nope! The word is: " << target << ".\n";
    
    cout << "Will you play another?: ";
    cin >> play;
    play = tolower(play);
    }
    
    cout << "Bye!\n";
    
    system ("PAUSE");
    return 0;
    }
    Last edited by biosninja; 10-24-2002 at 11:37 PM.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Replace: system ("PAUSE");

    With: system("PAUSE");
    >system ("PAUSE");
    >system("PAUSE");
    It's late and I'm tired, but it looks like the only difference is the space between the opening paren and the function name. Both forms are perfectly legal, otherwise my style would be totally obscene:

    system ( "PAUSE" );

    C++ is a free form language. This means that you can put whitespace just about anywhere as long as it doesn't change the semantics of the statement. Two examples are putting a space between an identifier:

    int my num = 10; // error!

    And breaking a string literal with a newline:

    std::cout<<"This is
    a test"<<std::endl; // error!

    There are a few other situations, but for the most part you can format code however you want. "However you want" should be a synonym for "As readable as possible" by the way.

    -Prelude
    My best code is written with the delete key.

  7. #7
    Registered User Kirdra's Avatar
    Join Date
    Aug 2002
    Posts
    105
    Heh, yup your right. I don't usually use system("PAUSE") to pause though

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I don't usually use system("PAUSE") to pause though
    It's just like any other function, but I don't blame you for not using it, system is way slow and something like this is just as effective:
    Code:
    std::cout<<"Press return to continue"<<std::flush;
    std::cin.get();
    -Prelude
    My best code is written with the delete key.

  9. #9
    Bios Raider biosninja's Avatar
    Join Date
    Jul 2002
    Location
    South Africa
    Posts
    765
    std::cout<<"Press return to continue"<<std::flush;
    std::cin.get();
    Or just plain old : "getch();"

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Or just plain old : "getch();"
    Only if you use DOS or Windows, getch is nonstandard and often not implemented on POSIX systems. It sure is a waste to kill your portability for a simple pause at the end of the program.

    -Prelude
    My best code is written with the delete key.

  11. #11
    Bios Raider biosninja's Avatar
    Join Date
    Jul 2002
    Location
    South Africa
    Posts
    765
    True...True.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Game Programming Book
    By Android in forum Game Programming
    Replies: 15
    Last Post: 08-10-2003, 06:56 AM
  2. Books on C and C++
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-28-2002, 04:18 PM
  3. Address book and connect 4 game
    By sundeeptuteja in forum C++ Programming
    Replies: 1
    Last Post: 07-21-2002, 05:02 AM
  4. I'm looking for a good book on 3D game programming
    By Music_Man in forum Game Programming
    Replies: 2
    Last Post: 09-09-2001, 09:29 PM
  5. Looking for a good book on 3D game programming
    By Music_Man in forum C++ Programming
    Replies: 2
    Last Post: 09-09-2001, 09:28 PM