Thread: pls help urgently

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

    pls help urgently

    Code:
    #include<stdio.h>
    
    /* program to calculate no of tabs,spaces,tabs alongwith characters */
    
    int main(){
    int c;
    int nc,nspc,ntb,nline;
    
    nc=nspc=ntb=nline=0;
    c='\0';
    
    while((c=getchar())!=EOF){
    ++nc;
    
    if(c == ' ')
    ++nspc;
    else if(c=='\t')
    ++ntb;
    else if(c=='\n')
    ++nline;
    else;
    }
    
    printf("\ncharacters:%d\tspace:%d\ttabs:%d\tNewlines:%d\t",nc,nspc,ntb,nline);
    return 0;
    }
    i can't find the bug here..i've used a MinGW gcc compiler on a windows machine..

    the program compiles correctly...i give it ctrl+c as a end of file indicator..

    the programs runs fine the first time..but start giving problems..after wards..sometimes the second last (printf) statement is not executed @ ctrl+c combination...
    other times the printf statement contains all 0 zero values ..pls help
    Last edited by rickyspaceguy; 09-25-2009 at 03:25 PM. Reason: clarification

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by rickyspaceguy View Post
    Code:
    #include<stdio.h>
    
    /* program to calculate no of tabs,spaces,tabs alongwith characters */
    
    int main(){
    int c;
    int nc,nspc,ntb,nline;
    
    nc=nspc=ntb=nline=0;
    c='\0';
    
    while((c=getchar())!='EOF'){
    ++nc;
    
    if(c == ' ')
    ++nspc;
    else if(c=='\t')
    ++ntb;
    else if(c=='\n')
    ++nline;
    else;
    }
    
    printf("\ncharacters:%d\tspace:%d\ttabs:%d\tNewlines:%d\t",nc,nspc,ntb,nline);
    return 0;
    }
    i can't find the bug here..i've used a MinGW gcc compiler on a windows machine..

    the program compiles correctly...i give it ctrl+c as a end of file indicator..

    the programs runs fine the first time..but start giving problems..after wards..sometimes the second last (printf) statement is not executed @ ctrl+c combination...
    other times the printf statement contains all 0 zero values ..pls help
    EOF is just EOF, not 'EOF'. No idea if there is a difference, however.

    ctrl+z is an EOF char for DOS and Windows, rather than ctrl+c.

    Your program doesn't have any loops, so I don't understand what you mean by "starts giving problems...after wards". Afterwards, it simply quits and you have to run it anew.

    It's a very small program, but please get in the habit of indenting your code, straight away.
    Last edited by Adak; 09-25-2009 at 03:37 PM.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Try putting this after the printf:
    Code:
    fflush(stdout);
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Quote Originally Posted by rickyspaceguy View Post
    snip non-indented code

    i can't find the bug here..i've used a MinGW gcc compiler on a windows machine..

    the program compiles correctly...i give it ctrl+c as a end of file indicator..

    the programs runs fine the first time..but start giving problems..after wards..sometimes the second last (printf) statement is not executed @ ctrl+c combination...
    other times the printf statement contains all 0 zero values ..pls help
    'EOF' means literally the letters E O and F, not end of file, which is just the macro EOF, and as others have stated, please indent your code, its barely readable

  5. #5
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by abachler View Post
    'EOF' means literally the letters E O and F, not end of file, which is just the macro EOF, and as others have stated, please indent your code, its barely readable
    Shouldn't 'EOF' generate some type of compiler error (or at least a warning) as '' is reserved for char and "" is for string. . . is it not so?

  6. #6
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Quote Originally Posted by Kennedy View Post
    Shouldn't 'EOF' generate some type of compiler error (or at least a warning) as '' is reserved for char and "" is for string. . . is it not so?
    nein, der preprocessor converts direct string entries into pointers prior to compilation, so the compiler will get a pointer to a string containing 'EOF'. A pointer to a string adn a pointer to a char both have the same type (char*).

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Und warum jetzt sprechen wir Deutsch, Abachler?

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Kennedy View Post
    Shouldn't 'EOF' generate some type of compiler error (or at least a warning) as '' is reserved for char and "" is for string. . . is it not so?
    I thought 'EOF' would generate an error, also.

    It does not.

    Thanks for the why, Abachler.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Man braucht nie einen Grund, Deutsch zum sprechen. And 'EOF' is not a pointer, of course, but a character constant, the exact value of which depends on your compiler, but is probably the values of 'E' and 'O' and 'F' concatenated together along with a warning about a multi-character constant.

  10. #10
    Registered User
    Join Date
    Sep 2009
    Posts
    15

    i'm very sorry

    look people...i've mistakenly copied EOF as 'EOF' i had already detected & tried to edit b4 ne1 replied..but i don't know why wasn't it updated..

    others who say tht y haven't i used Ctrl+z as EOF..is bcoz my friend my m/c doesn't accept ctrl+z as EOF..it accepts ctrl+c..i donno y is this happening

    neways let me see if fflush works....

  11. #11
    Registered User
    Join Date
    Sep 2009
    Posts
    15

    Unhappy fflush doesnt help

    fflush doesnt help too same problems....

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by rickyspaceguy View Post
    look people...i've mistakenly copied EOF as 'EOF' i had already detected & tried to edit b4 ne1 replied..but i don't know why wasn't it updated..

    others who say tht y haven't i used Ctrl+z as EOF..is bcoz my friend my m/c doesn't accept ctrl+z as EOF..it accepts ctrl+c..i donno y is this happening

    neways let me see if fflush works....
    Ctrl-C sends an interrupt signal, which stops everything. It is not EOF. EOF is Ctrl-Z on Windows systems, Ctrl-D on unixy systems.

  13. #13
    Registered User
    Join Date
    Sep 2009
    Posts
    15

    Red face

    Quote Originally Posted by Adak View Post
    EOF is just EOF, not 'EOF'. No idea if there is a difference, however.

    ctrl+z is an EOF char for DOS and Windows, rather than ctrl+c.

    Your program doesn't have any loops, so I don't understand what you mean by "starts giving problems...after wards". Afterwards, it simply quits and you have to run it anew.

    It's a very small program, but please get in the habit of indenting your code, straight away.
    my program doesnt have loops...?? thr is a while loop sitting..

  14. #14
    Registered User
    Join Date
    Sep 2009
    Posts
    15

    Unhappy

    Quote Originally Posted by tabstop View Post
    Ctrl-C sends an interrupt signal, which stops everything. It is not EOF. EOF is Ctrl-Z on Windows systems, Ctrl-D on unixy systems.
    i already know tht bud..can u compile the code then try to run it...but do tell me if u used unix or windows m/c

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Of course it has ONE loop, but it doesn't have the logic or loops that would enable it to take in another line of text, without re-starting it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. i dont know what to do. pls. help!!!!
    By Unregistered in forum C++ Programming
    Replies: 14
    Last Post: 03-14-2002, 03:24 PM
  2. help me pls..... :(
    By mocha_frap024 in forum C++ Programming
    Replies: 2
    Last Post: 02-22-2002, 10:46 AM
  3. C programming - pls pls help me
    By sally arnold in forum C Programming
    Replies: 10
    Last Post: 01-16-2002, 04:55 AM
  4. pls help me!!
    By hanseler in forum C++ Programming
    Replies: 1
    Last Post: 12-05-2001, 08:46 PM
  5. Pls Help Me In This Question!!!
    By Joanna in forum Windows Programming
    Replies: 1
    Last Post: 10-20-2001, 02:05 PM