how can i stop having newline attached to input string?

This is a discussion on how can i stop having newline attached to input string? within the C Programming forums, part of the General Programming Boards category; In the code below, I have to first take the string, and remove the newline value by setting input[length-1]=0; Is ...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    78

    how can i stop having newline attached to input string?

    In the code below, I have to first take the string, and remove the newline value by setting input[length-1]=0; Is there a simpler way to do this?

    Code:
    void input(){
    
    
    int continu=1;
    char input[256];
    int inlength; 
    
    	while (continu==1){
    
    		printf("\n\nEnter Command <h=help>: ");
    		fgets(input, 25, stdin);
    		inlength=strlen(input);
    		input[inlength-1]=0;
    		
    		if (strcmp(input, "x")==0){continu=0;}
    
    	}
    
    
    
    }

  2. #2
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    975
    This method is not safe: what if the last character is not a newline? Or even worse, what if no input at all is read?

    Better is to find the newline with strchr() and, if it exists, set it to 0. As to your question, I suppose you could write a wrapper around fgets() that always removes a newline if it exists. fgets() itself cannot be told to do this.

  3. #3

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    78
    Ok, thanks, I changed it to the following. Is there any advantage to using '\0' instead of just plain 0?

    Code:
    char input[BUFSIZ];
    char *p;
    
    ...
    
    printf("\n\nEnter Command <h=help>: ");
    		fgets(input, sizeof(input), stdin);
        			if ((p = strchr(input, '\n')) != NULL)
          			*p = '\0';
    -Adam Rinkleff

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    The '\0' is not zero but actually an escape character. Wiki NUL character.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,431
    Quote Originally Posted by Adam Rinkleff
    Is there any advantage to using '\0' instead of just plain 0?
    Readability since it hints that you expect to be assigning to a char instead of some other numeric or pointer type.

    By the way, you should improve your indentation, e.g.,
    Code:
    printf("\n\nEnter Command <h=help>: ");
    fgets(input, sizeof(input), stdin);
    if ((p = strchr(input, '\n')) != NULL)
        *p = '\0';
    Quote Originally Posted by AndrewHunter
    The '\0' is not zero but actually an escape character.
    That depends on your point of view: since '\0' has a value of zero, and in fact is of type int, one could argue that it is zero.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by laserlight View Post
    .....
    That depends on your point of view: since '\0' has a value of zero, and in fact is of type int, one could argue that it is zero.
    I guess I do have a differing point of view here but I do see what you are saying.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accept a string with newline
    By perrrson in forum C Programming
    Replies: 2
    Last Post: 10-22-2010, 01:16 PM
  2. Scanf sometimes do not stop for input
    By kapil1089thekin in forum C Programming
    Replies: 1
    Last Post: 10-03-2010, 07:25 AM
  3. Stop from bypassing string input?
    By Djanvk in forum C++ Programming
    Replies: 2
    Last Post: 08-21-2008, 03:32 AM
  4. how to remove newline from a string
    By rupurt in forum C Programming
    Replies: 8
    Last Post: 10-01-2005, 06:01 PM
  5. stop read in newline
    By Supra in forum C Programming
    Replies: 1
    Last Post: 03-04-2002, 09:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21