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 What is wrong with this code (C++)

    For a peice of coursework for my uni course I have to make a simple game.

    I not very experienced with C++.

    There are 3 difficulty levels in it, each one inside a loop which will contain the rest of the game. In this code I am testing it to see if the loops work.

    #include <iostream.h>

    int main()
    {
    bool Easy;
    bool Medium;
    bool Hard;
    int Option;

    std::cout<< "Game intructions\n";
    std::cout<< "Please select a difficulty level\n";
    std::cout<< "1=Easy\n";
    std::cout<< "2=Medium\n";
    std::cout<< "3=Hard\n";
    std::cout<< "4=Quit\n";
    std::cin>> Option;

    if (Option == 1) {
    Easy = true;
    }

    if (Option == 2) {
    Medium = true;
    }

    if (Option == 3) {
    Hard = true;
    }

    if (Option == 4) {
    return 0;
    }

    while (Easy == true);
    {
    cout<< "Easy test works\n";
    Easy = false;
    }

    while (Medium == true);
    {
    cout<< "Medium test works\n";
    Medium = false;
    }

    while (Hard == true);
    {
    cout<< "Hard test works\n";
    Hard = false;
    }
    return 0;
    }
    When I press 1 nothing happens.
    When I press 2 it says "Easy Test Works".
    When I press 3 it says "Easy Test Works", then "Medium Test Works".
    Pressing 4 does end the program.

    I have no idea way this is happening.
    Last edited by The_Doctor; 11-22-2006 at 16:52.

  2. #2
    zombologist Senior Member doc_bean's Avatar
    Join Date
    Oct 2004
    Location
    Riding Shai-Hulud
    Posts
    5,346

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

    Try initiating your booleans (all false)

    Have you tried using indirect input ? ( somthing like stringstream and then extracting)

    Do you have to use while loops ? I'd use a case structure here.

    I might be missing something obvious though, but I can't spot an obvious mistake.

    EDIT: I'd also make use of only 2 booleans, if it isn't one or the other it has to be the thrid state, you might want to include an exception (or default) for the fourth state though.
    Last edited by doc_bean; 11-22-2006 at 19:00.
    Yes, Iraq is peaceful. Go to sleep now. - Adrian II

  3. #3
    Iron Fist Senior Member Husar's Avatar
    Join Date
    Jan 2003
    Location
    Germany
    Posts
    15,617

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

    I'd also get rid of the booleans and simply use the number in the option variable to determine the difficulty.
    Can't say a lot about the rest, looks ok to me, but I never learned C++.


    "Topic is tired and needs a nap." - Tosa Inu

  4. #4

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

    get rid of the ";" at the end of the while instructions;
    i.e., instead of

    while () ;
    {

    }


    you should probably use

    while () {

    }
    Therapy helps, but screaming obscenities is cheaper.

  5. #5

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

    one more thing: if you're also being marked for style, it's poor style to use numerical constants in your code; if I were you, I'd replace the comparisons "if option == 2" with something like:

    #define MODERATE_LEVEL 2

    if (option == MODERATE_LEVEL) ...

    And the same for the other options, naturally.

    my two cents.
    Therapy helps, but screaming obscenities is cheaper.

  6. #6
    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 changed around some of it, and now it does not work. When you enter something and press enter it ends the program.

    #include <iostream.h>

    void main()
    {
    #define Easy_Level 1
    #define Medium_Level 2
    #define Hard_Level 3
    int Option;
    bool Word_Complete;

    std::cout<< "Game intructions\n";
    std::cout<< "Please select a difficulty level\n";
    std::cout<< "1=Easy\n";
    std::cout<< "2=Medium\n";
    std::cout<< "3=Hard\n";
    std::cin>> Option;

    if (Option == Easy_Level) {
    do
    {cout<< "Display scambled word\n";
    Word_Complete = true;
    }
    while(Word_Complete = false);
    }

    if (Option == Medium_Level) {
    do
    {cout<< "Display scambled word\n";
    Word_Complete = true;
    }
    while(Word_Complete = false);
    }

    if (Option == Hard_Level) {
    do
    {cout<< "Display scambled word\n";
    Word_Complete = true;
    }
    while(Word_Complete = false);
    }

    return 0;
    }
    Last edited by The_Doctor; 11-22-2006 at 21:10.

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