Thread: getchar putchar: a word on each line

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    29

    Exclamation getchar putchar: a word on each line

    Hey! I need some help getting my input to output 1 word on each line.
    Instead of being told how, I'd like to learn because it is for school Thank you

    Code:
    #include <stdio.h>
    
    int main(void)
    
    {
        int usertext;
    
        while ((usertext = getchar() ) !=EOF)
            putchar (usertext);
        return 0;
    }
    Last edited by krazymanrebirth; 09-28-2009 at 04:07 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    How does what you have not meet your requirements?

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    You are getting a letter at a time and outputting it. If the user enters a word and presses return, then it will work fine.

    If the user enters two words on a single line, then you'll have to add your own newline character and remove (filter out / ignore) the blanks.
    Mainframe assembler programmer by trade. C coder when I can.

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    29
    For instance if the input was

    The Poem


    This Poem, Is the

    Poem, of the people:


    I would need it to come out as
    The
    Poem
    This
    Poem
    Is
    the
    Poem
    of
    the
    People

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Ok, so check to see what type of thing you've got from getchar, and if it's what you need to stop on, put a newline.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    29
    Alright this is a little better, I need to get rid of the blank spaces when commas are entered, and if multiple blank lines are entered, i need them to disapear.

    Code:
    #include <stdio.h>
    
    int main(void)
    
    {
        int usertext;
        int newline = 0;
    
        while ((usertext = getchar() ) !=EOF)
        {
            if (((usertext >= 'A')&&(usertext <='Z')) || ((usertext >= 'a')&&(usertext <='z')))
            {
                putchar (usertext);
                newline = 1;
    
            }
        else if (newline == 1)
        {
            putchar(10);
            newline = 1;
        }
        }
        return 0;
    }
    Last edited by krazymanrebirth; 09-28-2009 at 04:37 PM.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1. You never reset newline to zero.
    2. You're using an else when you probably shouldn't for the way you're doing it.
    3. You should be setting newline to zero inside the else-chunk-that-shouldn't-be-else.
    4. Of course it's going to print punctuation. You're not telling it not to.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Sep 2009
    Posts
    29
    1. What would be better than the else statement?
    2. Is there a more efficient way to go about the If statement I dont like the way the A Z a z looks...
    3. thank you, Now i need to find out how not to print punctuation...

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Look up <ctype.h>, it has a slew of functions which might interest you. If you're looking in your book's index, look for functions starting with "is".


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Sep 2009
    Posts
    29
    hmm, So am I trying to use isalpha?
    How would i use that function in my code? My class is just now learning functions, how would i go about getting that to work?
    Im reading the Cprogramming.com tutorial on functions and am not understanding how to implement it into my code.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Let's say we only want to show punctuation.
    Code:
    while( (c = getchar()) != EOF )
    {
        if( ispunct( c ) )
            putchar( c );
        else
            putchar( 'X' );
    }
    Now, I chose to display an X for everything that wasn't punctuation. But basically it works something like that. Or if you want to be fun...
    Code:
    while( (c=getchar())!=EOF )
        putchar( ispunct(c)?c:'X' );

    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    Sep 2009
    Posts
    29
    Heres what I got, Not quite workin, but im feelin it! Im starting to understand it better

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main(void)
    
    {
        int usertext;
        int newline = 0;
        int ispunct;
    
        while ((usertext = getchar() ) !=EOF)
        {
            if (ispunct(usertext) )
            {
                putchar(usertext);
                newline = 1;
            }
            else
            {
                putchar(10);
                newline = 0;
            }
        }
        return 0;
    }

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You only need 'newline = ...' in there if you plan on using that for something. For example:
    Code:
    if( ispunct( usertext ) )
        newline = 1;
    if( newline == 1 )
    {
        putchar( '\n' );
        newline = 0;
    }
    else
    {
        putchar( usertext );
    }
    Not that the above code is necessarily the ideal way to do it, but rather, it's an illustration of how you might use that sort of flag to do something later on in your code. As it is in your above code, you aren't actually using it for anything.


    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Registered User
    Join Date
    Sep 2009
    Posts
    29
    Better?

    Now I need to get it functioning again Why isnt the ispunct working?
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main(void)
    
    {
        int usertext;
        int ispunct;
        int newline = 0;
    
        while ((usertext = getchar() ) !=EOF)
        {
            if (ispunct(usertext) )
                newline = 1;
            if (newline == 1)
            {
                putchar('\n');
                newline = 0;
            }
            else
            {
                putchar(usertext);
            }
        }
        return 0;
    }

  15. #15
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    I am not sure you want ispunct - it looks like quzah was just using that as example of the 'is' functions. You are looking for a space, right? Take a look at the different 'is' functions available to you, and decide from there what you need.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  2. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  3. need help please! getchar, putchar, etc.
    By sue in forum C Programming
    Replies: 1
    Last Post: 03-21-2003, 08:40 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM