Results 1 to 30 of 35

Thread: What is wrong with this code (C++)

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Time Lord Member The_Doctor's Avatar
    Join Date
    Oct 2004
    Location
    The TARDIS
    Posts
    2,040

    Default Re: What is wrong with this code (C++)

    well, doc, let us know how it comes along :)
    Of course.

  2. #2
    Time Lord Member The_Doctor's Avatar
    Join Date
    Oct 2004
    Location
    The TARDIS
    Posts
    2,040

    Default Re: What is wrong with this code (C++)

    I need more help, this time it is not the compiler that is causing the problems.

    Here is the current code, I know it looks messy the final version will have comments in it.

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

    void main()
    {
    #define EASY_LEVEL 1
    #define MEDIUM_LEVEL 2
    #define HARD_LEVEL 3
    #define QUIT 4
    int Option;
    bool Word_Complete = false;
    bool Run = true;
    #define NUMBEROFWORDSEASY 9
    #define MAXWORDLENGTHEASY 6
    int randomnumber;
    char wordeasy[NUMBEROFWORDSEASY][MAXWORDLENGTHEASY] = { "print", "phone", "worth", "throw", "games", "study", "error", "debug", "build"};
    char myChosenWordEasy[MAXWORDLENGTHEASY];
    char input;
    int length;
    char scrambledword[MAXWORDLENGTHEASY];
    char answer[MAXWORDLENGTHEASY];
    int WordsAnswered;
    int Guess;
    int WordCount;
    int i;
    bool gotrandomchar = false;

    do {
    cout<< "Game intructions\n";
    cout<< "Please select a difficulty level\n";
    cout<< "1=Easy\n";
    cout<< "2=Medium\n";
    cout<< "3=Hard\n";
    cout<< "4=Quit\n";

    cin>> Option;

    switch (Option)
    {
    case EASY_LEVEL:
    do
    {srand(time(NULL));
    randomnumber = rand()%NUMBEROFWORDSEASY;
    strcpy(myChosenWordEasy, wordeasy[randomnumber]);
    cout << "The random word is " << myChosenWordEasy << endl;

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

    cin >> answer;
    if (answer == myChosenWordEasy) {
    cout<< "The word is correct\n";
    WordCount = WordsAnswered + 1;
    if (WordsAnswered == 3) {
    cout << "Well done!!!\n";
    Word_Complete = true;
    }
    }
    else {
    cout<< "The word is not correct\n";
    Guess = Guess + 1;
    if (Guess == 3) {
    cout<< "You have failed\n";
    Word_Complete = true;
    }
    }
    }
    while(Word_Complete == false);
    break;

    case MEDIUM_LEVEL:
    cout<< "Display scambled word Medium\n";
    do {
    cout<< "Medium loop is working\n";
    Word_Complete = true;
    }
    while(Word_Complete == false);
    break;

    case HARD_LEVEL:
    cout<< "Display scambled word Hard\n";
    do {
    cout<< "Hard loop is working\n";
    Word_Complete = true;
    }
    while(Word_Complete == false);
    break;
    case QUIT:
    Run = false;

    default:
    cout<< "Press enter to end game\n";
    }
    }
    while (Run == true);
    }
    Problem 1:
    If I type in the correct answer for the scrambled word, it says it is incorrect. I think is something to do with answer and myChosenWordEasy being arrays.

    I think this is wrong:
    Guess = Guess + 1
    If you can make sense of this and help, Thank You.
    Last edited by The_Doctor; 11-29-2006 at 20:58.

  3. #3

    Default Re: What is wrong with this code (C++)

    Hi Doc,

    I have a few suggestions:

    1. Initialize Guess. You shouldn't try to use uninitialized variables - and while we're at it, I'll remind you again to enable warnings in your compiler - using uninitialized variables should cause a warning.
    Make sure you initialize all other variables before using them (note: here, by "use" I mean "read" from them, test them; if you first write to them before any use, then you don't need to initialize them).

    2. You are right, you cannot compare two char arrays to figure out if two strings are identical. Use strcmp instead (man strcmp).

    3. Hard to read unindented code (I'm assuming it's because of the way the post gets formatted by the board), but I'm not sure what this line does:

    myChosenWordEasy[MAXWORDLENGTHEASY];

    in the context:
    for (i=0;i<MAXWORDLENGTHEASY-1;i++)
    flag[i] = false;
    myChosenWordEasy[MAXWORDLENGTHEASY];
    length = MAXWORDLENGTHEASY-1;

    Is that an incompletely pasted line ? The code shouldn't even compile with a line like that, as far as I can tell.

    -----------

    I really don't understand the second half of your post, so I can't make any comments on that.

    hope this helps a bit.
    Therapy helps, but screaming obscenities is cheaper.

  4. #4
    Time Lord Member The_Doctor's Avatar
    Join Date
    Oct 2004
    Location
    The TARDIS
    Posts
    2,040

    Default Re: What is wrong with this code (C++)

    I'm not sure what this line does:

    myChosenWordEasy[MAXWORDLENGTHEASY];
    I copied and pasted the code that we where given and the start of the project and then modified it.

    That line used to declare myChosenWordEasy as a variable, and since then I have moved it to the top of the code. It would seem that I had forgot to delete it.

    Hard to read unindented code (I'm assuming it's because of the way the post gets formatted by the board)
    Yes it is the board.

    I really don't understand the second half of your post, so I can't make any comments on that.
    Ignore that bit.

    2. You are right, you cannot compare two char arrays to figure out if two strings are identical. Use strcmp instead (man strcmp).
    That is very useful, thanks.

  5. #5
    Time Lord Member The_Doctor's Avatar
    Join Date
    Oct 2004
    Location
    The TARDIS
    Posts
    2,040

    Default Re: What is wrong with this code (C++)

    Update:
    The strcmp worked perfectly. Thank You Blodrast.


    More questions:
    1. How do I take a single letter out of a string and compare it to another letter in another string that only has one letter.

    For example:
    I have the word "games", then it is scrambled to "agmse". "agmse" appear on the screen and the user is asked to guess the first letter of the scrambled word. Then the user inputs "g" and then the program checks if the this matches the first letter in the original word. Then it asks the user to guess the second letter and check that, and so on until it gets to the end of the word.

    I have a feeling that it will contain a for loop.

  6. #6

    Default Re: What is wrong with this code (C++)

    Say the word to be guessed is named wtbg.

    guessed_chars_so_far = 0;
    while (guessed_chars_so_far < strlen (wtbg)) {
    boolean guessed = false;
    while (guessed == false) {
    cout << "Guess character in position " << guessed_chars_so_far << endl;
    cin >> c;
    if (c == wtbg[guessed_chars_so_far]) {
    cout << "Correct !" << endl;
    guessed = true;
    guessed_chars_so_far ++;
    }
    else {
    cout << "Wrong guess, let's try again " << endl;
    }
    }
    }


    To answer your question, to compare two characters you don't need strcmp, you can simply compare them (with ==). If you want to compare two strings, you need to use strcmp.

    edit: even though my code was indented, the board ignores that...
    Therapy helps, but screaming obscenities is cheaper.

  7. #7
    Time Lord Member The_Doctor's Avatar
    Join Date
    Oct 2004
    Location
    The TARDIS
    Posts
    2,040

    Default Re: What is wrong with this code (C++)

    Great, I will try it tomorrow.

  8. #8
    Dragonslayer Emeritus Senior Member Sigurd's Avatar
    Join Date
    Nov 2002
    Location
    Norge
    Posts
    6,877

    Default Re: What is wrong with this code (C++)

    Quote Originally Posted by Blodrast
    edit: even though my code was indented, the board ignores that
    guessed_chars_so_far = 0;
    while (guessed_chars_so_far < strlen (wtbg))


    {
    boolean guessed = false;
    while (guessed == false)


    {
    cout << "Guess character in position " << guessed_chars_so_far << endl;
    cin >> c;
    if (c == wtbg[guessed_chars_so_far])
    {

    cout << "Correct !" << endl;
    guessed = true;
    guessed_chars_so_far ++;

    }
    else


    {
    cout << "Wrong guess, let's try again " << endl;

    }

    }

    }


    ------------------------------------------------------------------
    Like this? ...
    Status Emeritus

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Single Sign On provided by vBSSO