Close, but no cigarette

This is a discussion on Close, but no cigarette within the C++ Programming forums, part of the General Programming Boards category; I have the following code for a game of hangman that imports the words it uses from a flat text ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    1

    Question Close, but no cigarette

    I have the following code for a game of hangman that imports the words it uses from a flat text file called "words.dat". It works well. How though can I convert the words when they are brought in from the file to into pig latin to be used by the app? Any help is greatly appreciated.

    #include <iostream.h>
    #include <fstream.h>
    #include <string.h>
    #include <time.h>
    #include <stdlib.h>

    #define MAX_WORD_SIZE 15
    #define MAX_WORDS 255

    void LoadFile();
    void RunGame();
    void DrawGallows(int State);

    typedef char String[MAX_WORD_SIZE];
    String Words[MAX_WORDS - 1];
    int Count;

    void main()
    {
    char Continue = 'Y';
    cout<<"Hangman"<<endl;

    LoadFile();

    while(Continue == 'Y')
    {
    RunGame();
    cout<<endl<<"Do you want to play again (Yes or No)? ";
    cin>>Continue;
    Continue = toupper(Continue);
    }

    cout<<endl<<"Thanks for playing."<<endl;
    }

    void LoadFile()
    {
    char C;
    ifstream Datfile;

    Count=0;

    Datfile.open("words.dat");

    while((C=Datfile.peek()) != EOF)
    {

    Datfile>>Words[Count++];

    if(Count > MAX_WORDS - 1)
    {
    cout<<endl<<"Too many words in the file, stopping with "
    <<MAX_WORDS<<" Words."<<endl;
    Count = MAX_WORDS;
    break;
    }

    }

    Count--;

    Datfile.close();

    }

    void RunGame()
    {
    int Word;
    int Size;
    int State=1;
    int Subscript=0;
    char Guess[MAX_WORD_SIZE];
    char Copy[MAX_WORD_SIZE];
    char Letter;
    int Correct=0;

    srand((unsigned)time( NULL ));
    Word = rand() % Count;

    strcpy(Copy,Words[Word]);

    Size = strlen(Words[Word]);

    for(; Subscript < Size; Subscript++)
    {
    Guess[Subscript] = '-';
    }

    Guess[Subscript] = '\0';

    while(State!=7)
    {
    DrawGallows(State);
    cout<<Guess<<endl;

    cout<<"Guess a letter :";
    cin>>Letter;

    Letter = tolower(Letter);

    for(Subscript = 0; Subscript < Size; Subscript++)
    {

    if(Copy[Subscript] == Letter)
    {
    Guess[Subscript] = Letter;
    Correct = 1;
    cout<<endl<<"Good Guess!";

    if(strcmp(Words[Word],Guess) == 0)
    {
    cout<<endl<<"You won!";
    return;
    }
    }
    }


    if(Correct == 0)
    {
    cout<<endl<<"Sorry, bad guess!";
    State++;
    }

    Correct = 0;

    }

    DrawGallows(State);
    cout<<"The word was : "<<Words[Word]<<endl<<endl;

    }


    void DrawGallows(int State)
    {
    if(State==7)
    {
    cout<<endl<<endl
    <<" +----+ "<<endl
    <<" | | "<<endl
    <<" O | "<<endl
    <<" /|\\ | "<<endl
    <<" | | "<<endl
    <<" / \\ | "<<endl
    <<" | "<<endl
    <<" ============"<<endl<<endl;
    }
    else if(State==6)
    {
    cout<<endl<<endl
    <<" +----+ "<<endl
    <<" | | "<<endl
    <<" O | "<<endl
    <<" /|\\ | "<<endl
    <<" | | "<<endl
    <<" \\ | "<<endl
    <<" | "<<endl
    <<" ============"<<endl<<endl;
    }
    else if(State==5)
    {
    cout<<endl<<endl
    <<" +----+ "<<endl
    <<" | | "<<endl
    <<" O | "<<endl
    <<" /|\\ | "<<endl
    <<" | | "<<endl
    <<" | "<<endl
    <<" | "<<endl
    <<" ============="<<endl<<endl;
    }
    else if(State==4)
    {
    cout<<endl<<endl
    <<" +----+ "<<endl
    <<" | | "<<endl
    <<" O | "<<endl
    <<" /|\\ | "<<endl
    <<" | "<<endl
    <<" | "<<endl
    <<" ============="<<endl<<endl;
    }
    else if(State==3)
    {
    cout<<endl<<endl
    <<" +----+ "<<endl
    <<" | | "<<endl
    <<" O | "<<endl
    <<" /| | "<<endl
    <<" | "<<endl
    <<" | "<<endl
    <<" ============="<<endl<<endl;
    }
    else if(State==2)
    {
    cout<<endl<<endl
    <<" +----+ "<<endl
    <<" | | "<<endl
    <<" O | "<<endl
    <<" | | "<<endl
    <<" | "<<endl
    <<" | "<<endl
    <<" ============="<<endl<<endl;
    }
    else if(State==1)
    {
    cout<<endl<<endl
    <<" +----+ "<<endl
    <<" | | "<<endl
    <<" | "<<endl
    <<" | "<<endl
    <<" | "<<endl
    <<" | "<<endl
    <<" ============="<<endl<<endl;
    }

    }

  2. #2
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    So, that the experts around here can help you use code tags for your own sake and this shall yield better results for you.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    I'm sure that if you take a look here you'll find that converting a phrase to pig latin was a recent competition where the code was posted.

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

  4. #4
    Unregistered
    Guest
    Close, but no cigar

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Close a File in Managed
    By Coding in forum C# Programming
    Replies: 4
    Last Post: 05-20-2008, 08:06 AM
  2. Close an HTTPListenerResponse.OutputStreram
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 04-23-2008, 11:00 PM
  3. open() and close()
    By SoFarAway in forum C Programming
    Replies: 3
    Last Post: 04-08-2005, 01:16 PM
  4. XWindows- Close window event?
    By Exile in forum Linux Programming
    Replies: 8
    Last Post: 01-09-2005, 09:39 PM
  5. Ghost in the CD Drive
    By Natase in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 10-12-2001, 05:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21