Thread: number every line in std output

  1. #16
    Registered User
    Join Date
    Jul 2002
    Posts
    33
    Ouch !

  2. #17
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Quote Originally Posted by john.c View Post
    I doubt Salem was thinking of that!
    Close enough to be useful to the OP.
    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. #18
    Registered User
    Join Date
    Apr 2018
    Posts
    43
    Is it really that easy as everyone assumes?

    I have been stuck on this exercise since sunday , I have spent hours but I can't get to the solution

    Code:
    #include <stdio.h>
    int main()
    {
      int c;
      int counter=1;
      
    
    while((c=getchar())!=EOF){
        
        putchar(c);
        
        
        if ( c == '\n' ) {
            
            putchar(counter+'0');
           putchar(':');
        putchar(' ');
       
            ++counter;
           
        }
       
        
       
    }
    
    
    return 0;
    }
    the output should be 1: input

    what am I doing wrong?

  4. #19
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    I'd expect the first 9 lines to be ok.
    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.

  5. #20
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Quote Originally Posted by joemccarr View Post
    Ouch !
    I apologize. I didn't realize he was only allowed to use putchar and that Salem had mentioned something about that previously. Of course, you could just do it like this:
    Code:
    putchar(line / 10);
    putchar(line % 10);
    And don't use 48 when you mean '0'! (Why do beginners constantly do that???)
    A little inaccuracy saves tons of explanation. - H.H. Munro

  6. #21
    Registered User
    Join Date
    Jul 2002
    Posts
    33
    Quote Originally Posted by Salem View Post
    I'd expect the first 9 lines to be ok.
    That's correct, I didn't want to give him the whole answer just something to get him going in the right direction

  7. #22
    Registered User
    Join Date
    Jul 2002
    Posts
    33
    Quote Originally Posted by john.c View Post
    I apologize. I didn't realize he was only allowed to use putchar and that Salem had mentioned something about that previously. Of course, you could just do it like this:
    Code:
    putchar(line / 10);
    putchar(line % 10);
    And don't use 48 when you mean '0'! (Why do beginners constantly do that???)
    Because I meant add 48 to the int to get to the correct ascii number.

  8. #23
    Registered User
    Join Date
    Jul 2002
    Posts
    33
    Quote Originally Posted by Obvious Guy View Post
    Is it really that easy as everyone assumes?

    I have been stuck on this exercise since sunday , I have spent hours but I can't get to the solution

    Code:
    #include <stdio.h>
    int main()
    {
      int c;
      int counter=1;
      
    
    while((c=getchar())!=EOF){
        
        putchar(c);
        
        
        if ( c == '\n' ) {
            
            putchar(counter+'0');
           putchar(':');
        putchar(' ');
       
            ++counter;
           
        }
       
        
       
    }
    
    
    return 0;
    }
    the output should be 1: input

    what am I doing wrong?
    -----------------------------------------------------------------

    Well its not that simple of a problem if you are just starting out with C.
    The parts to finish this are in the responses you got.

  9. #24
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Quote Originally Posted by joemccarr View Post
    Because I meant add 48 to the int to get to the correct ascii number.
    Well that's not the correct way to do it. The correct way is to NOT ASSUME ASCII (why?) and add '0'.
    So hopefully you've learned something today.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  10. #25
    Registered User
    Join Date
    Jul 2002
    Posts
    33
    Quote Originally Posted by john.c View Post
    Well that's not the correct way to do it. The correct way is to NOT ASSUME ASCII (why?) and add '0'.
    So hopefully you've learned something today.
    Not sure if I follow, your saying to use '0' because it shows that I am intending to manipulate ASCII ?

  11. #26
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Quote Originally Posted by joemccarr View Post
    Not sure if I follow, your saying to use '0' because it shows that I am intending to manipulate ASCII ?
    Althouh it does more clearly show your intention, the reason is deeper. To be portable you can't assme ascii (or utf-8). '0' will always be the correct value, no matter what character set. (In EBCDIC the value of '0' is 240.)
    character encodings

    EDIT: I forgot to add '0' on the putchar in that little code snippet I posted above. (How embarrassing!) It should be:
    Code:
            putchar(line / 10 + '0');
            putchar(line % 10 + '0');
    Last edited by john.c; 05-08-2018 at 01:50 PM.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  12. #27
    Registered User
    Join Date
    Jul 2002
    Posts
    33
    Quote Originally Posted by john.c View Post
    Althouh it does more clearly show your intention, the reason is deeper. To be portable you can't assme ascii (or utf-8). '0' will always be the correct value, no matter what character set. (In EBCDIC the value of '0' is 240.)
    character encodings
    Understood, thanks

  13. #28
    Registered User
    Join Date
    Jul 2002
    Posts
    33
    Quote Originally Posted by Obvious Guy View Post
    Is it really that easy as everyone assumes?

    I have been stuck on this exercise since sunday , I have spent hours but I can't get to the solution
    I don't see how you are going to get the line numbers in front of the lines any other way.

    If your tutor has a sense of humor, you can try this ...

    Code:
    while((c=getchar())!=EOF)
        if ( c == '\n' ) {
    
           {do the coversion of line number -> ascii here}
    
            putchar(':');
            putchar(' ');
            ++linenum;
    
        }
    Last edited by joemccarr; 05-08-2018 at 02:53 PM. Reason: non closed quote

  14. #29
    Registered User
    Join Date
    Apr 2018
    Posts
    43
    hi
    I want to thank you all for your help
    I have finally solved the problem
    It was difficult though and yes I am a beginner

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 05-15-2017, 07:24 AM
  2. Missing a line from the output
    By raz23 in forum C++ Programming
    Replies: 5
    Last Post: 04-05-2013, 05:27 PM
  3. how to get line number and column number
    By zcrself in forum C Programming
    Replies: 3
    Last Post: 12-24-2010, 02:49 PM
  4. Input a Hex number and output hex number to a text field
    By zoobaby in forum C++ Programming
    Replies: 4
    Last Post: 05-12-2009, 11:26 AM
  5. C++ has an output maximum of 80 chars per line?
    By NinchN in forum C++ Programming
    Replies: 11
    Last Post: 02-01-2005, 07:45 AM

Tags for this Thread