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;
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.
add x to y.
Now, the thing is you need to make sure you have y at some sensible value.
i solved that way... using a char array... but something is not good why?
i think there r problem in the passage of the array to the function generateCode:#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]); } }
Last edited by xphoenix; 06-29-2010 at 10:02 AM.
solved!!! the last for is bugging, without it it works
printf("Generated code is : %s\n",code);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; }
here crash...
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
Now, do this in startGameCode: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++; } }
you pass code and its filled up in generate. You allocate the memory outside generate so code will last until startGame returns.Code:char code[sizeMax]; ... generate(symbols, code);
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
NOTE2!: I see you have thisCode:char code[sizeMax+1]; ... generate(symbols, code); char code[sizeMax] = '\0'; //note that this is the last position in the array
So you will eventually do code[sizeMax] = ...Code:char code[sizeMax]; while(cont <= sizeMax) { code[cont] = symbols[randomNum]; cont++; }
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!
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++; } }
Why create cod1?
You are usingCode: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++; } }
That is another way, but you need to change %s to %c to print a single char.Code:for(i=0;i<=sizeMax;i++){ printf("code: %s\n",code[i]);} }
Note also about the length. You need to do char[maxSize+1] to keep your code.
You should read more about c-strings.
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
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 ofSo, why are you making this so difficult? What are your actual requirements?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; }
lool i know, but dunno how to change the title of the thread
May I ask, why do you need ASCII representation of 1, 2, 3, and 4? They're all invisible characters.
just a curiosity ))
can u help me with the problem? it shows no char
but i have not used the ascii... look the code