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

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

    Say the word to be guessed is named wtbg.

    guessed_chars_so_far = 0;
    while (guessed_chars_so_far < strlen (wtbg)) {
    boolean guessed = false;
    while (guessed == false) {
    cout << "Guess character in position " << guessed_chars_so_far << endl;
    cin >> c;
    if (c == wtbg[guessed_chars_so_far]) {
    cout << "Correct !" << endl;
    guessed = true;
    guessed_chars_so_far ++;
    }
    else {
    cout << "Wrong guess, let's try again " << endl;
    }
    }
    }


    To answer your question, to compare two characters you don't need strcmp, you can simply compare them (with ==). If you want to compare two strings, you need to use strcmp.

    edit: even though my code was indented, the board ignores that...
    Therapy helps, but screaming obscenities is cheaper.

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

    Great, I will try it tomorrow.

  3. #3
    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 Blodrast
    edit: even though my code was indented, the board ignores that
    guessed_chars_so_far = 0;
    while (guessed_chars_so_far < strlen (wtbg))


    {
    boolean guessed = false;
    while (guessed == false)


    {
    cout << "Guess character in position " << guessed_chars_so_far << endl;
    cin >> c;
    if (c == wtbg[guessed_chars_so_far])
    {

    cout << "Correct !" << endl;
    guessed = true;
    guessed_chars_so_far ++;

    }
    else


    {
    cout << "Wrong guess, let's try again " << endl;

    }

    }

    }


    ------------------------------------------------------------------
    Like this? ...
    Status Emeritus

  4. #4

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

    gasp!
    what is the secret ?! It took me longer to indent the stuff than it took to write the damn thing...
    Therapy helps, but screaming obscenities is cheaper.

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

    Update:
    It works!!! (well more or less)

    There are a few bugs to work out, I should be able to work these out by myself, if I can't there is always you guys.

  6. #6

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

    great, good to hear :)
    Therapy helps, but screaming obscenities is cheaper.

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