Results 1 to 30 of 35

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

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #25
    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.

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