I need help

I'm doing my project which contain a game called Code Breaker. It is a game that the computer generates a four digits code and the user to guess/break the code. Now I'm facing a problem of how to get the user's input code and compare with the secret code. Can anybody help me?
Thanks!

Jack

Comments

  • Hi Jack,
    What language are you using for your game?

    Kreitler

    : I'm doing my project which contain a game called Code Breaker. It is a game that the computer generates a four digits code and the user to guess/break the code. Now I'm facing a problem of how to get the user's input code and compare with the secret code. Can anybody help me?
    : Thanks!
    :
    : Jack
    :

  • I'm using C++. Hope anybody can help me.

    : Hi Jack,
    : What language are you using for your game?
    :
    : Kreitler
    :
    : : I'm doing my project which contain a game called Code Breaker. It is a game that the computer generates a four digits code and the user to guess/break the code. Now I'm facing a problem of how to get the user's input code and compare with the secret code. Can anybody help me?
    : : Thanks!
    : :
    : : Jack
    : :
    :
    :

  • Hi again, Jack,
    You have quite a few options open to you in C++.

    First, you can use the input stream operator, '>>'. This is sort of the 'C++ standard' for reading input, but it tends to make your programs big. A quick search on google will show you lots of examples, but I think this will work:
    char n1, n2, n3, n4;
    cin >> n1 >> n2 >> n3 >> n4;

    You can also use getChar() to read one character at a time from the keyboard. Something like this:

    char n1, n2, n3, n4;
    n1 = getChar();
    n2 = getChar();
    n3 = getChar();
    n4 = getChar();

    would read 4 characters from the keyboard and store them in the variables n1-n4.

    There are other functions and macros you can use, like fgetc(), getc(), gets() and fgets(), but the two methods shown above are probably the most straightforward.

    I hope this helps.

    Kreitler

    : I'm using C++. Hope anybody can help me.
    :
    : : Hi Jack,
    : : What language are you using for your game?
    : :
    : : Kreitler
    : :
    : : : I'm doing my project which contain a game called Code Breaker. It is a game that the computer generates a four digits code and the user to guess/break the code. Now I'm facing a problem of how to get the user's input code and compare with the secret code. Can anybody help me?
    : : : Thanks!
    : : :
    : : : Jack
    : : :
    : :
    : :
    :
    :

  • Hi Kreitler,
    Thanks for your help first. I know i can do it this way. But I need to get the input like this.
    Iuput number e.g. 1234 as a string, not one by one. And how do i compare it whith the computer secret code and print the message that how many digits are correct and in the right position; and how many are in the wrong position.


    : Hi again, Jack,
    : You have quite a few options open to you in C++.
    :
    : First, you can use the input stream operator, '>>'. This is sort of the 'C++ standard' for reading input, but it tends to make your programs big. A quick search on google will show you lots of examples, but I think this will work:
    : char n1, n2, n3, n4;
    : cin >> n1 >> n2 >> n3 >> n4;
    :
    : You can also use getChar() to read one character at a time from the keyboard. Something like this:
    :
    : char n1, n2, n3, n4;
    : n1 = getChar();
    : n2 = getChar();
    : n3 = getChar();
    : n4 = getChar();
    :
    : would read 4 characters from the keyboard and store them in the variables n1-n4.
    :
    : There are other functions and macros you can use, like fgetc(), getc(), gets() and fgets(), but the two methods shown above are probably the most straightforward.
    :
    : I hope this helps.
    :
    : Kreitler
    :
    : : I'm using C++. Hope anybody can help me.
    : :
    : : : Hi Jack,
    : : : What language are you using for your game?
    : : :
    : : : Kreitler
    : : :
    : : : : I'm doing my project which contain a game called Code Breaker. It is a game that the computer generates a four digits code and the user to guess/break the code. Now I'm facing a problem of how to get the user's input code and compare with the secret code. Can anybody help me?
    : : : : Thanks!
    : : : :
    : : : : Jack
    : : : :
    : : :
    : : :
    : :
    : :
    :
    :

  • Hey Jack,
    You can use scanf or fgets to read a string from the keyboard (I don't want to spoil your fun and supply the whole syntax--but you should be able to find the info quickly on-line).

    You'll be reading the input into a string (i.e., a char array), so it's easy to check each element:

    [code]
    char code[4];
    char userGuess[4];

    /* Fill 'code' with the sequence the user must decode */

    /* Use fgets() to read the characters from stdin into 'userGuess' */

    for (int i=0; i<4; i++) {
    /* Compare code[i] with userGuess[i] to see if they match. */
    /* If they do, increment a counter to record the match. */
    /* If they do, print out which digit matched. */
    }
    [/code]

    The above is a rough outline of the routine to print out how many and which digits match. Again, I don't want to spell everything out because that's no fun...

    I hope this helps.

    Kreitler

    : Hi Kreitler,
    : Thanks for your help first. I know i can do it this way. But I need to get the input like this.
    : Iuput number e.g. 1234 as a string, not one by one. And how do i compare it whith the computer secret code and print the message that how many digits are correct and in the right position; and how many are in the wrong position.
    :
    :
    : : Hi again, Jack,
    : : You have quite a few options open to you in C++.
    : :
    : : First, you can use the input stream operator, '>>'. This is sort of the 'C++ standard' for reading input, but it tends to make your programs big. A quick search on google will show you lots of examples, but I think this will work:
    : : char n1, n2, n3, n4;
    : : cin >> n1 >> n2 >> n3 >> n4;
    : :
    : : You can also use getChar() to read one character at a time from the keyboard. Something like this:
    : :
    : : char n1, n2, n3, n4;
    : : n1 = getChar();
    : : n2 = getChar();
    : : n3 = getChar();
    : : n4 = getChar();
    : :
    : : would read 4 characters from the keyboard and store them in the variables n1-n4.
    : :
    : : There are other functions and macros you can use, like fgetc(), getc(), gets() and fgets(), but the two methods shown above are probably the most straightforward.
    : :
    : : I hope this helps.
    : :
    : : Kreitler
    : :
    : : : I'm using C++. Hope anybody can help me.
    : : :
    : : : : Hi Jack,
    : : : : What language are you using for your game?
    : : : :
    : : : : Kreitler
    : : : :
    : : : : : I'm doing my project which contain a game called Code Breaker. It is a game that the computer generates a four digits code and the user to guess/break the code. Now I'm facing a problem of how to get the user's input code and compare with the secret code. Can anybody help me?
    : : : : : Thanks!
    : : : : :
    : : : : : Jack
    : : : : :
    : : : :
    : : : :
    : : :
    : : :
    : :
    : :
    :
    :

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories