Welcome to Dream.In.Code
Become a C++ Expert!

Join 150,007 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,392 people online right now. Registration is fast and FREE... Join Now!




Guessing Game Application

2 Pages V  1 2 >  
Reply to this topicStart new topic

Guessing Game Application

KevSmith
5 Dec, 2006 - 06:28 AM
Post #1

New D.I.C Head
*

Joined: 3 Dec, 2006
Posts: 12


My Contributions
This is what i have so far and am now stuck!!

CODE
#include <iostream.h>    // Required for cout, cin function
#include <string.h>        // Required for any string operation functions
#include <time.h>        // Required for time() function
#include <stdlib.h>        // Required for srand() function
#include <stdio.h>
#include <cstdlib>        // for system.("cls"), abs() and rand() functions

// ---------------------------------------------------------
//
int main() {


    struct player {
    int words_correct;
    int number_of_guesses;
    int number_of_lives;
    int player_score;
};
    //
    player myPlayer;
    
    //
     myPlayer.words_correct = 0;
     myPlayer.number_of_guesses = 0;
     myPlayer.number_of_lives = 5;
     myPlayer.player_score = 0;

    //
    bool keep_playing = true;

    //
    //
    do {

        //
        //
        //
        system("cls");
        cout << "================================================================\n\n";
        cout << "        WORD SCRAMBLE                                           \n\n";
        cout << "================================================================\n";
        cout << "\n";
        cout << "Welcome to the Word Scramble game!\n";
        cout << "The goal of this game is to guess the word letter by letter, you\n";
        cout << "will be given points for each word you get correct. You have    \n";
        cout << "five lives, you will lose a ife for each letter you guess       \n";
        cout << "incorrectly.\n";
        cout << "\n";
        cout << "================================================================\n";
        cout << "\n";
        cout << "Lives: " << myPlayer.number_of_lives << "\n";
        cout << "Score: " << myPlayer.player_score << "\n";                                        
        cout << "\n";
        cout << "================================================================\n";

        //
        {

#define NUMBEROFWORDS 20
#define MAXWORDLENGTH 20
char myWordList[NUMBEROFWORDS][MAXWORDLENGTH] = {    "one", "two", "three",
                                                    "four", "five", "six",
                                                    "seven", "eight", "nine",
                                                    "ten", "eleven", "twelve",
                                                    "thirteen", "fourteen", "fifteen",
                                                    "sixteen", "seventeen", "eighteen",
                                                    "nineteen", "twenty", };

{

    char myChosenWord[MAXWORDLENGTH];    
    char guess;
    cin >> guess;
    
    
    srand(time(NULL));                    /*    initialize random number generator - without
                                            this the random number generated will be the same
                                            everytime the program runs */


    do {

         {

            int randomnumber = rand()%NUMBEROFWORDS;            // generate a random number (0 to NUMBEROFWORDS-1)
            strcpy(myChosenWord, myWordList[randomnumber]);        /*    copy the content of myWordList[randomnumber]
                                                                    to myChosenWord */

        }


    } while (keep_playing = true);

    char currentword[NUMBEROFWORDS] = {myChosenWord};
    bool flag[MAXWORDLENGTH-1];
    for (int i=0;i<MAXWORDLENGTH-1;i++)
        flag[i] = false;
        char scrambledword[MAXWORDLENGTH];
        int length = MAXWORDLENGTH;
        for (i=0;i<MAXWORDLENGTH-1;i++) {
            bool gotrandomchar = false;
            do {
                int rndpos = rand()%length;
                if (flag[rndpos]==false) {
                    flag[rndpos] = true;
                    scrambledword[i] = currentword[rndpos];
                    gotrandomchar = true;
                }
            } while (gotrandomchar==false);
        }
    scrambledword[MAXWORDLENGTH-1] = NULL;
    //cout << "The original word is " << testword << "\n";
    cout << "The scrambled word is " << scrambledword << "\n";    
    


        //
        //
    while (keep_playing = true);

    scrambledword[MAXWORDLENGTH-1] = NULL;
    cout << "The scrambled word is " << scrambledword << "\n";

    cout << "Guess the word letter by letter\n";
        char inputdata;
        cin >> inputdata;

        if (inputdata=='i') {
            //
            //
            cout << "Correct! Please continue.\n";
            ("player_score = player_score + 1");
            
            } else {
            //
            //
            cout << "Sorry, that is not the correct answer.\n";
            ("number_of_lives = number_of_lives -1");
            }
        }

    //return 1;
        }
        }




If any one show me were i have gone wrong it would be great help as this has to be in today
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Guessing Game Application
5 Dec, 2006 - 06:30 AM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,355



Thanked: 51 times
Dream Kudos: 25
My Contributions
Can you please provide a clear description of the problem you are encountering, along with any error messages that are generated at compilation or runtime?
User is online!Profile CardPM
+Quote Post

KevSmith
RE: Guessing Game Application
5 Dec, 2006 - 06:37 AM
Post #3

New D.I.C Head
*

Joined: 3 Dec, 2006
Posts: 12


My Contributions
I can't see were the problem is,, i checked locial errors but still can't excute it,, Urgent help needed
User is offlineProfile CardPM
+Quote Post

Antiokus
RE: Guessing Game Application
5 Dec, 2006 - 07:28 AM
Post #4

D.I.C Head
**

Joined: 6 Sep, 2006
Posts: 128



Thanked: 1 times
My Contributions
Just ran through it really quick and I was seeing some missing braces. You might check about that.
User is offlineProfile CardPM
+Quote Post

KevSmith
RE: Guessing Game Application
5 Dec, 2006 - 07:33 AM
Post #5

New D.I.C Head
*

Joined: 3 Dec, 2006
Posts: 12


My Contributions
can u tell me were plz cause this looks all the same to me now
User is offlineProfile CardPM
+Quote Post

Antiokus
RE: Guessing Game Application
5 Dec, 2006 - 07:50 AM
Post #6

D.I.C Head
**

Joined: 6 Sep, 2006
Posts: 128



Thanked: 1 times
My Contributions
When I looked at it there was a missing closing bracket at the end. As far as other ones I don't know. What compiler are you using?
User is offlineProfile CardPM
+Quote Post

BitByte
RE: Guessing Game Application
5 Dec, 2006 - 08:50 AM
Post #7

D.I.C Head
**

Joined: 9 Aug, 2006
Posts: 194



Thanked: 3 times
My Contributions
You need to declare your variables outside your do-while loops. scrambledword is out of scope, the compiler says it's an undeclared identifier. This bit:

CODE
scrambledword[MAXWORDLENGTH-1] = NULL;


Gives an error of 'converting to non-pointer type 'char' from null. You are missing a while at the end of your main do-while loop.

This bit:

CODE
char currentword[NUMBEROFWORDS] = {myChosenWord};


Gives an error of 'invalid conversion from 'char *' to 'char' '

There is too much going on inside your do while loops. It is confusing you, it confused me trying to sort it out. No offense, i think you just need to break it up into smaller more manageable chunks.

Edit: forgot to mention this bit. player_score and number_of_lives are members of struct player so they should be

CODE
myPlayer.number_of_lives = myPlayer.number_of_lives - 1;


This post has been edited by BitByte: 5 Dec, 2006 - 08:54 AM
User is offlineProfile CardPM
+Quote Post

KevSmith
RE: Guessing Game Application
5 Dec, 2006 - 10:21 AM
Post #8

New D.I.C Head
*

Joined: 3 Dec, 2006
Posts: 12


My Contributions
no i've tryed what your saying, i don't really wanna have to start this again,,,
has anyone tryed compiling this i badly want this to work
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Guessing Game Application
5 Dec, 2006 - 10:35 AM
Post #9

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,355



Thanked: 51 times
Dream Kudos: 25
My Contributions
Are you getting any error messages? If so, please post them, as per my original post.
User is online!Profile CardPM
+Quote Post

KevSmith
RE: Guessing Game Application
5 Dec, 2006 - 10:37 AM
Post #10

New D.I.C Head
*

Joined: 3 Dec, 2006
Posts: 12


My Contributions
this is what i am getting:

Compiling...
Text2.cpp
M:\CMPCD1016 Tutorials\Coursework\Text2.cpp(89) : error C2065: 'myChosenWord' : undeclared identifier
M:\CMPCD1016 Tutorials\Coursework\Text2.cpp(150) : fatal error C1004: unexpected end of file found
Error executing cl.exe.

Coursework.exe - 2 error(s), 0 warning(s)

User is offlineProfile CardPM
+Quote Post

KevSmith
RE: Guessing Game Application
5 Dec, 2006 - 11:39 AM
Post #11

New D.I.C Head
*

Joined: 3 Dec, 2006
Posts: 12


My Contributions
This code is doing my head in,, can anyone plz try and get it working as i planning on smashing my laptop up.. or is there anyother way i can get around my problem thanks
User is offlineProfile CardPM
+Quote Post

BitByte
RE: Guessing Game Application
5 Dec, 2006 - 11:46 AM
Post #12

D.I.C Head
**

Joined: 9 Aug, 2006
Posts: 194



Thanked: 3 times
My Contributions
You need to put a while at the end of your last bracket. Then another bracket on the next line down. myChosenWord is out of scope, the compiler doesn't know what it is.
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Reply to this topicStart new topic
Time is now: 1/8/09 08:42PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month