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++)

    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.

  2. #2

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

    Again, I believe you're doing something that you're not intending to do:
    in your while statements, I am guessing you want to test the condition, not to assign false to the boolean variable.
    Hence, it should probably be
    while (Word_Complete == false)

    and NOT

    while (Word_Complete = false)

    Notice the difference ? ( == instead of =).
    Also, in my previous comment, with regard to style, named constants should be in all caps. Like I said, EASY_LEVEL, not Easy_Level. It's just a convention, but that's how things are done. Again, if you're not getting any marks for style, you may as well not bother, although it's nice in general to have readable code.
    Therapy helps, but screaming obscenities is cheaper.

  3. #3

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

    One more thing: you're using the Word_Complete variable uninitialized.
    Turn on warnings in your compiler (-Wall if you're using gcc/g++), it will notify you about it.
    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 did what you advised. It did not work. I also tried it with a case/switch statement.

    #include <iostream.h>

    void main()
    {
    #define EASY_LEVEL 1
    #define MEDIUM_LEVEL 2
    #define HARD_LEVEL 3
    int Option;
    bool Word_Complete = false;

    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;

    switch (Option)
    {
    case EASY_LEVEL:
    do
    {cout<< "Display scambled word Easy\n";
    Word_Complete = true;
    }
    while(Word_Complete == false);
    break;
    case MEDIUM_LEVEL:
    do
    {cout<< "Display scambled word Medium\n";
    Word_Complete = true;
    }
    while(Word_Complete == false);
    break;
    case HARD_LEVEL:
    do
    {cout<< "Display scambled word Hard\n";
    Word_Complete = true;
    }
    while(Word_Complete == false);
    break;
    default:
    cout<< "???";
    }
    return 0;
    }
    OR

    #include <iostream.h>

    void main2()
    {
    #define EASY_LEVEL 1
    #define MEDIUM_LEVEL 2
    #define HARD_LEVEL 3
    int Option;
    bool Word_Complete = false;

    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 Easy\n";
    Word_Complete = true;
    }
    while(Word_Complete == false);
    }

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

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

    }
    I am using Dev-C++ version 4.

  5. #5
    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++)

    I am not sure what you are trying to do here doc...
    but if you want a hotfix:

    {

    #define EASY_LEVEL 1
    #define MEDIUM_LEVEL 2
    #define HARD_LEVEL 3
    #define END 4

    int Option;
    bool Word_Complete = false;

    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";


    do
    {
    std::cin>> Option;

    if (Option == EASY_LEVEL)
    {
    std::cout<< "Display scambled word Easy\n";
    Word_Complete = true;
    }

    else if (Option == MEDIUM_LEVEL)
    {
    std::cout<< "Display scambled word Medium\n";
    Word_Complete = true;
    }

    else if (Option == HARD_LEVEL)
    {
    std::cout<< "Display scambled word Hard\n";
    Word_Complete = true;
    }

    else
    {
    return 0;
    }
    }
    while (Option != END);
    }
    Last edited by Sigurd; 11-22-2006 at 22:58.
    Status Emeritus

  6. #6
    Boy's Guard Senior Member LeftEyeNine's Avatar
    Join Date
    Sep 2003
    Location
    Yozgat
    Posts
    5,168

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

    Wow, looking at these, I feel like a CEO of a very serious corporation which is dedicated to conquer the world.

    Keep on guys, I'll pump the wages

  7. #7
    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++)

    That code looks pretty easy, I should try C++, it's to Java like German is to Dutch or so.
    Also I noticed there is a fault here:

    std::cout<< "1=Easy\n";
    std::cout<< "2=Medium\n";
    std::cout<< "3=Hard\n";
    std::cin>> Option;

    if (Option == EASY_LEVEL)
    in the if-clause you check whether option is EASY_LEVEL, but since option is an integer, it cannot be a string and besides that, option was only assigned a number so that can never ever return true.
    You either need to use a different check or turn option into a string and add three more lines and another variable and....


    Despite Blodrast saying this was bad style, I would use

    if (Option == 1)
    Or you could change the start to say:

    #include <string>

    int Option;
    string difficulty;
    bool Word_Complete = false;

    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 == 1) {
    difficulty = "EASY_LEVEL"}

    if (option == 2) {
    difficulty = "MEDIUM_LEVEL"}

    if (option == 3) {
    difficulty = "HARD_LEVEL"}
    the three ifs could also be a case-option but then you could later check with

    if (difficulty == "EASY_LEVEL")
    {
    std::cout<< "Display scambled word Easy\n";
    }
    but in Java checking strings does not work like this IIRC, so the condition may need a big change.
    Ok, guess this really needs something else to check what is inside the string, but I would actually prefer numbers for such a small program anyway...
    Last edited by Husar; 11-22-2006 at 23:47.


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

  8. #8
    Member Member Alexander the Pretty Good's Avatar
    Join Date
    Jun 2004
    Location
    New Jersey, USA
    Posts
    4,979

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

    The_Doctor - the program ends after displaying the difficulty level because you haven't put anything after the switch statement that displays the difficulty level. If you want the program to continue after that, either add what else the code needs to do or put the entire thing in a loop as Sigurd Fafnesbane has done.

    I would recommend using <iostream>instead of <iostream.h> and then put the phrase "using namespace std;" under it. Then you won't have to put std:: in front of every cout and cin. Just a little trick.

    Husar - he's defined EASY_LEVEL to be 1, so the compiler treats every "EASY_LEVEL" as a 1. I guess Java doesn't have that feature...

  9. #9
    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++)

    Quote Originally Posted by Alexander the Pretty Good
    Husar - he's defined EASY_LEVEL to be 1, so the compiler treats every "EASY_LEVEL" as a 1. I guess Java doesn't have that feature...
    You're right, I didn't notice that.
    At least my lazy brain got to think about some code again.


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

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