C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-14-2002, 12:20 AM   #1
Unregistered
Guest
 
Posts: n/a
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.
  Reply With Quote
Old 07-14-2002, 12:23 AM   #2
and the hat of vanishing
 
Salem's Avatar
 
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   Reply With Quote
Old 07-14-2002, 01:15 AM   #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   Reply With Quote
Old 07-14-2002, 01:30 AM   #4
and the hat of vanishing
 
Salem's Avatar
 
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   Reply With Quote
Old 07-14-2002, 01:37 AM   #5
Guest
 
Sebastiani's Avatar
 
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   Reply With Quote
Old 07-14-2002, 12:53 PM   #6
Unregistered
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...
  Reply With Quote
Old 07-14-2002, 01:04 PM   #7
Unregistered
Guest
 
Posts: n/a
yeah it work now. thx guyz
  Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 06:05 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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