Entry and output Problem

This is a discussion on Entry and output Problem within the C Programming forums, part of the General Programming Boards category; Hi! I have a question about showing my input. First , I have to ask user how many names want ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    9

    Exclamation Entry and output Problem

    Hi! I have a question about showing my input.
    First , I have to ask user how many names want to input
    i need to use a (dynamic) array to get all the name
    then, show it

    However, the program didnt show the name after i input the name

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    #define merror()   {printf("memory allocation problem\n");exit(1);}
    
    int main(void)
    {
    	char *buffer;
    	char **buf;
    	int num;
    	int i, j;
    
    	printf(" how many names will be entered : \n");
    	fflush(stdout);
    	fgets ( buffer, 10,  stdin );
        num = atoi (buffer);
    
        if (num <=0)
        {
           printf("empty input \n");
        }
        else if (num >10)
       {
           printf("input too long \n");
        }
         else if (num >0 && num <= 10)
        {
            printf("you will enter %d names \n", num);
        }
    
    	for(i=0; i < num; i++)
    	{
    		for(j=0; j<21; j++)
    		{
    		      buf[j]=fgetc(stdin);
    		      if (buf[j]=='\n') {
    		        buf[j]='\0';
    		        break;   /* break j-loop */
    		      }
    		    }/* end j-loop */
    
    		    if (j==21)
    		    {
    		      printf("input too long\n");
    		      while(fgetc(stdin)!='\n');
    		      continue;   /* continue i-loop */
                                         }
       }
    
        printf("\nunsorted input:\n");
        for(i=0; i< num; i++)
        printf("%s\n", buf[i]);
    
    	return 0;
    }

  2. #2
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Code:
    	char *buffer;
    	char **buf;
    You should allocate memory for those buffers as you state.
    With your "hard-coded" sizes, buffer should be allocated to 10 bytes, buf to 'num' pointers to char. and every buf[i] to 21 bytes.
    Do you understand how to perform memory allocation using the malloc function?
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble writing to file using fwrite()
    By yougene in forum C Programming
    Replies: 4
    Last Post: 12-30-2008, 04:13 PM
  2. 2d game
    By JordanCason in forum Game Programming
    Replies: 5
    Last Post: 12-08-2007, 09:08 PM
  3. Having trouble making a function do what I want.
    By Shamino in forum C++ Programming
    Replies: 9
    Last Post: 12-07-2007, 10:20 AM
  4. Sigmaze! -- Second Attempt.
    By quzah in forum Contests Board
    Replies: 42
    Last Post: 11-09-2004, 05:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21