1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Need minor help

Discussion in 'C' started by TheRookie, Feb 10, 2013.

  1. TheRookie

    TheRookie New Member

    Joined:
    Feb 10, 2013
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hey guys so i was tasked to write a program that will ask the user to input a set of basic informations and the user will be given 3 choices to exit, to input and to display all inputted information using array of structures

    my code is already done but the problem is that whenever i input 1 so that i can input, the name would always be skipped
    Code:
    #include <stdio.h>
    struct info{
    	char name[50];
    	char birth[50];
    	char address[50];
    	char gender[50];
    }infos[10];
    
    main(){
    	struct info student;
    	int i=0, choice;
    	printf("0 - exit");
    	printf("\n1 - add entry");
    	printf("\n2 - view all entry");
    	printf("\n\nEnter your choice:");
    	scanf("%d", &choice);
    	switch(choice){
        case 0:
             system("cls");
             printf("\nThank you");
             system("pause");
             break;
        case 1:
             system("cls");
             for(i=i;i<i+1;){
             printf("Provide the following:\n");
             
    	     printf("\nName:");
    	     gets(infos[i].name);
    	
             printf("\n\nDay of Birth:");
    	     gets(infos[i].birth);
    
    	     printf("\nAddress:");
    	     gets(infos[i].address);
    	     
             printf("\nGender:");
    	     scanf("%s", &infos[i].gender);
    	     system("cls");
    	     return main();
    	     break;
          }
          case 2:
             system("cls");
             for(i=0;i<10;i++){
             printf("Name: %s", infos[i].name);
    	     printf("\nBirthday: %s", infos[i].birth);
    	     printf("\nAddress: %s", infos[i].address);     
    	     printf("\nGender: %s\n\n", infos[i].gender);    
             return main();         
             break;
          }
          default:
             printf("\n\nChoose again\n");
             return main();
             break;
                        
                       }
    
    	getch();
    	return;
    }
    
     
  2. hobbyist

    hobbyist New Member

    Joined:
    Jan 7, 2012
    Messages:
    141
    Likes Received:
    0
    Trophy Points:
    0
    The first thing you should scrap is calling main() within main(). The next thing is using gets(). That function doesn't check that you've got enough space in your input buffer to accommodate what's been entered. If the input exceeds the size of your buffer, then you're writing to out of bounds memory.

    In regards to skipping over the name field, you've got a call to scanf and a subsequent call to gets. When mixing input functions, scanf can leave the new line char in the buffer, and gets may be picking it up. Clear the keyboard buffer after the scanf call.

    Code:
    #define MAX_INFO 10
    
    typedef struct {
       char name[50];
       // other fields
    } info;
    
    int main(void) {
    
       info infos[MAX_INFO];
       // other vars
    
       do {
    
           // show and prompt for menu choice
           scanf("%d", &choice);
           while(getchar() != '\n'){} // eat keyboard buffer until newline
    
           switch(choice) {
               case 1 : for(i=0; i < MAX_INFO; ++i) {
    
                            printf("Enter name: ");
                            fgets(infos[i].name, sizeof infos[i].name, stdin);
    
                            // other fields
                         }
    
                         break;
               // other cases
            }
    
        } while(choice != 0);
    
        // other code
        return 0;
    }
    One thing to be aware of is fgets will store the newline if it will fit in your buffer. To remove it, you can use something like below either during the storing of data or when you're outputting the data.

    Code:
    char *p;
    
    if((p = strchr(infos[i].name, '\n')) != NULL) *p = 0;
    printf("Name: %s", infos[i].name);
    
    That would apply to all of the char arrays where fgets is used.
     
  3. rehan112233

    rehan112233 New Member

    Joined:
    Apr 12, 2013
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    dear this is not accept the string but when you type flushall between the this two line like

    OLD:-----
    printf("\nName:");
    gets(infos.name);

    NEW:-----
    printf("\nName:");
    flushall();
    gets(infos.name);
    you try it and send me your feedback on my this e-mail id,
    abdul.qadir390786@gmail.com
     

Share This Page