Number, Letters and Space Only allowed.

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..

here is the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <ctype.h>

int main(){

	char *letter;
	char hold;
	int ctr = 0;
	char *holdLetter;
    letter = (char*)malloc(sizeof(char));
	holdLetter = letter;
	printf("Enter String Letter: ");
	do
    {
    hold = getch();
    if(isalpha(hold))
    {
                     *holdLetter = hold;
                     printf("%c", *holdLetter);
                     holdLetter++;
                     ctr++;
                     *holdLetter = 0;
                     }
                     else if(hold == 8 && ctr!= 0)
                          {
                            printf("\b \b");
                            ctr--;
                            holdLetter--;
                             }
                     }while(hold !=13);
                     *holdLetter = 0;
                     free(holdLetter);
                     
                     printf("\nLetters are : %s", letter);
                     
                     
                     
	getch();
	return 0;
}


any hints there sir??

can someone help me with space and number? T_T
Last edited on
true if hold is...
- A number:
hold>='0' && hold<='9'
- A letter:
(hold>='a' && hold<='z') || (hold>='A' && hold<='Z')
-space
hold==' '

together:
(hold>='0' && hold<='9') || (hold>='a' && hold<='z') || (hold>='A' && hold<='Z') || hold==' '
Last edited on
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 should cast the char to an int and specify what ascii intervals that should be allowed for the char or use strings.

May I ask what the purpose of your program is?
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.

Regards
sir im using c++

compiler: dev c++ in exact

@JoR

thanks for the tip..
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 new char[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].

Regards
Topic archived. No new replies allowed.