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>
/* 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='
-
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
-

Originally Posted by
rickyspaceguy
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.
-
spurious conceit
Try putting this after the printf:
-
Malum in se

Originally Posted by
rickyspaceguy
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
Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.
-
{Jaxom,Imriel,Liam}'s Dad

Originally Posted by
abachler
'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?
-
Malum in se

Originally Posted by
Kennedy
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*).
Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.
-
Guest
Und warum jetzt sprechen wir Deutsch, Abachler?
-

Originally Posted by
Kennedy
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.
-
and the Hat of Guessing
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.
-
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....
-
fflush doesnt help
fflush doesnt help too same problems....
-
and the Hat of Guessing

Originally Posted by
rickyspaceguy
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.
-

Originally Posted by
Adak
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..
-

Originally Posted by
tabstop
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
-
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
Similar Threads
-
By Unregistered in forum C++ Programming
Replies: 14
Last Post: 03-14-2002, 02:24 PM
-
By mocha_frap024 in forum C++ Programming
Replies: 2
Last Post: 02-22-2002, 09:46 AM
-
By sally arnold in forum C Programming
Replies: 10
Last Post: 01-16-2002, 03:55 AM
-
By hanseler in forum C++ Programming
Replies: 1
Last Post: 12-05-2001, 07:46 PM
-
By Joanna in forum Windows Programming
Replies: 1
Last Post: 10-20-2001, 02:05 PM