undesired newline (\n)

This is a discussion on undesired newline (\n) within the C Programming forums, part of the General Programming Boards category; Code: #include<time.h> #include<stdio.h> int main(void) { char hello[25]; time_t timer; timer=time(NULL); strcpy(hello,asctime(localtime(&timer))); timer=time(NULL); printf("%s THIS\n", &hello); fgetc(stdin); return 0; } ...

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    109

    undesired newline (\n)

    Code:
    #include<time.h>
    #include<stdio.h>
    
    int main(void)
    {
        char hello[25];
        time_t timer;
        timer=time(NULL);
        strcpy(hello,asctime(localtime(&timer)));
        timer=time(NULL);
        printf("%s THIS\n", &hello);
        fgetc(stdin);  
        return 0;
    }
    Above is some code which gets the time and prints it to the screen but I have a problem with it. After the time has bee printed to the screen (variable "hello") it automatically puts the next bit of text (THIS) on a new line. How do I stop this?
    OS:- XP
    Compiler:- MSVC++ 6 or DJGPP or Dev-c++ (Mingw)

  2. #2
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    I don't understand, just post the sample output, so I know what do you want to do...
    Don't forget to include <string.h> to use strcpy()
    none...

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    109

    output

    The output of the program is like this:
    Tue Dec 31 12:52:30 2002
    THIS
    but i want it to look like this:
    Tue Dec 31 12:52:30 2002 THIS
    OS:- XP
    Compiler:- MSVC++ 6 or DJGPP or Dev-c++ (Mingw)

  4. #4
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,109
    I dont know if im rightbut i thought a string wich in your case is
    char hello[25] is always terminated by \n if u dont want the \n at the end of the string u have to do it away manually ive read this somewhere i think but i dont know how .... anyway thats why theres a \n because a string is always terminated by \n

  5. #5
    Registered User
    Join Date
    Aug 2002
    Posts
    109

    Hey I got it

    Code:
    #include<time.h>
    #include<stdio.h>
    
    
    void remove_char( char *string, const char item )
    {
            char *at = strchr( string, item );
    
            while( *at ) *at++ = *(at + 1);
    }
    
    
    int main(void)
    {
        char hello[25];
        time_t timer;
        timer=time(NULL);
        strcpy(hello,asctime(localtime(&timer)));
        timer=time(NULL);
        remove_char( hello, '\n' );
        printf("%s THIS\n", &hello);
        fgetc(stdin);  
        return 0;
    }
    That removes the /n but it does bring some warnings up with my compiler (Dev-c++ (mingw)):

    [Warning] In function `remove_char':
    Line 7: [Warning] initialization makes pointer from integer without a cast
    OS:- XP
    Compiler:- MSVC++ 6 or DJGPP or Dev-c++ (Mingw)

  6. #6
    Registered User
    Join Date
    Aug 2002
    Posts
    109

    Red face ooppss

    Sorry I forgot to put #include<string.h> once I put that in it got rid of all the warnings.

    Thanks all for the help
    OS:- XP
    Compiler:- MSVC++ 6 or DJGPP or Dev-c++ (Mingw)

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,643
    Make:

    while( *at ) *at++ = *(at + 1);

    to

    while( *at ) *(at++) = *(at + 1);



    Here's another useful one:

    Code:
    char * swap_char(char * string, char find, char replace)
    {
     char * s;
    
        for(s = string; *s; ++s)
      {
           if(*s == find)
         {
           *s = replace;
         } 
      }
    
     return string;
    }
    
    
    char * remove_newlines(char * string)
    {
      return swap_char(string, '\n', ' ');
    }
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getting rid of newline
    By AngKar in forum C Programming
    Replies: 24
    Last Post: 04-28-2006, 07:52 PM
  2. How to get rid of newline character
    By C++angel in forum C++ Programming
    Replies: 3
    Last Post: 02-07-2006, 06:50 PM
  3. Newline characters
    By xRandyx in forum C Programming
    Replies: 3
    Last Post: 11-15-2005, 10:01 AM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  5. fgets and a bothersome newline char
    By ivandn in forum Linux Programming
    Replies: 1
    Last Post: 11-14-2001, 12:41 PM

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