Storing char

Jan 12, 2015 at 5:24am
Hi, how you store a string of char in c using stdio.h and conio.h only.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int j;
char input[26];
printf("Please enter your name: ");
do
{
input[j] = getchar();
if (input[j] == '*' || input[j] == "#")
{
printf("Name should not include * and #. Try again.");
j = 0;
continue;
}
j++;
} while (input[j] < 25 && input[j] != '\n');
input[j] = 0;
puts("Your name is");
puts(input);
}

From the example above, the program must be able to store each char and then display the name on the screen. The name must be less than 25 char and must not include * and #. From the code I used above, I cant seem to display the name on the screen. Any help is appreciated. Thanks!
Jan 12, 2015 at 5:40am
input[j] = 0; you are trying to store an integer in an array of char

puts(input); you cannot just print an array like that, you should loop it 1 by 1
Jan 12, 2015 at 5:55am
I thought input[j] = 0 is to append a NULL character to end of input.
Can you maybe show me an example of how you do it? Thanks.
Jan 12, 2015 at 7:07am
oh yes im wrong, i thought you were initializing it in '0'. sorry

for displaying the name
1
2
for ( int a = 0; a < j; a++ ) 
cout << input[a]; // i dont know how to printf it in C :P  
Last edited on Jan 12, 2015 at 7:07am
Jan 12, 2015 at 7:21am
In your code, j may be any value initially, so input[j]= getchar() is likely to write to memory you have no business accessing.
Jan 12, 2015 at 8:18am
I managed to solve it. Thanks for the help.
Topic archived. No new replies allowed.