Thread: number every line in std output

  1. #1
    Registered User
    Join Date
    Apr 2018
    Posts
    43

    number every line in std output

    The code belows prints the input to std output until EOF is detected

    After the user inputs' \n' the output should look like this 01: <input>
    the second line 02: <input> and so on until 100.

    I am only allowed to use putchar one local variable and a global variable.

    I am having trouble figuring out the approach I should take.

    Could someone give me a hint?


    Code:
    #include <stdio.h>
    
    
    
    int main(){
    
    int c;    
    
        
        while((c=getchar())!=EOF){
            
            
            
            
                   putchar(c);        
            }
            
            
            
        return 0;    
        
       }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It sounds like you need a line counter in addition to the variable you are using to store the character read. Both should be local variables, though of course if the requirements state that one must be a global variable, you could do just as well with a global variable, though then keep in mind that such a requirement is a bad requirement. Your current loop is fine, except that you need to do more stuff in the loop body, e.g., check if the current character is a newline character.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2018
    Posts
    43
    thanks for the reply

    thats what I tried
    my pseudocode in the loop
    if(c=='\n')
    ++counter; // adds the newline to the count

    now I am not sure what I should do next how do I get the program to increment 01 till 100?
    thats where im.stuck

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You already have the program incrementing the line counter, so it is just a matter of printing it at the right time.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    33
    Here's a hint

    Use this in your while loop to store the input characters.

    mystring[i++]=c;

    At the end of your program you want to print out this string but with the line numbers in front.

  6. #6
    Registered User
    Join Date
    Apr 2018
    Posts
    43
    thanks but I am only allowed to use one local variable and a global variable so in my case

    counter[i++]=c?

    I am only allowed to use one local variable and one global variable

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Does this count as one variable?
    Code:
    struct {
        char buff[BUFSIZ];
        int linenumber;
    } singleVariable;
    > After the user inputs' \n' the output should look like this 01: <input>
    Where were you planning to store input up to the point where you receive the \n?

    Code:
    printf("%02d ",++linenum);
    while((c=getchar())!=EOF) {
      putchar(c);
      if ( c == '\n' ) printf("%02d ",++linenum);
    }
    > I am only allowed to use putchar one local variable and a global variable.
    Pointless contrivances which aren't teaching you how to program.
    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.

  8. #8
    Registered User
    Join Date
    Apr 2018
    Posts
    43
    I just wrote my tutor an email

    he said I can use two local variables and 1 global variable

    if I use anymore then I wouldn't fulfill his requirements.

    And no structs and printf function allowed. -.-

    I really have no Idea how to do this.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well can you replace my printf calls with a bit of math, and putchar?
    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.

  10. #10
    Registered User
    Join Date
    Jul 2002
    Posts
    33
    I'd call him again. It kinda looks like the number of local & global variables is not constant ; )

  11. #11
    Registered User
    Join Date
    Apr 2018
    Posts
    43
    sorry guys

    so I can have 2 local variables and one global variable

    so in my case I already have one local variable which is int c.

    so now 2 variables remain one local and one global.

  12. #12
    Registered User
    Join Date
    Apr 2018
    Posts
    43
    i will try that means I just have to delete the struct

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well I think you only need two variables.
    The scope is irrelevant.
    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.

  14. #14
    Registered User
    Join Date
    Jul 2002
    Posts
    33
    You could use something like this, which is what Salem was talking about.
    For displaying line #s 1 - 9 ...

    Code:
      if ((linenum > 0) && (linenum < 10)){
                 putchar('0');
                 putchar((linenum )+48);
       }

  15. #15
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Quote Originally Posted by joemccarr View Post
    You could use something like this, which is what Salem was talking about.]
    I doubt Salem was thinking of that!
    A little inaccuracy saves tons of explanation. - H.H. Munro

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