Thread: int to char

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    46

    Mastermind

    Hi i have a INT X that can be random value between 1 and 4
    how can i obtain a CHAR Y that is the ascii value of that int?

    int x = 1+ rand() %4;
    char y;
    Last edited by xphoenix; 06-29-2010 at 10:42 AM.

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    add x to y.

    Now, the thing is you need to make sure you have y at some sensible value.

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    46
    i solved that way... using a char array... but something is not good why?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void menu();
    void startGame(char gs[]); 
    void generate(int sizeMax,char symbols[]);
    
    int main() {
        
        char symbols[] = "abcdefghijk"; /* symbols that is used for generate the code*/
        int sizeMax = 4; /* max lenght of the code */
    
        generate(sizeMax,symbols); 
    
        system("PAUSE");    	
        return 0;
    }
    
    void startGame(char gs[]){
        gs = "ciao";
        printf("code : %s\n",gs);
    }
    
    void menu(){
        printf("--MASTERMIND cmd version by K--\n");
    }
    
    void generate(int sizeMax, char symbols[]){
    
        char code[sizeMax];   
        int cont = 0;
        while(cont <= sizeMax) {
            int randomNum = rand() %4;    
            code[cont] = symbols[randomNum];
            cont++;
        }
        int i;
        for(i=0;i<sizeMax;i++){
          printf("elem. %s = %s\n", i, code[i]);
        }
    }
    i think there r problem in the passage of the array to the function generate
    Last edited by xphoenix; 06-29-2010 at 10:02 AM.

  4. #4
    Registered User
    Join Date
    Jun 2010
    Posts
    46
    solved!!! the last for is bugging, without it it works

  5. #5
    Registered User
    Join Date
    Jun 2010
    Posts
    46
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    void menu();
    void startGame(int sizeMax,char symbols[]); 
    char generate(int sizeMax,char symbols[]);
    
    int main() {
        
        char symbols[] = "abcdefghijk"; /* symbols that is used for generate the code*/
        int sizeMax = 4; /* max lenght of the code */
    
        menu();
        startGame(sizeMax,symbols); 
    
        system("PAUSE");    	
        return 0;
    }
    
    void startGame(int sizeMax,char symbols[]){
        char code = generate(sizeMax,symbols);
        printf("\nTry to guess the secret code\n");
        printf("the code is made by using these symbols: %s\n",symbols); 
        printf("Generated code is : %s\n",code);
        }
    
    void menu(){
        printf("--MASTERMIND cmd version by K--\n");
    }
    
    /* generate the code */
    char generate(int sizeMax, char symbols[]){
        int allowedSymbols = 4;    
        char code[sizeMax];   
        int cont = 0;
        srand(time(NULL));
        while(cont <= sizeMax) {
            int randomNum = rand() % allowedSymbols; /* modify the number to chose how many symbols allow" */   
            code[cont] = symbols[randomNum];
            cont++;
        }
        return code;
    }
    printf("Generated code is : %s\n",code);
    here crash...

  6. #6
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Two flaws
    1) code is a char[] not a char. It is a string not a simple char. When trying to printf with %s it tries to print a string. And it crashes
    2) You cannot return a char[] that you statically allocated. Because what you allocate in a function is lost when the function returns/finishes. You have to dynamically allocate and return a pointer. But do something like this instead
    Code:
    void generate(char symbols[], char code[]){
        int allowedSymbols = 4;    
        int cont = 0;
        srand(time(NULL));
        while(cont <= sizeMax) {
            int randomNum = rand() % allowedSymbols; /* modify the number to chose how many symbols allow" */   
            code[cont] = symbols[randomNum];
            cont++;
        }
    }
    Now, do this in startGame
    Code:
    char code[sizeMax];
    ...
    generate(symbols, code);
    you pass code and its filled up in generate. You allocate the memory outside generate so code will last until startGame returns.
    You also have the correct type, char code is an array of char not a single char.

    NOTE: There is one final thing you need to know about strings. They are null-terminated. Every string has to have a '\0' at the end otherwise printf will crash again. You have to manually add one, like this for example
    Code:
    char code[sizeMax+1];
    ...
    generate(symbols, code);
    char code[sizeMax] = '\0'; //note that this is the last position in the array
    NOTE2!: I see you have this
    Code:
    char code[sizeMax];
    while(cont <= sizeMax) {  
            code[cont] = symbols[randomNum];
            cont++;
        }
    So you will eventually do code[sizeMax] = ...
    This is wrong. The arrays are zero based which means that the last element is code[sizMax-1]. So you need to do char code[sizeMax+1] as shown above. And probably code[sizeMax+2] to insert the '\0' in the end. Take care of the sizes of arrays!

  7. #7
    Registered User
    Join Date
    Jun 2010
    Posts
    46
    ty i modified that way, no crash but the code results null

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    void menu();
    void startGame(int sizeMax,char symbols[]); 
    void generate(int sizeMax,char symbols[],char code[]);
    
    int main() {
        
        char symbols[] = "abcdefghijk"; /* symbols that is used for generate the code*/
        int sizeMax = 4; /* max lenght of the code */
    
        menu();
        startGame(sizeMax,symbols); 
    
        system("PAUSE");    	
        return 0;
    }
    
    void startGame(int sizeMax,char symbols[]){
        char code[sizeMax];
        generate(sizeMax,symbols,code);
        printf("\nTry to guess the secret code\n");
        printf("the code is made by using these symbols: %s\n",symbols); 
        int i;
        for(i=0;i<=sizeMax;i++){ printf("code: %s\n",code[i]);}
        }
    
    void menu(){
        printf("--MASTERMIND cmd version by K--\n");
    }
    
    /* generate the code */
    void generate(int sizeMax, char symbols[], char code[]){
        int allowedSymbols = 4;    
        char code1[sizeMax];   
        int cont = 0;
        srand(time(NULL));
        while(cont <= sizeMax) {
            int randomNum = rand() % allowedSymbols; /* modify the number to chose how many symbols allow" */   
            code1[cont] = symbols[randomNum];
            cont++;
        }
    }

  8. #8
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Why create cod1?
    Code:
    void generate(int sizeMax, char symbols[], char code[]){
        int allowedSymbols = 4;    
        char code1[sizeMax];   
        int cont = 0;
        srand(time(NULL));
        while(cont <= sizeMax) {
            int randomNum = rand() % allowedSymbols; /* modify the number to chose how many symbols allow" */   
            code1[cont] = symbols[randomNum];
            cont++;
        }
    }
    You are using
    Code:
        for(i=0;i<=sizeMax;i++){ printf("code: %s\n",code[i]);}
        }
    That is another way, but you need to change %s to %c to print a single char.

    Note also about the length. You need to do char[maxSize+1] to keep your code.

    You should read more about c-strings.

  9. #9
    Registered User
    Join Date
    Jun 2010
    Posts
    46
    yeah i know i'm started 2 o 3 days ago with c

    i put code1 cause with code i had error in compiling...

    and if i put %c i got no result

  10. #10
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    I'm so confused. Your initial question was not what your code does. An answer to your initial question would be something along the lines of
    Code:
    int main(void)
    {
            char y = '0';
            int x = 1+ rand() %4;
    
            y += x;
            printf("Value of x = %i,  char of x = %c\n", x, y);
            return 0;
    }
    So, why are you making this so difficult? What are your actual requirements?

  11. #11
    Registered User
    Join Date
    Jun 2010
    Posts
    46
    lool i know, but dunno how to change the title of the thread

  12. #12
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    May I ask, why do you need ASCII representation of 1, 2, 3, and 4? They're all invisible characters.

  13. #13
    Registered User
    Join Date
    Jun 2010
    Posts
    46
    just a curiosity ))

    can u help me with the problem? it shows no char

  14. #14
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Quote Originally Posted by xphoenix View Post
    just a curiosity ))

    can u help me with the problem? it shows no char
    The ASCII representation of 1, 2, 3, and 4 are invisible, like I just said. You shouldn't see anything on stdout.

  15. #15
    Registered User
    Join Date
    Jun 2010
    Posts
    46
    but i have not used the ascii... look the code

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  3. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  4. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 04:24 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM