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

How to capture a NULL?

Discussion in 'C' started by askmewhy25, Mar 14, 2010.

  1. askmewhy25

    askmewhy25 New Member

    Joined:
    Jan 24, 2010
    Messages:
    49
    Likes Received:
    0
    Trophy Points:
    0
    I don't know how to capture a NULL because I am using the gets function in order to get the strings but the problem is that when I press the ENTER BUTTON it goes on and on..How can i capture if there is no input or the input is a NULL

    Here is the part in my program that makes me problematic..

    Code:
    ................
    ................
              printf("First Name: ");
              gets(info[tCnt].fname);
              printf("Middle Name: ");
              gets(info[tCnt].mname);
              printf("Last Name: ");
              gets(info[tCnt].lname);
              printf("ID Number: ");
              gets(info[tCnt].id);
              printf("Birthday: ");
              gets(info[tCnt].bday);
    ................
    ................
    
    My full program
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <math.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef char SData[128];
    
    struct Data{
      SData fname,mname,lname,bday,id;
    };
    
    typedef Data NData[10];
    
    //Function to Display All Entries
    void DisplayAll(int size,NData info){
      int i,j;
      for(i=0;i<size;i++){
        system("cls");
        printf("Full Name: %s %s %s\n",info[i].fname,info[i].mname,info[i].lname);
        printf("ID Number: %s\n",info[i].id);
        printf("Birthday: %s\n",info[i].bday);
        printf("Please press the spacebar to continue!!\n");
        getche();
        }
    }
    
    //Function to Search an Entry using ID Number
    void SearchIdNum(SData Input,NData info, int nCnt) {
      int i=0,loopext=0,j;
      do{
        if(strcmpi(info[i].id,Input)==0){
          system("cls");
          printf("Full Name: %s %s %s\n",info[i].fname,info[i].mname,info[i].lname);
          printf("ID Number: %s\n",info[i].id);
          printf("Birthday: %s\n",info[i].bday);
          printf("Please press the spacebar to continue!!\n");
          getche();
          loopext=1;
          }
        else 
          i++;
      }while(i<nCnt&&loopext==0);
      if(i>=nCnt)
        printf("ID Number does not exists\n");
        getche();
    }
    
    //Function to Search an Entry using Name
    void SearchName(SData Input,NData info,int nCnt,SData Input2,SData Input3){
      int i=0,loopext=0,j;
      do{
        if(strcmpi(info[i].fname,Input)==0&&strcmpi(info[i].mname,Input2)==0&&strcmpi(info[i].lname,Input3)==0){
          system("cls");
          printf("Name: %s %s %s\n",info[i].fname,info[i].mname,info[i].lname);
          printf("ID Number: %s\n",info[i].id);
          printf("Birthday: %s\n",info[i].bday);
          printf("Please press the spacebar to continue!!\n");
          getche();
          loopext=1;
          }
        else 
         i++;
      }while(i<nCnt&&loopext==0);
      if(i>=nCnt)
      printf("Name does not exists\n");
      getche();
    }
    
    //Function to Edit an Entry (Name)
    void Edit1(SData Input,NData info, int nCnt){
      char cDump;
      int i=0,j,loopext = 0;
      do{
        if(strcmpi(info[i].id,Input) == 0){
          system("cls");
          printf("ID Number: %s\n",info[i].id);
          printf("Birthday: %s\n",info[i].bday);
          printf("First Name: ");
          gets(info[i].fname);
          printf("Middle Name: ");
          gets(info[i].mname);
          printf("Last Name: ");
          gets(info[i].lname);
          getche();
          loopext = 1;
          }
        else 
          i++;
      }while(i<nCnt && loopext == 0);
      if(i>=nCnt)
      printf("ID Number does not exists\n");
    }
    
    //Function to Edit an Entry (ID Number)
    void Edit2(SData Input,NData info, int nCnt){
      char cDump;
      int i=0,j,loopext = 0;
      do{
        if(strcmpi(info[i].id,Input) == 0){
          system("cls");
          printf("Full Name: %s %s %s\n",info[i].fname,info[i].mname,info[i].lname);
          printf("Birthday: %s\n",info[i].bday);
          printf("ID Number: ");
          gets(info[i].id);
          getche();loopext = 1;
          }
        else 
          i++;
      }while(i<nCnt && loopext == 0);
      if(i>=nCnt)
      printf("ID Number does not exists\n");
    }
    
    //Function to Edit an Entry (Birthday)
    void Edit3(SData Input,NData info, int nCnt){
      char cDump;
      int i=0,j,loopext = 0;
      do{
        if(strcmpi(info[i].id,Input) == 0){
          system("cls");
          printf("Full Name: %s %s %s\n",info[i].fname,info[i].mname,info[i].lname);
          printf("ID Number: %s\n",info[i].id);
          printf("New Birthday: ");
          gets(info[i].bday);
          getche();loopext = 1;
          }
        else 
          i++;
      }while(i<nCnt && loopext == 0);
      if(i>=nCnt)
      printf("ID Number does not exists\n");
    }
    //Function to Remove an Entry
    void RemoveEntry(SData Input,NData info, int *nCnt){
      int j = 0,i; 
      int EntryFound = 1; 
      while(EntryFound!=0 && j <*nCnt){
        if(strcmpi(Input,info[j].id)== 0){
          EntryFound = 0; 
          printf("The Entry was searched, found and deleted.\n");
          getche();
          }
        else 
          j++;
      }
      if(j<*nCnt){
        for(i = j; i<*nCnt; i++){
          system("cls");
          strcpy(info[i].fname,info[i+1].fname);
          strcpy(info[i].mname,info[i+1].mname);
          strcpy(info[i].lname,info[i+1].lname);
          strcpy(info[i].id,info[i+1].id);
          strcpy(info[i].bday,info[i+1].bday);
          }
        *nCnt-=1;
      }
      else 
        printf("Entry does not exists\n");
        getche();
    }
    
    main(){
      NData info;
      SData input,input1,input2,input3;
      int selection,QA=0,nCnt=0,tCnt=0,ed;
      char error[128];
      
      do{
        system("cls");
        printf("Database Selection Screen\n\n");
        printf("1 - Add  an Entry\n");
        printf("2 - Remove an Entry\n");
        printf("3 - Edit an Entry\n");
        printf("4 - Search Entry by Name\n");
        printf("5 - Search Entry by ID Number\n");
        printf("6 - Display All Entries\n");
        printf("7 - Exit\n");
        fgets(error,128,stdin);
        selection=atoi(error);
        switch(selection){
        case 1://Add an Entry
          if(nCnt <=10){
            tCnt = nCnt;
            do{
              system("cls");
              printf("First Name: ");
              gets(info[tCnt].fname);
              printf("Middle Name: ");
              gets(info[tCnt].mname);
              printf("Last Name: ");
              gets(info[tCnt].lname);
              printf("ID Number: ");
              gets(info[tCnt].id);
              printf("Birthday: ");
              gets(info[tCnt].bday);
              tCnt++;
              do{
                system("cls");
                printf("Do you want to add another Entry?\n\n");
                printf("1 - Yes\n");
                printf("2 - No\n");
                fgets(error,128,stdin);
                QA=atoi(error);
                switch(QA){
                  case 1:
                    break;
                  case 2:
                    break;
                  default:
                    system("cls");
                    printf("Invalid input. Please try again!!!\n");
                    printf("Please press the spacebar to continue!!");
                    getche();
                    break;   
                    }
                  }while(QA>2||QA<1);
                }while(QA!=2 &&tCnt<10);
              nCnt=tCnt;
            }
          else
            printf("The Database is Maxed Out\n");
          break;
        case 2://Remove an Entry
          system("cls");
          printf("\nInput the ID Number: ");
          gets(input);
          RemoveEntry(input,info,&nCnt);
          break;
        case 3://Edit an Entry
          do{
            system("cls");
            printf("Edit an Entry\n\n");
            printf("1 - Name\n");
            printf("2 - ID Number\n");
            printf("3 - Birthday\n");
            fgets(error,128,stdin);
            ed=atoi(error);
            switch(ed){
              case 1:
                system("cls");
                printf("Input the ID Number: ");
                gets(input);
                Edit1(input,info,nCnt);
                break;
              case 2:
                system("cls");
                printf("Input the ID Number: ");
                gets(input);
                Edit2(input,info,nCnt);
                break;
              case 3:
                system("cls");
                printf("Input the ID Number: ");
                gets(input);
                Edit3(input,info,nCnt);
                break;
              default:
                system("cls");
                printf("Invalid input. Please try again!!!\n");
                printf("Please press the spacebar to continue!!");
                getche();
                break;
              }
          }while(ed>3||ed<1);
          break;
        case 4://Search an Entry using Name
          system("cls");
          printf("Input the First Name: ");
          gets(input1);
          printf("Input the Middle Name: ");
          gets(input2);
          printf("Input the Last Name: ");
          gets(input3);
          SearchName(input1,info,nCnt,input2,input3);
          break;
        case 5://Search an Entry using ID Number
          system("cls");
          printf("Input the ID Number: ");
          gets(input);
          SearchIdNum(input,info,nCnt);
          break;
        case 6://Display all Entries
          DisplayAll(nCnt,info);
          break;
        case 7://Exit
          exit(1);
          break;
        default://Program redirects invalid input
          system("cls");
          printf("Invalid input. Please try again!!!\n");
          printf("Please press the spacebar to continue!!");
          getche();
          break;
        }
      }while(selection!=7||QA==2);
    }
    
    
     
    Last edited: Mar 14, 2010
  2. karthigayan

    karthigayan New Member

    Joined:
    Feb 19, 2010
    Messages:
    33
    Likes Received:
    0
    Trophy Points:
    0
    You can check the null by checking the input using the strcmp function.

    Code:
    #include<stdio.h>
    #include<string.h>
    main()
    {
            char str[100];
            printf("Enter the value : ");
            gets(str);
            if(strcmp(str,"\0")==0)
            {
                    printf("Null entered \n");
            }
            else
            {
            printf("%s",str);
            }
    
    }
    
    Try to avoid gets,instead you can use fgets .

    The same requirement using fgets is,

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    main()
    {
            char str[100];
            printf("Enter the value : ");
            if(fgets (str,sizeof(str),stdin)==NULL)
            {
                    printf("fgets : read error \n");
                    exit(1);
            }
            if(strcmp(str,"\n")==0) // Because fgets store the '\n'.
            {
                    printf("Null entered \n");
            }
            else
            {
            printf("%s",str);
            }
    
    }
    
    cheers
     
    Last edited: Mar 15, 2010

Share This Page