-
Whats wrong?
Whats wrong with this?
I am a newbie plz help!
what is wrong???
#include <stdio.h>
main()
{
int c, nl, np, nq;
printf("Press ENTER and then CTRL+C to finish input\n");
nl = 0;
np = 0;
nq = 0;
while ((c = getchar()) != EOF)
if(c == '\n')
++nl;
if(c == '\t')
++np;
if(c == ' ')
++nq;
printf("FINAL REPORT:\n");
printf("Blanks: %d\n", nq);
printf("Lines: %d\n", nl);
printf("Tabs: %d\n", np);
}
It only sez the correct number of lines and everything else is incorrwctly zero.
If i do this:
#include <stdio.h>
main()
{
int c, nl, np, nq;
printf("Press ENTER and then CTRL+C to finish input\n");
nl = 0;
np = 0;
nq = 0;
while ((c = getchar()) != EOF)
if(c == '\t')
++np;
if(c == '\n')
++nl;
if(c == ' ')
++nq;
printf("FINAL REPORT:\n");
printf("Blanks: %d\n", nq);
printf("Lines: %d\n", nl);
printf("Tabs: %d\n", np);
}
Thx 4 Help.
-
Try putting {} around all the statements you want in your while loop.
-
I can't quite figure out what your program is trying to do
but CTRL+C is a dangerous thing to ask the user to type.
When the console server detects a CTRL+C it terminates the
execution of the current program.
Some debuggers use a "signal" to catch the CTRL+C and continue
execution but when not in a debugger it's like pulling the plug.
Post a more complete explaination of what you want to do and I'll
try to help.
-
> but CTRL+C is a dangerous thing to ask the user to type
Good point - it should be CTRL+D (Unix/Linux) or CTRL+Z(DOS) to generate an EOF on the input stream
-
Not to mention that it's a bad habit. If you are running a program that has just alloc'd 1MB and abort it - guess what?
-
but if i dont make them press CTRL+C then once the input is over the programm will just skip a line once enter is pressed and wait. if CTRL+C is pressed the output is produced. ill try it the "{"s now but pepper ann is on...
-
yeah it work now. thx guyz