Thread: subsequent cods fails / doesn't fail depending on the length of lest input

  1. #1

    subsequent cods fails / doesn't fail depending on the length of lest input

    locate(5,8);char codeIn[10]; printf("Code");locate(30,8);printf(": ");
                fgets(codeIn, sizeof(codeIn),stdin);
                if ( (p = strchr(codeIn,'\n')) != NULL ) *p = '\0';
                strcpy(B.bill, billIn);    
                strcpy(, dateIn);    
                    strcpy(B.descr, descrIn);    
                    strcpy(B.euros, eurosIn);
                strcpy(B.code, codeIn);
                FILE *C;
                C = fopen("data2","ab");
                fwrite(&B,sizeof(struct A),1,C);
                locate(6,23);printf("%s %s %s %s %s",billIn, dateIn, descrIn, eurosIn, codeIn);
                locate(6,24);printf("The above has been saved to disk");
                int c;
                printf("Continue y / n : ");
                c = getchar();
                if (c == 'y' || c == 'Y') 
                                                                     //  do something
    Depening on the amount of characters during the last data keyed in, the program will skip over the request to enter Y (or N). What to do? Zach K.
    Last edited by zach; 08-30-2019 at 03:50 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Well you have really small buffers for fgets().

    So any enthusiastic typing will cause problems.
    if ( (p = strchr(codeIn,'\n')) != NULL ) {
      *p = '\0';
    } else {
      int ch;
      // burn the rest of the line
      while ( (ch=getchar()) != EOF && ch != '\n' );
    Similar for your "Continue y / n" thing.
    You need to make sure your input reaches a consistent state, not leave a bunch of garbage and a newline to trip something else up.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2019
    To not worry about line size:
    char *lineptr;
    size_t line_size;
    lineptr = NULL;  // NULL means "allocate" getline().
    line_size = 0;   // 0 means "allocate" for getline().
    if ( getline( &lineptr, &line_size, fstream ) == -1 )
      /* error handling */
    /* Strip \r or \n chars at the end. */
    { char *p; if ( p = strchr( lineptr, "\r\n" ) ) *p = '\0'; }
    /* Here lineptr points to newly allocated line. Must free() later. */
    getline() is in POSIX.1-2008 standard.

  4. #4
    null pointer Structure's Avatar
    Join Date
    May 2019


    the program will skip over the request to enter Y (or N)
    Say what now ?

    Can i suggest not using C and c variables ?
    "without goto we would be wtf'd"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 04-23-2019, 09:05 AM
  2. Subsequent function calls increase length of return string!
    By SqueezyPeas in forum C Programming
    Replies: 8
    Last Post: 10-30-2015, 10:17 AM
  3. Replies: 1
    Last Post: 02-02-2013, 05:33 AM
  4. Depending on length of name, output changes?
    By Hp7130p in forum C++ Programming
    Replies: 8
    Last Post: 04-21-2005, 07:21 PM
  5. Still can't print a Histogram from fail input
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 07-11-2002, 12:24 PM

Tags for this Thread