Thread: help with using printf()

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    5

    help with using printf()

    I am using Microsoft Visual C++ V6.0 Standard Edition to compile the C code below as a standard console application. But when I run the code in the console and type some text the printf() function will not work for me when printf() is the outside of the While loop the way I have it below. Only when I move the printf() inside the While loop will I get a result. Can anyone tell me what I am doing wrong? This problem also happens when I try to use putchar(). My email is [email protected].


    Code:
    /* This is a program to count blanks, tabs, and newlines */
     
    #include <stdio.h>
     
    void main()
    {
        int nb, nt, nl, c;
     
        nb = 0;
        nt = 0;
        nl = 0; 
        while ((c = getchar()) != EOF)
        {
           if (c == ' ')
               ++nb; 
           if (c == '\t')
               ++nt;   
           if (c == '\n')
               ++nl;
        }
        printf("%d %d %d\n", nb, nt, nl);
    }
    Joe

  2. #2
    Registered User Sake's Avatar
    Join Date
    Jan 2005
    Posts
    89
    >>void main()
    Visual Studio may allow this, but it's not a good idea. The language specification requires main to return int, and not doing so could cause problems.

    Aside from that, the code works just fine. Are you actually signalling EOF with the ctrl+z key combination?
    Kampai!

  3. #3
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    Unless the while loop ever sees an EOF, it won't leave the loop. Try adding a check against a newline as well.

    [edit]just realized that will conflict with the loop trying to count newlines.[/edit]
    Last edited by Scribbler; 02-08-2005 at 08:40 PM.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    try this bit of code
    #include <stdio.h>

    Code:
    int main()
    {
        int nb, nt, nl, c;
     
        nb = 0;
        nt = 0;
        nl = 0; 
        do
        {
            c=getchar();
               if (c == ' ')
               ++nb; 
           if (c == '\t')
               ++nt;   
           if (c == '\n')
               ++nl;
    }while(c!='\n');
    
        /*
        while ((c = getchar()) != '\n')
        {
           if (c == ' ')
               ++nb; 
           if (c == '\t')
               ++nt;   
           if (c == '\n')
               ++nl;
        }*/
        printf("%d %d %d\n", nb, nt, nl);
        getchar();
    }
    hope it helps

  5. #5
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    You get nothing in from your printf statement because you have not reached EOF. run the following code, type random strings, spaces and tabs then signal EOF by pressing Ctrl-z. And start writng your programs correctly, as far as your concerned main always returns an int. When your adept to know it doesn't then you'll know
    Code:
    /* This is a program to count blanks, tabs, and newlines */
     
    #include <stdio.h>
     
    int main(void)
    {
        int nb, nt, nl, c;
     
        nb = 0;
        nt = 0;
        nl = 0; 
        while ((c = getchar()) != EOF)
        {
           if (c == ' ')
               ++nb; 
           if (c == '\t')
               ++nt;   
           if (c == '\n')
               ++nl;
        }
        printf("%d %d %d\n", nb, nt, nl);
        getchar();
        return 0;
    }
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  6. #6
    Registered User
    Join Date
    Feb 2005
    Posts
    5
    Quote Originally Posted by Sake
    >>void main()
    Visual Studio may allow this, but it's not a good idea. The language specification requires main to return int, and not doing so could cause problems.

    Aside from that, the code works just fine. Are you actually signalling EOF with the ctrl+z key combination?

    Hi,
    Thanks for your reply. If I run the program and type some code and then signal EOF without hitting return, the console asks me to 'press any key to close window'. I think you are right though that the problem must be that somehow I am not actually signalling EOF.

    Joe

  7. #7
    Registered User
    Join Date
    Feb 2005
    Posts
    5
    Quote Originally Posted by ssharish
    try this bit of code
    #include <stdio.h>

    Code:
    int main()
    {
        int nb, nt, nl, c;
     
        nb = 0;
        nt = 0;
        nl = 0; 
        do
        {
            c=getchar();
               if (c == ' ')
               ++nb; 
           if (c == '\t')
               ++nt;   
           if (c == '\n')
               ++nl;
    }while(c!='\n');
    
        /*
        while ((c = getchar()) != '\n')
        {
           if (c == ' ')
               ++nb; 
           if (c == '\t')
               ++nt;   
           if (c == '\n')
               ++nl;
        }*/
        printf("%d %d %d\n", nb, nt, nl);
        getchar();
    }
    hope it helps

    Hi,
    Thanks for your reply. I replaced EOF with '\n' and ran my program and it worked. So the problem must have been that I was not signalling EOF. When I run my program and hit the return I guess I am sending a carriage return and not EOF like I thought.

    Joe

  8. #8
    Registered User
    Join Date
    Feb 2005
    Posts
    5
    Quote Originally Posted by ssharish
    try this bit of code
    #include <stdio.h>

    Code:
    int main()
    {
        int nb, nt, nl, c;
     
        nb = 0;
        nt = 0;
        nl = 0; 
        do
        {
            c=getchar();
               if (c == ' ')
               ++nb; 
           if (c == '\t')
               ++nt;   
           if (c == '\n')
               ++nl;
    }while(c!='\n');
    
        /*
        while ((c = getchar()) != '\n')
        {
           if (c == ' ')
               ++nb; 
           if (c == '\t')
               ++nt;   
           if (c == '\n')
               ++nl;
        }*/
        printf("%d %d %d\n", nb, nt, nl);
        getchar();
    }
    hope it helps

    Hi,
    Thanks for your reply. I executed the code above and it worked perfectly. I just started learning c and was not familiar with DO. I have a long way to go.

    Joe

  9. #9
    Registered User
    Join Date
    Feb 2005
    Posts
    5
    Quote Originally Posted by Scribbler
    Unless the while loop ever sees an EOF, it won't leave the loop. Try adding a check against a newline as well.

    [edit]just realized that will conflict with the loop trying to count newlines.[/edit]

    Hi,
    Thanks for your reply. Checking for a newline was one way to solve the problem. With my code, apparently the while loop never sees an EOF.

    Joe

  10. #10
    Registered User
    Join Date
    Feb 2005
    Posts
    24
    studying...............

  11. #11
    ---
    Join Date
    May 2004
    Posts
    1,379
    Hi,
    Thanks for you 4 consecutive replies. ( )

    sand_man

  12. #12
    C maniac
    Join Date
    Aug 2004
    Location
    Cyberwarping to Middle Earth
    Posts
    154
    As far as I know, getchar() and putchar() are buffered, which means that they wait for an enter before anything happens.

  13. #13
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    To get your program to read the contents of a file easily, try this in a Windows console:

    Code:
    c:\>type myprog.c | myprog.exe
    142 0 23
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. making it portable.....?
    By ShadeS_07 in forum C Programming
    Replies: 11
    Last Post: 12-24-2008, 09:38 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. segmentation fault upon reload
    By yabud in forum C Programming
    Replies: 8
    Last Post: 12-18-2006, 06:54 AM
  4. Simple C question: user input to repeat a loop
    By evernaut in forum C Programming
    Replies: 2
    Last Post: 11-18-2006, 09:23 AM
  5. Drawing tables in C
    By stanoman in forum C Programming
    Replies: 5
    Last Post: 10-09-2003, 10:14 AM