Errors in scanf and scanf_s

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • casybay
    New Member
    • Oct 2007
    • 35

    Errors in scanf and scanf_s

    Hi all,

    I am having a strange problem when I run the scanf. The following is my code:

    Code:
      int main(int argc, char** argv){
         
         int result;
         
         char* Y = (char*)malloc(sizeof(char)*MAXLENGTH);
         
         printf("Enter the target DNA sequence :");
         result = scanf_s("%c",&Y);
         if(result == EOF){
            printf("Read no character.\n");
            return 1;
         }
         
         char* X = (char*)malloc(sizeof(char)*MAXLENGTH);
         printf("\nEnter the query DNA sequence :");
         result = scanf_s("%c",&X);
         printf("\n");
         if(result == EOF){
            printf("Read no character.\n");
            return 1;
         }
         
         runSW(X,Y);
         CUT_EXIT(argc, argv);
    }
    The target DNA sequence can be entered. But the program stops as soon as I enter the sequence.
    Could anyone give me a hint why this happens?
    Thx.
  • oler1s
    Recognized Expert Contributor
    • Aug 2007
    • 671

    #2
    Explain to me the use of %c in your scanf call.

    Comment

    • MonaLisaO
      New Member
      • Mar 2008
      • 13

      #3
      Originally posted by casybay
      Hi all,

      I am having a strange problem when I run the scanf. The following is my code:

      Code:
        int main(int argc, char** argv){
           
           int result;
           
           char* Y = (char*)malloc(sizeof(char)*MAXLENGTH);
           
           printf("Enter the target DNA sequence :");
           result = scanf_s("%c",&Y);
           if(result == EOF){
              printf("Read no character.\n");
              return 1;
           }
           
           char* X = (char*)malloc(sizeof(char)*MAXLENGTH);
           printf("\nEnter the query DNA sequence :");
           result = scanf_s("%c",&X);
           printf("\n");
           if(result == EOF){
              printf("Read no character.\n");
              return 1;
           }
           
           runSW(X,Y);
           CUT_EXIT(argc, argv);
      }
      The target DNA sequence can be entered. But the program stops as soon as I enter the sequence.
      Could anyone give me a hint why this happens?
      Thx.
      Is your result really EOF? Is that why you are returning? What line does your execution stop on? Have you tried running this with a debugger attached?

      ~mona

      Comment

      Working...