![]() |
| | #1 |
| Guest
Posts: n/a
| Whats wrong? 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. |
|
| | #2 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| Try putting {} around all the statements you want in your while loop. |
| Salem is offline | |
| | #3 |
| Registered User Join Date: Jul 2002
Posts: 28
| 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. |
| jerryvtts is offline | |
| | #4 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| > 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 |
| Salem is offline | |
| | #5 |
| Guest Join Date: Aug 2001
Posts: 4,923
| 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? |
| Sebastiani is offline | |
| | #6 |
| Guest
Posts: n/a
| 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... |
|
| | #7 |
| Guest
Posts: n/a
| yeah it work now. thx guyz |
|
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| What's wrong with my code (HELP) | n00by | C Programming | 20 | 08-11-2004 03:15 PM |
| Problem with input choice... Somethings wrong... really wrong.... | greenferoz | C++ Programming | 9 | 07-15-2004 03:30 PM |
| Debugging-Looking in the wrong places | JaWiB | A Brief History of Cprogramming.com | 1 | 11-03-2003 10:50 PM |
| Confused: What is wrong with void?? | Machewy | C++ Programming | 19 | 04-15-2003 12:40 PM |
| God | datainjector | A Brief History of Cprogramming.com | 746 | 12-22-2002 12:01 PM |