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

  2. #2
    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

  3. #3
    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...

  4. #4
    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

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

    It works now.

    I tried the code on one of the computers in university and it worked.

    I wonder why it does not work on my computer?

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

    Quote Originally Posted by The_Doctor
    It works now.

    I tried the code on one of the computers in university and it worked.

    I wonder why it does not work on my computer?
    So when are we moving on to conquest, boys? Don't spoil my fun, hey!

  7. #7

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

    Nothing appears to be wrong with that code in your first post apart from the booleans need to be initialised.
    “The majestic equality of the laws prohibits the rich and the poor alike from sleeping under bridges, begging in the streets and stealing bread.” - Anatole France

    "The law is like a spider’s web. The small are caught, and the great tear it up.” - Anacharsis

  8. #8

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

    Quote Originally Posted by Manco Capac
    Nothing appears to be wrong with that code in your first post apart from the booleans need to be initialised.
    There is. He wants loops, but his while loops don't do anything, because they end in a semicolon. His code IS, both syntactically, and semantically, correct, yes, but I very much doubt that was what he wanted (while loops that do nothing), at least from what he explained.
    Therapy helps, but screaming obscenities is cheaper.

  9. #9
    Master of Few Words Senior Member KukriKhan's Avatar
    Join Date
    Jun 2001
    Posts
    10,415

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

    Quote Originally Posted by LeftEyeNine
    So when are we moving on to conquest, boys? Don't spoil my fun, hey!
    LoL. I think you install his mini-game, and pick "Medium". No one ever picks medium. Then 'the plan' is revealed.
    Be well. Do good. Keep in touch.

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