pls help urgently

This is a discussion on pls help urgently within the C Programming forums, part of the General Programming Boards category; Code: #include<stdio.h> int main(){ int c,nl; nl=0; while((c=getchar())!=EOF) if(c=='\n') ++nl; printf("\n%d\n",nl); return 0; } this is an example code frm ...

  1. #16
    Registered User
    Join Date
    Sep 2009
    Posts
    15
    Code:
    #include<stdio.h>
    
    int main(){
    int c,nl;
    
    nl=0;
    while((c=getchar())!=EOF)
    if(c=='\n')
    ++nl;
    printf("\n%d\n",nl);
    return 0;
    }
    this is an example code frm denis ritchie's book..it accepts ctrl+c as EOF & gives up no no problems..but try changin '\n' to '\t' or ' ' & all hell breaks loose

  2. #17
    Registered User
    Join Date
    Sep 2009
    Posts
    15
    Quote Originally Posted by Adak View Post
    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.
    pls see the above example code i posted..well it does tk it to next line of logic...why???

    bcos it does give correct answer albeit not always...thts my actual problem

    it w'd b better if u compiled the code...
    Last edited by rickyspaceguy; 09-26-2009 at 02:10 AM. Reason: presentation

  3. #18
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Stop abbreviating the crap out of everything, and form a complete thought. What is it you're actually trying to say? What's your exact problem? Start over, because you're just confusing whatever it was that you were originally trying to say.


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

  4. #19
    Registered User
    Join Date
    Sep 2009
    Posts
    15
    Microsoft Windows [Version 6.0.6000]
    Copyright (c) 2006 Microsoft Corporation. All rights reserved.


    C:\Users\Ricky>cd c:\MinGW\bin

    c:\MinGW\bin>mingw32-gcc c:\MinGW\work\count.c -o c:\MinGW\work\count.out

    c:\MinGW\bin>c:\MinGW\work\count.out
    cnajnjkfn jbjfdjknfa ^C
    c:\MinGW\bin>c:\MinGW\work\count.out
    ncnjkjhfa jjjahrhrhrh dkfkjfkdjf
    characters:0 space:0 tabs:0 Newlines:0
    c:\MinGW\bin>

    this is what i get..u see sumtimes the last line is executed other time its not..i can't fig out wat is happening

  5. #20
    Registered User
    Join Date
    Sep 2009
    Posts
    15
    pls refer to my original post here

    & u may like to see this too..

  6. #21
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    ctrl C isn't EOF. It kills the program. Try ctrl D.


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

  7. #22
    Registered User
    Join Date
    Sep 2009
    Posts
    15
    Quote Originally Posted by quzah View Post
    ctrl C isn't EOF. It kills the program. Try ctrl D.


    Quzah.
    i already know tht..ctrl D is infact used for unix while windows uses ctrl+z... but here gcc compiler doesnt tk ctrl+z or ctrl+d as EOF..i donno y is such a thing happening..btw im using a windows m/c..it must b evident from all the command prompt messages i posted

  8. #23
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    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
    Let me explain you clearly:
    I copied your code, pasted it then executed it 4 times and got the correct output everytime. I used Ctrl+Z for EOF.
    Now do the same thing and tell us the error or problem encountered.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  9. #24
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by rickyspaceguy View Post
    i already know tht..ctrl D is infact used for unix while windows uses ctrl+z...
    You're right so far...
    but here gcc compiler doesnt tk ctrl+z or ctrl+d as EOF..
    Your compiler has nothing to do with what EOF is. It matters what environment you're running the program under.
    i donno y is such a thing happening..btw im using a windows m/c..it must b evident from all the command prompt messages i posted
    As I've said, CTRL C is stopping your program from ever running. That's why you aren't getting any output.


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

  10. #25
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I have indeed compiled your program, and corrected it's mistakes, and posted the corrected program, along with the note that

    ctrl+z is EOF in DOS and Windows - not ctrl+c

    I've told you, Ben10 has told you, Quzah has told you - all telling you this same thing.


    If you don't want to take advice anymore, please stop asking for help. You're taking being stubborn into the realm of the truly world class jackasses.

    Stop it, for gawd's sake.

  11. #26
    Registered User
    Join Date
    Sep 2009
    Posts
    15
    Quote Originally Posted by BEN10 View Post
    Let me explain you clearly:
    I copied your code, pasted it then executed it 4 times and got the correct output everytime. I used Ctrl+Z for EOF.
    Now do the same thing and tell us the error or problem encountered.
    thanks ben for the first serious attempt...

    this is the command i give @ my windows cmd prompt alongwith the results..can u still find why is this happening??


    c:\MinGW\bin>mingw32-gcc c:\MinGW\work\count.c -o c:\MinGW\work\count.out

    c:\MinGW\bin>c:\MinGW\work\count.out
    nadbhbb jndsjnsjdn nmnjnssdanj ^Z^Z^C
    c:\MinGW\bin>c:\MinGW\work\count.out
    KDNSLNJBNDJND nsabbsbdjjnsjdn jdsjnjdn
    cznnmn^Z^C
    c:\MinGW\bin>c:\MinGW\work\count.out
    basbgdsja jhjhfdsa
    mnsnnnjkn mmsdm^Z^C
    c:\MinGW\bin>c:\MinGW\work\count.out
    fasjbfbamnbb bbsajbsa
    ^C
    c:\MinGW\bin>c:\MinGW\work\count.out
    jbjsann nsjandjndsn kksak
    nmjbjj
    characters:27 space:3 tabs:0 Newlines:1 ^C
    c:\MinGW\bin>c:\MinGW\work\count.out
    ndsnhhdshbjb jsajbnsajbdfjb jsabjbfbj
    sjfnjfnsjnfs cnnjncj jxzcbjkcjk^Z^C
    c:\MinGW\bin>c:\MinGW\work\count.out
    sjansjnjns jjaBJKBAb JJDBDjnn nj

    characters:33 space:3 tabs:0 Newlines:1
    c:\MinGW\bin>c:\MinGW\work\count.out
    nndsnd,n njknsankd msnansaknklsam kasnknks
    ^C
    c:\MinGW\bin

    ^Z is actually ctrl +Z
    so u can see the program doesnt take @ctrl+z as EOF
    so i tried using ctrl+c..sumtimes it gets executed sumtimes it isnt thts wat bothers me
    i use a MinGW compiler with no ide...i donno whether visual studio ide w'd work...

  12. #27
    Registered User
    Join Date
    Sep 2009
    Posts
    15
    Quote Originally Posted by Adak View Post
    I have indeed compiled your program, and corrected it's mistakes, and posted the corrected program, along with the note that

    ctrl+z is EOF in DOS and Windows - not ctrl+c

    I've told you, Ben10 has told you, Quzah has told you - all telling you this same thing.


    If you don't want to take advice anymore, please stop asking for help. You're taking being stubborn into the realm of the truly world class jackasses.

    Stop it, for gawd's sake.
    well dear friend i can wish to be accepted as a jackass but not stubborn...im trying to know wat is happening..i use ctrl+z all the time..but the loop won't end so i tried using ctrl+c..

    sumtimes the program is taking ctrl+c as EOF but other times as kill command..

    thts y i m curious abt wat's actually happening

    btw where have u posted the corrected program

  13. #28
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No it isn't. It's not ever taking CTRL C as EOF. If it were, it would actually be giving you the final printf statement. We've already told you what's wrong. Why on earth should he post the "corrected program" for you? You'd just use it wrong anyway.

    I'm done here. You can't teach people who won't listen.


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

  14. #29
    Registered User
    Join Date
    Sep 2009
    Posts
    15
    Quote Originally Posted by quzah View Post
    No it isn't. It's not ever taking CTRL C as EOF. If it were, it would actually be giving you the final printf statement. We've already told you what's wrong. Why on earth should he post the "corrected program" for you? You'd just use it wrong anyway.

    I'm done here. You can't teach people who won't listen.


    Quzah.
    hey pls...

    c:\MinGW\bin>c:\MinGW\work\count.out
    jbjsann nsjandjndsn kksak
    nmjbjj
    characters:27 space:3 tabs:0 Newlines:1 ^C

    this was reached with ctrl+c command...if the process was wholly killed why did the msg got printed

    neways it bcoz sumbody said he posted the corrected program i asked 4 it..u r probably not looking at the output i posted....just tk a luk pls..& spit it out tht anger..
    Last edited by rickyspaceguy; 09-26-2009 at 03:42 AM.

  15. #30
    Registered User
    Join Date
    Sep 2009
    Posts
    15
    sorry people i at last figured it out...i didnt know tht a return is what makes stdin available to getchar()..
    im sorry for all the bad blood

    ctrl+z is the EOF in windows...i figured it out how to make it available to my program..thx 4 all the concer

Page 2 of 3 FirstFirst 123 LastLast
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, 03:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21