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

    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!

  2. #2

    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

  3. #3

    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.

  4. #4

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

    Quote Originally Posted by Blodrast
    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.
    I've just spotted that! So without the semicolons and, if the booleans are initialised it should work? (bear in mind it has been a long time, and I am more C than C++ !) e.g.

    Code:
    #include <iostream.h>
    
    int main()
    {
    bool Easy=false;
    bool Medium=false;
    bool Hard=false;
    int Option=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::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;
    }
    ???
    “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

  5. #5

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

    well, I'll be honest and I'll admit that I haven't actually tried the code - just looked at it.
    But yeah, as far as I can tell, this should work just fine. Conceptually, it's correct, although it's always possible to miss a semicolon or some such.
    Therapy helps, but screaming obscenities is cheaper.

  6. #6
    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 Manco Capac
    I've just spotted that! So without the semicolons and, if the booleans are initialised it should work? (bear in mind it has been a long time, and I am more C than C++ !) e.g.

    Code:
    #include <iostream.h>
     
    int main()
    {
    bool Easy=false;
    bool Medium=false;
    bool Hard=false;
    int Option = 0;
     
    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;
    }
    ???
    Ok... I have had a second look...
    Manco Capac's code should work... if you initialise with a zero instead of false on the integer .

    So, doc... you want to put a game in each of those loops?
    Status Emeritus

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

    A few posts ago I did say I got it working in university.

    So, doc... you want to put a game in each of those loops?
    Yes. It is scrambled word solving game. The program picks a word out of a list and scrambles it. Then the player must de-scramble it, one letter at a time. I have all the code for this, it is just a matter of intergreating it into my code.
    Last edited by The_Doctor; 11-25-2006 at 17:51.

  8. #8

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

    well, doc, let us know how it comes along :)
    Sigurd: false IS zero - the wonders of C, eh ?
    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