hey guys.. i just want your help.. because i am making a program that has only numbers, letters, and space should be allowed.. i dont know the syntax for the space and for the numbers >.<
i already made this program but only characters and backspace allowed..
I should add that you're doing something weird with dynamic memory there. So weird that I can't event tell what it should be. Are you trying to make a string out of correct chars? Use realloc for that. Now you just allocate a single byte once, then do stuff with it then set it's pointer to 0 and then free that 0. Static memory is fine if that's all you want..
I guess you are programming in C, not C++. Not that it matters. Here is a reference that you can use: http://www.cplusplus.com/reference/clibrary/cctype/ (Consult it for the ctype.h functions - it was renamed to cctype in C++) What you need is the alphanumeric and space characters - isalnum in disjunction (logical or) with isspace.
I understand, but I meant something else. The header files you are using are deprecated and the I/O is C-style. This code would probably compile as C program. That is not important. Just saying.
okay sir.. isalnum and isspace worked. but the problem is, i want to put another one.. like name address age.. can you tell me how to do that? any tips?
i want to put another one.. like name address age.. can you tell me how to do that? any tips?
Sorry, I did not get the idea. Add another validating condition for the string or have more than one string entered?
Also, you have not allocated enough space for storing an entire string. malloc(sizeof(char)) allocates just space for one character. In fact, as far as strings are concerned not even that, because you need one character for the terminating 0. (which I see is appended after the loop in your code.) If you want to use malloc, then you should specify a bigger number of characters, like malloc(SOME_BIGGER_SIZE). Also, in C++ it is preferable to use newchar[SOME_BIGGER_SIZE] and delete[] letter instead of malloc and free respectively. It won't change anything in this case, but it is generally advisable to have consistency. Also, if you choose so, you don't have to dynamically allocate memory at all. You could simply have letter[SOME_BIGGER_SIZE].