Thread: Caracter input

  1. #16
    Registered User
    Join Date
    Nov 2008
    Posts
    49
    i'm confused....

    in the first one, secretCode is an integer (but it is initialized as char in main)
    and the second one has a char table[] for an argument...

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <time.h>
    
    #define NBCOLORS 4
    #define NBTRIES 6
    
    char readkey();
    void createCode(char code[]);
    int playGame(char code[]);
    
    int main (void)
    {
        char secretCode[NBCOLORS];
        createCode(secretCode);
        while(playGame(secretCode));
        return 0;
    }
    
    // Lecture des coleurs entrees par l'utilisateur
    
    char readkey() {
     //   fflush(stdin);           // Causait des problemes
        char c = '\n';
        while (c == '\n' || c == ' ')
            c = getchar();
        return c;
    }
    
    void createCode(char code[])
    {
        int i;
        srand(time(NULL));
    
        /* create random code */
        for (i = 0; i < NBCOLORS; i++)
           switch(rand() &#37; 4)
           {
               case 0: code[i] = 'r'; break;
               case 1: code[i] = 'b'; break;
               case 2: code[i] = 'v'; break;
               case 3: code[i] = 'j'; break;
           }  
    }
    
    int playGame(char code[]) {
        char codeCopy[NBCOLORS];
        int victory = 0;
        int i, j, k;
        char c = 0;                         /* character being read */
        char secretCode[NBCOLORS];
        createCode(secretCode);
            
        for (i = 0; i < NBTRIES; i++) {
            int identical = 0;
            int present = 0;
            
            for (j = 0; j < NBCOLORS; j++)
                codeCopy[j] = code[j];
            
            printf("Votre coup (essai no %i): ", i+1);
            
            for (j = 0; j < NBCOLORS; j++) {
                c = readkey();
                if (codeCopy[j] == c) {
                    codeCopy[j] = '#';
                    identical++;
                }
                else {
                    for(k = 0; k < NBCOLORS; k++)
                        if (codeCopy[k] == c) {
                            codeCopy[k] = '#';
                            present++;
                        }
                }
            }
            
            printf("Couleurs identiques: %i\n", identical);
            printf("Coleurs presentes: %i\n", present);
            
            if(identical == NBCOLORS) {
                printf("Bravo! Vous avez trouve notre sequence de couleurs\n");
                victory=1;
                break;
            }
        }
        
        if(!victory) {
            printf("Desole, la sequence de couleurs a deviner etait ");
            for (j = 0; j < NBCOLORS; j++)
                putchar(code[j]);
            putchar('\n');
        }
        
        while (c != 'o' && c != 'O' && c != 'n' && c != 'N') {
            fflush(stdin);
            printf("Voulez-vous jouer une autre partie? (O/N)?");
            c = readkey();
        }
        
        if (c == 'o' || c == 'O')
            return 1;
        return 0;
    }
    This code runs the program fine and the guessing part seems to be working ok.. but the code is not changing after selecting O.

  2. #17
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    In the first one, secretCode is most emphatically not an integer. Not not not. Not. secretCode is, in fact, a char[] (that is, an array of char). Notice however that the "char []" part is not passed to the function, since the function doesn't care. It just wants the array itself.

  3. #18
    Registered User
    Join Date
    Nov 2008
    Posts
    49
    i don't even know what we're talking about anymore. which ones are you referring to ?

  4. #19
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    why dont you save it to a struct like scanf the string u want and put it in a structure

  5. #20
    Registered User
    Join Date
    Nov 2008
    Posts
    49
    Quote Originally Posted by lolguy View Post
    why dont you save it to a struct like scanf the string u want and put it in a structure
    because it's not in the realm of this class :S

    i hate it when i just can't make sense of what's going on and get totally lost... i feel like scrapping the whole file because I just can't see what's the problem...

  6. #21
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by OrAnGeWorX View Post
    i don't even know what we're talking about anymore. which ones are you referring to ?
    The two lines I quoted earlier -- the two calls to createCode.

  7. #22
    Registered User
    Join Date
    Nov 2008
    Posts
    49
    this one:
    Code:
    int main (void)
    {
        char secretCode[NBCOLORS];
        createCode(secretCode);
        while(playGame(secretCode));
        return 0;
    }
    and this one
    Code:
    int playGame(char code[]) {
        char codeCopy[NBCOLORS];
        int victory = 0;
        int i, j, k;
        char c = 0;                         /* character being read */
        char secretCode[NBCOLORS];
        createCode(secretCode);
    yeah sry i had changed em to just secretCode


    even like so, it won't change the generate another secretCode
    Last edited by OrAnGeWorX; 12-13-2008 at 08:47 PM.

  8. #23
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Of course it will. You did change the body of the playGame function to use secretCode instead of code, did you not?

  9. #24
    Registered User
    Join Date
    Nov 2008
    Posts
    49
    Quote Originally Posted by tabstop View Post
    Of course it will. You did change the body of the playGame function to use secretCode instead of code, did you not?
    yes i have.. but I'm still getting the same color combination when i choose to replay.

  10. #25
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by OrAnGeWorX View Post
    yes i have.. but I'm still getting the same color combination when i choose to replay.
    Then you didn't change the body. Post what you have.

  11. #26
    Registered User
    Join Date
    Nov 2008
    Posts
    49
    here goes nothing

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <time.h>
    
    #define NBCOLORS 4
    #define NBTRIES 6
    
    char readkey();
    void createCode(char code[]);
    int playGame(char code[]);
    
    int main (void)
    {
        char secretCode[NBCOLORS];
        createCode(secretCode);
        while(playGame(secretCode));
        return 0;
    }
    
    // Lecture des coleurs entrees par l'utilisateur
    
    char readkey() {
     //   fflush(stdin);           // Causait des problemes
        char c = '\n';
        while (c == '\n' || c == ' ')
            c = getchar();
        return c;
    }
    
    void createCode(char code[])
    {
        int i;
        srand(time(NULL));
    
        /* create random code */
        for (i = 0; i < NBCOLORS; i++)
           switch(rand() &#37; 4)
           {
               case 0: code[i] = 'r'; break;
               case 1: code[i] = 'b'; break;
               case 2: code[i] = 'v'; break;
               case 3: code[i] = 'j'; break;
           }  
    }
    
    int playGame(char code[]) {
        char codeCopy[NBCOLORS];
        int victory = 0;
        int i, j, k;
        char c = 0;                         /* character being read */
        char secretCode[NBCOLORS];
        createCode(secretCode);
            
        for (i = 0; i < NBTRIES; i++) {
            int identical = 0;
            int present = 0;
            
            for (j = 0; j < NBCOLORS; j++)
                codeCopy[j] = code[j];
            
            printf("Votre coup (essai no %i): ", i+1);
            
            for (j = 0; j < NBCOLORS; j++) {
                c = readkey();
                if (codeCopy[j] == c) {
                    codeCopy[j] = '#';
                    identical++;
                }
                else {
                    for(k = 0; k < NBCOLORS; k++)
                        if (codeCopy[k] == c) {
                            codeCopy[k] = '#';
                            present++;
                        }
                }
            }
            
            printf("Couleurs identiques: %i\n", identical);
            printf("Coleurs presentes: %i\n", present);
            
            if(identical == NBCOLORS) {
                printf("Bravo! Vous avez trouve notre sequence de couleurs\n");
                victory=1;
                break;
            }
        }
        
        if(!victory) {
            printf("Desole, la sequence de couleurs a deviner etait ");
            for (j = 0; j < NBCOLORS; j++)
                putchar(code[j]);
            putchar('\n');
        }
        
        while (c != 'o' && c != 'O' && c != 'n' && c != 'N') {
            fflush(stdin);
            printf("Voulez-vous jouer une autre partie? (O/N)?");
            c = readkey();
        }
        
        if (c == 'o' || c == 'O')
            return 1;
        return 0;
    }

  12. #27
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by OrAnGeWorX View Post
    here goes nothing

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <time.h>
    
    #define NBCOLORS 4
    #define NBTRIES 6
    
    char readkey();
    void createCode(char code[]);
    int playGame(char code[]);
    
    int main (void)
    {
        char secretCode[NBCOLORS];
        createCode(secretCode);
        while(playGame(secretCode));
        return 0;
    }
    
    // Lecture des coleurs entrees par l'utilisateur
    
    char readkey() {
     //   fflush(stdin);           // Causait des problemes
        char c = '\n';
        while (c == '\n' || c == ' ')
            c = getchar();
        return c;
    }
    
    void createCode(char code[])
    {
        int i;
        srand(time(NULL));
    
        /* create random code */
        for (i = 0; i < NBCOLORS; i++)
           switch(rand() % 4)
           {
               case 0: code[i] = 'r'; break;
               case 1: code[i] = 'b'; break;
               case 2: code[i] = 'v'; break;
               case 3: code[i] = 'j'; break;
           }  
    }
    
    int playGame(char code[]) {
        char codeCopy[NBCOLORS];
        int victory = 0;
        int i, j, k;
        char c = 0;                         /* character being read */
        char secretCode[NBCOLORS];
        createCode(secretCode);
            
        for (i = 0; i < NBTRIES; i++) {
            int identical = 0;
            int present = 0;
            
            for (j = 0; j < NBCOLORS; j++)
                codeCopy[j] = code[j];  /*you told me you changed this to secretcode but you didn't */
            
            printf("Votre coup (essai no %i): ", i+1);
            
            for (j = 0; j < NBCOLORS; j++) {
                c = readkey();
                if (codeCopy[j] == c) {
                    codeCopy[j] = '#';
                    identical++;
                }
                else {
                    for(k = 0; k < NBCOLORS; k++)
                        if (codeCopy[k] == c) {
                            codeCopy[k] = '#';
                            present++;
                        }
                }
            }
            
            printf("Couleurs identiques: %i\n", identical);
            printf("Coleurs presentes: %i\n", present);
            
            if(identical == NBCOLORS) {
                printf("Bravo! Vous avez trouve notre sequence de couleurs\n");
                victory=1;
                break;
            }
        }
        
        if(!victory) {
            printf("Desole, la sequence de couleurs a deviner etait ");
            for (j = 0; j < NBCOLORS; j++)
                putchar(code[j]);
            putchar('\n');
        }
        
        while (c != 'o' && c != 'O' && c != 'n' && c != 'N') {
            fflush(stdin);
            printf("Voulez-vous jouer une autre partie? (O/N)?");
            c = readkey();
        }
        
        if (c == 'o' || c == 'O')
            return 1;
        return 0;
    }
    If you lie to us about what you changed, how can we help you?

  13. #28
    Registered User
    Join Date
    Nov 2008
    Posts
    49
    I'm certainly not lying but rather lost...

    I've changed the line you mentioned to codeCopy[j] = secretCode[j];
    now, even if u put in the correct color sequence, it doesn't accept it
    Last edited by OrAnGeWorX; 12-14-2008 at 12:15 PM. Reason: changed to secretCode

  14. #29
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by OrAnGeWorX View Post
    I'm certainly not lying but rather lost...
    I said:
    Quote Originally Posted by tabstop View Post
    Of course it will. You did change the body of the playGame function to use secretCode instead of code, did you not?
    Note that code is used one place in the body of the playGame function, namely here:
    Code:
     codeCopy[j] = code[j];
    You responded with:
    Quote Originally Posted by OrAnGeWorX View Post
    yes i have.. but I'm still getting the same color combination when i choose to replay.
    And yet, when you posted your code, the one place that there was to change, didn't change. What did you think you had to change, if not the one place in the code where the variable was used?

  15. #30
    Registered User
    Join Date
    Nov 2008
    Posts
    49
    i misunderstood what you meant, i'm sorry, i believed u meant something else... it's been a few days that i'm not getting sleep and i'm just staring at the pc screens and i have the final exam tomorrow. so i'm a little edgy and stressed out. your comments have been helpful in the general area but confusing me because i did not write the whole program, i had a friend help me out a little.
    the codeCopy table is there to compare the user's entry code with the generated secretCode (since codeCopy is the same as secretCode and we don't want modifications on the original)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. continues input
    By Matty_Alan in forum C Programming
    Replies: 2
    Last Post: 06-22-2007, 10:04 PM
  2. Input statement problem
    By une in forum C Programming
    Replies: 3
    Last Post: 05-29-2007, 11:16 PM
  3. For loop problems, input please.
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-22-2007, 03:54 AM
  4. Simple Console Input for Beginners
    By jlou in forum C++ Programming
    Replies: 0
    Last Post: 06-21-2005, 01:50 PM
  5. Help with Input Checking
    By Derek in forum C Programming
    Replies: 7
    Last Post: 06-17-2003, 03:07 AM