Page 1 of 2 12 LastLast
Results 1 to 30 of 35

Thread: What is wrong with this code (C++)

  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.

  7. #7

    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.

  8. #8

    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.

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

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

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

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

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

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

  15. #15
    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?

  16. #16
    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!

  17. #17

    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

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

  19. #19

    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.

  20. #20

    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

  21. #21

    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.

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

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

  24. #24

    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.

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

    well, doc, let us know how it comes along :)
    Of course.

  26. #26
    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 need more help, this time it is not the compiler that is causing the problems.

    Here is the current code, I know it looks messy the final version will have comments in it.

    #include <iostream.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    #include <stdio.h>

    void main()
    {
    #define EASY_LEVEL 1
    #define MEDIUM_LEVEL 2
    #define HARD_LEVEL 3
    #define QUIT 4
    int Option;
    bool Word_Complete = false;
    bool Run = true;
    #define NUMBEROFWORDSEASY 9
    #define MAXWORDLENGTHEASY 6
    int randomnumber;
    char wordeasy[NUMBEROFWORDSEASY][MAXWORDLENGTHEASY] = { "print", "phone", "worth", "throw", "games", "study", "error", "debug", "build"};
    char myChosenWordEasy[MAXWORDLENGTHEASY];
    char input;
    int length;
    char scrambledword[MAXWORDLENGTHEASY];
    char answer[MAXWORDLENGTHEASY];
    int WordsAnswered;
    int Guess;
    int WordCount;
    int i;
    bool gotrandomchar = false;

    do {
    cout<< "Game intructions\n";
    cout<< "Please select a difficulty level\n";
    cout<< "1=Easy\n";
    cout<< "2=Medium\n";
    cout<< "3=Hard\n";
    cout<< "4=Quit\n";

    cin>> Option;

    switch (Option)
    {
    case EASY_LEVEL:
    do
    {srand(time(NULL));
    randomnumber = rand()%NUMBEROFWORDSEASY;
    strcpy(myChosenWordEasy, wordeasy[randomnumber]);
    cout << "The random word is " << myChosenWordEasy << endl;

    bool flag[MAXWORDLENGTHEASY-1];
    for (i=0;i<MAXWORDLENGTHEASY-1;i++)
    flag[i] = false;
    myChosenWordEasy[MAXWORDLENGTHEASY];
    length = MAXWORDLENGTHEASY-1;
    for (i=0;i<MAXWORDLENGTHEASY-1;i++) {
    bool gotrandomchar = false;
    do {
    int rndpos = rand()%length;
    if (flag[rndpos]==false) {
    flag[rndpos] = true;
    scrambledword[i] = myChosenWordEasy[rndpos];
    gotrandomchar = true;
    }
    }
    while (gotrandomchar==false);
    }
    scrambledword[MAXWORDLENGTHEASY-1] = NULL;
    cout << "The original word is " << myChosenWordEasy << "\n";
    cout << "The scrambled word is " << scrambledword << "\n";

    cin >> answer;
    if (answer == myChosenWordEasy) {
    cout<< "The word is correct\n";
    WordCount = WordsAnswered + 1;
    if (WordsAnswered == 3) {
    cout << "Well done!!!\n";
    Word_Complete = true;
    }
    }
    else {
    cout<< "The word is not correct\n";
    Guess = Guess + 1;
    if (Guess == 3) {
    cout<< "You have failed\n";
    Word_Complete = true;
    }
    }
    }
    while(Word_Complete == false);
    break;

    case MEDIUM_LEVEL:
    cout<< "Display scambled word Medium\n";
    do {
    cout<< "Medium loop is working\n";
    Word_Complete = true;
    }
    while(Word_Complete == false);
    break;

    case HARD_LEVEL:
    cout<< "Display scambled word Hard\n";
    do {
    cout<< "Hard loop is working\n";
    Word_Complete = true;
    }
    while(Word_Complete == false);
    break;
    case QUIT:
    Run = false;

    default:
    cout<< "Press enter to end game\n";
    }
    }
    while (Run == true);
    }
    Problem 1:
    If I type in the correct answer for the scrambled word, it says it is incorrect. I think is something to do with answer and myChosenWordEasy being arrays.

    I think this is wrong:
    Guess = Guess + 1
    If you can make sense of this and help, Thank You.
    Last edited by The_Doctor; 11-29-2006 at 20:58.

  27. #27

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

    Hi Doc,

    I have a few suggestions:

    1. Initialize Guess. You shouldn't try to use uninitialized variables - and while we're at it, I'll remind you again to enable warnings in your compiler - using uninitialized variables should cause a warning.
    Make sure you initialize all other variables before using them (note: here, by "use" I mean "read" from them, test them; if you first write to them before any use, then you don't need to initialize them).

    2. You are right, you cannot compare two char arrays to figure out if two strings are identical. Use strcmp instead (man strcmp).

    3. Hard to read unindented code (I'm assuming it's because of the way the post gets formatted by the board), but I'm not sure what this line does:

    myChosenWordEasy[MAXWORDLENGTHEASY];

    in the context:
    for (i=0;i<MAXWORDLENGTHEASY-1;i++)
    flag[i] = false;
    myChosenWordEasy[MAXWORDLENGTHEASY];
    length = MAXWORDLENGTHEASY-1;

    Is that an incompletely pasted line ? The code shouldn't even compile with a line like that, as far as I can tell.

    -----------

    I really don't understand the second half of your post, so I can't make any comments on that.

    hope this helps a bit.
    Therapy helps, but screaming obscenities is cheaper.

  28. #28
    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'm not sure what this line does:

    myChosenWordEasy[MAXWORDLENGTHEASY];
    I copied and pasted the code that we where given and the start of the project and then modified it.

    That line used to declare myChosenWordEasy as a variable, and since then I have moved it to the top of the code. It would seem that I had forgot to delete it.

    Hard to read unindented code (I'm assuming it's because of the way the post gets formatted by the board)
    Yes it is the board.

    I really don't understand the second half of your post, so I can't make any comments on that.
    Ignore that bit.

    2. You are right, you cannot compare two char arrays to figure out if two strings are identical. Use strcmp instead (man strcmp).
    That is very useful, thanks.

  29. #29
    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:
    The strcmp worked perfectly. Thank You Blodrast.


    More questions:
    1. How do I take a single letter out of a string and compare it to another letter in another string that only has one letter.

    For example:
    I have the word "games", then it is scrambled to "agmse". "agmse" appear on the screen and the user is asked to guess the first letter of the scrambled word. Then the user inputs "g" and then the program checks if the this matches the first letter in the original word. Then it asks the user to guess the second letter and check that, and so on until it gets to the end of the word.

    I have a feeling that it will contain a for loop.

  30. #30

    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.

Page 1 of 2 12 LastLast

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