Thread: Taking C for College, Side Project

  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    5

    Taking C for College, Side Project

    I've just started classes for taking C, and am trying to write a program that will print back the input for my first name, then tell me the character count, print back the input for my last name, then tell me the character count, then print both names out together on the same line with a space or tab between, then tell me the counts for both and the total count. My code is this so far:

    Code:
    #include <studio.h>
    #include <string.h>
    
    int main (void)
    {
       char id[20];
       char id2[20];
    
       printf("Enter your name: ");
       fgets(id, 20, stdin);
       printf("%s\n", id);
       printf("The number of letters entered is %d \n", strlen(id)-1);
       
       printf("%s\n", id2);
       fgets(id2, 20, stdin);
       printf("%s\n", id2);
       printf("The number of letters entered is %d \n", strlen(id2)-1);
       //From here I've tried numerous variations, but I'll leave the simplest one here
       printf("%s\t%s", id, id2);
       printf("The number of letters in first name is %d \n", strlen(id)-1);
       printf("The number of letters in last name is %d \n", strlen(id2)-1);
       printf("The number of letters in both names is %d \n", strlen(id) - 1 + strlen(id2) - 1);
    }
    Every time I enter in my last name and hit enter, it shows this vvv

    "First name"
    "Last name"

    The number of...

    No matter what I try, it always does a new line after first name before doing the tab before last name, I've tried removing all new line commands, all tab commands, etc. Nothing seemed to work, so I'm thinking the issue is that it's reading the enter key as part of the first name, and using that as a command to begin a new line before it moves on to the last name since it's still part of the first name... I could be wrong, and if so please tell me, but if I'm right, how would I go about removing the last character from the first name, (id), in the process of using printf("%s\t", id). ? I would really appreciate the assistance on this lol...

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    696
    fgets() inputs the entire line, including the trailing newline char.

    After using the fgets(), you need to remove the newline char. You can use the following code:
    Code:
      char *p = NULL;
      if ((p = strchr(id, '\n')) != NULL)
      {
          *p = '\0';
      }
    Other problems exist. Compare my version with yours:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main (void)
    {
       char id[20];
       char id2[20];
       char *p = NULL;
    
       printf("Enter your first name: ");
       fgets(id, 20, stdin);
       if ((p = strchr(id, '\n')) != NULL)
       {
          *p = '\0';
       }
    
       printf("%s\n", id);
       printf("The number of letters entered is %zu \n", strlen(id));
    
       printf("Enter your last name: ");
       fgets(id2, 20, stdin);
       if ((*p = strchr(id2, '\n')) != NULL)
       {
          *p = '\0';
       }
       printf("%s\n", id2);
       printf("The number of letters entered is %zu \n", strlen(id2));
       //From here I've tried numerous variations, but I'll leave the simplest one here
       printf("%s\t%s\n", id, id2);
       printf("The number of letters in first name is %zu \n", strlen(id));
       printf("The number of letters in last name is %zu \n", strlen(id2));
       printf("The number of letters in both names is %zu \n", strlen(id) + strlen(id2));
    }
    Last edited by rstanley; 10-02-2019 at 04:57 PM.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    3,747
    Get a line of text from the user/keyboard (C) - Cprogramming.com

    Code:
    /*
         *  Now test for, and remove that newline character
         */
        if ((p = strchr(buf, '\n')) != NULL)
          *p = '\0';
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    839
    alternatively just use fgetc:
    Code:
    int myfgets( FILE * file, char *dst, size_t size ) {
      size_t i = 0;
      int c;
      while ( !feof(file) ) {
        c = fgetc(file);
        if ( c == '\n' ) return 0;
        if ( i == size ) return ERANGE;
        dst[i++] = c;
      }
      return ENODATA;
    }
    Last edited by awsdert; 10-03-2019 at 04:59 PM. Reason: Forgot to increment i

  5. #5
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    701
    Quote Originally Posted by awsdert View Post
    alternatively just use fgetc:
    Code:
    int myfgets( FILE * file, char *dst, size_t size ) {
      size_t i = 0;
      int c;
      while ( !feof(file) ) {
        c = fgetc(file);
        if ( c == '\n' ) return 0;
        if ( i == size ) return ERANGE;
        dst[i++] = c;
      }
      return ENODATA;
    }
    That doesn't null-terminate the string.

    Also, Why it's bad to use feof() to control a loop.

  6. #6
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    839
    Well thx 4 the link, the example wasn't intended to b perfect since colleges will typically use a recent enough library and fs that that issue would not show, I'll still keep that in mind with my own code though

  7. #7
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    701
    Quote Originally Posted by awsdert View Post
    Well thx 4 the link, the example wasn't intended to b perfect since colleges will typically use a recent enough library and fs that that issue would not show, I'll still keep that in mind with my own code though
    It's not an issue of library version. It's how the C language works. It's bad to use feof() to control a loop because it's asking the wrong thing. feof() tells you that you have already tried to read past the end of the file. It doesn't tell you that the next read will leave you at the end of the file.

    It's just as easy to try reading a byte and breaking out if you're past the end of file:

    Code:
    int myfgets( FILE * file, char *dst, size_t size ) {
      size_t i = 0;
      int c;
      while ( (c = fgetc(file)) != EOF ) {
        if ( c == '\n' ) return 0;
        if ( i == size ) return ERANGE;
        dst[i++] = c;
      }
      return ENODATA;
    }
    That fixes the EOF issue but doesn't do anything about the unterminated string issue.

  8. #8
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    839
    Ooh ok I was planning on changing my loops anyways so good to know, your way made it much clearer thab the oversized post you linked too

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 02-03-2018, 05:13 PM
  2. Need help with a big project for college.
    By CammRobb in forum C++ Programming
    Replies: 1
    Last Post: 02-02-2012, 05:29 PM
  3. College project on arrays and functions
    By jthunder in forum C Programming
    Replies: 14
    Last Post: 12-04-2011, 11:13 PM
  4. help with while statement for college project
    By fsutati in forum C Programming
    Replies: 8
    Last Post: 10-24-2010, 08:47 PM
  5. need help with C++ project for college
    By dawiz66 in forum C++ Programming
    Replies: 6
    Last Post: 04-05-2002, 12:08 PM

Tags for this Thread