What am I doing wrong??

J

JS

I have made the following code;


#include <stdio.h>

main(){

int c, i, nwhite, nother;
int ndigit[10];

nwhite = nother = 0;

for (i = 0; i < 10; ++i)
ndigit = 0;

while ((c = getchar()) != EOF)
if (c >= '0' && c <= '9')
++ndigit[c-'0'];
else
if (c == ' ' || c == '\n' || c == '\t')
++nwhite;
else
++nother;

printf("digits =");
for (i = 0; i < 10; ++i)
printf(" %d", ndigit);

printf(", white space = %d, other = %d\n",
nwhite, nother);

}

It compiles ok and I also get an .exe file. But when I doubleclick on the
..exe file from WinXP explorer a black box appears and I can enter som text,
press enter and then nothing happens...the black box does not disappear
before I chose to close it.

I use the Cygwin compiler bcause I need my code to be run on a Linux system.

Hope somone can help

JS
 
M

Michael Coyne

I have made the following code;
...
main(){

main should really properly return an int:
int main(void){

.... which will also necessitate adding:
return 0;

just before the closing brace.
It compiles ok and I also get an .exe file. But when I doubleclick on the
.exe file from WinXP explorer a black box appears and I can enter som
text, press enter and then nothing happens...the black box does not
disappear before I chose to close it.

What do you expect? You've got a while loop that doesn't end until it
sees an EOF...

Type in some text, press enter a few times, and then trying sending an EOF
(typically CTRL-D).

Your program should spit out the results as expected.


Michael
 
J

JS

Michael Coyne said:
main should really properly return an int:
int main(void){

... which will also necessitate adding:
return 0;

just before the closing brace.


What do you expect? You've got a while loop that doesn't end until it
sees an EOF...

Type in some text, press enter a few times, and then trying sending an EOF
(typically CTRL-D).

Your program should spit out the results as expected.

I have tried to enter a few numbers and some text but when I press CTRL-D
the window just closes!
 
M

Michael Coyne

...
Type in some text, press enter a few times, and then trying sending an EOF
(typically CTRL-D).

Your program should spit out the results as expected.

<OT>
I should also have added that, if you're running this by double-clicking
on it in the Explorer, most likely the minute you hit CTRL-D, the program
will spit out the results and the window will promptly close.

Open up a command prompt, navigate to the proper directory, and then run
the program by typing its name on the command line.
</OT>


Michael
 
J

JS

Michael Coyne said:
<OT>
I should also have added that, if you're running this by double-clicking
on it in the Explorer, most likely the minute you hit CTRL-D, the program
will spit out the results and the window will promptly close.

Open up a command prompt, navigate to the proper directory, and then run
the program by typing its name on the command line.
</OT>


I works if I press CTRL+Z instead! Can someone explain the difference
between CTRL+D and CTRL+Z?
 
K

Keith Thompson

Michael Coyne said:
What do you expect? You've got a while loop that doesn't end until it
sees an EOF...

Type in some text, press enter a few times, and then trying sending an EOF
(typically CTRL-D).

Your program should spit out the results as expected.

<OT>
If he runs it by double-clicking from a WinXP explorer window, it will
expect control-Z to mark EOF. Once it gets an EOF, the box will close
before he can read the output. He can try running from a command
prompt (MS-DOS window) or from a Cygwin shell (bash, tcsh, whatever).
</OT>
 
J

JS

Keith Thompson said:
<OT>
If he runs it by double-clicking from a WinXP explorer window, it will
expect control-Z to mark EOF. Once it gets an EOF, the box will close
before he can read the output. He can try running from a command
prompt (MS-DOS window) or from a Cygwin shell (bash, tcsh, whatever).
</OT>


I found out that I needed to press CTRL+Z, but what is EOF. I know it means
End Of File but is it some specific integer??
 
M

Michael Mair

JS said:
Michael Coyne said:
]

It compiles ok and I also get an .exe file. But when I doubleclick on
the
.exe file from WinXP explorer a black box appears and I can enter som
text, press enter and then nothing happens...the black box does not
disappear before I chose to close it.

What do you expect? You've got a while loop that doesn't end until it
sees an EOF...

Type in some text, press enter a few times, and then trying sending an
EOF
(typically CTRL-D).

Your program should spit out the results as expected.

<OT>
If he runs it by double-clicking from a WinXP explorer window, it will
expect control-Z to mark EOF. Once it gets an EOF, the box will close
before he can read the output. He can try running from a command
prompt (MS-DOS window) or from a Cygwin shell (bash, tcsh, whatever).
</OT>

I found out that I needed to press CTRL+Z, but what is EOF. I know it means
End Of File but is it some specific integer??

EOF is a negative int value defined in <stdio.h>.
Many input functions return EOF if the end-of-file has been
encountered; you can "send" an end-of-file for stdin for example
with CTRL+D on Unix systems and with CTRL+Z on Windows/MS-DOS systems.


Cheers
Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,125
Latest member
VinayKumar Nevatia_
Top