![]() |
| | #1 |
| Registered User Join Date: Jun 2008
Posts: 5
| Newb Question Character Counting I am trying to create a small program that counts characters. I am following along in a book and I entered the code exactly as it says, but it doesn't work like it should (it does compile though). I'll post the code below. Code: #define EOF -1
main ()
{
long nc;
nc = 0;
while (getchar() != EOF)
++nc;
printf("%1d\n", nc);
}
|
| Wilder is offline | |
| | #2 |
| int x = *((int *) NULL); Join Date: Jul 2003 Location: Banks of the River Styx
Posts: 891
| You need to send a EOF. Either pipe a file in through stdin, like: Code: cat somefile.txt | yourprogram Or, if you're on Windows type somefile.txt | yourprogram Edit: Also, main() returns an int! Make it such! Code: int main()
{
// ... code ...
return 0; // or some nonzero number if errors occur.
}
__________________ long time; /* know C? */ Unprecedented performance: Nothing ever ran this slow before. Any sufficiently advanced bug is indistinguishable from a feature. Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31. The best way to accelerate an IBM is at 9.8 m/s/s. recursion (re - cur' - zhun) n. 1. (see recursion) |
| Cactus_Hugger is offline | |
| | #3 |
| Registered User Join Date: Jun 2008
Posts: 5
| Thanks, pretty sure I understand now. |
| Wilder is offline | |
| | #4 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
|
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #5 | |
| Chinese pâté Join Date: Jul 2007 Location: Canada
Posts: 406
| Quote:
Oh and Wilder, don't forget to include stdio.h in your source file (#include <stdio.h>) and remove that line where you define EOF.
__________________ I hate real numbers. | |
| foxman is offline | |
| | #6 |
| Registered User Join Date: Jun 2008
Posts: 5
| I changed the code around a little bit so it is now: Code: #include <stdio.h>
main()
{
long nc;
nc = 0;
while (getchar() != EOF)
++nc;
printf("%1d\n", nc);
while (1 != 0) // Only here so that the DOS window doesn't close automatically
; // so I can view the result.
}
|
| Wilder is offline | |
| | #7 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| http://cpwiki.sourceforge.net/Implicit_main Just use fgets instead.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
Last edited by Elysia; 06-22-2008 at 09:54 PM. | |
| Elysia is offline | |
| | #8 | |
| Frequently Quite Prolix Join Date: Apr 2005 Location: Canada
Posts: 7,629
| Quote:
Or you could use a function that does it all for you, like fgets(), as Elysia suggested. Personally, I wouldn't, though; you're just counting characters, after all, so reading one character at a time makes more sense than reading a whole line all in one go . . . . Also: the "1" in %1d is entirely optional. This works just fine. Code: printf("%d\n", nc);
Code: while (1 != 0) // Only here so that the DOS window doesn't close automatically ; // so I can view the result. However, it uses 100% of your processor, so it's not ideal. You could put another getchar() in there to wait for a key to be pressed, but I don't think that would work after you type EOF, so only try this once you have the '\n'/fgets() thing working.
__________________ dwk Seek and ye shall find. quaere et invenies. "Simplicity does not precede complexity, but follows it." -- Alan Perlis "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra "The only real mistake is the one from which we learn nothing." -- John Powell Other boards: DaniWeb, TPS Unofficial Wiki FAQ: cpwiki.sf.net My website: http://dwks.theprogrammingsite.com/ Projects: codeform, xuni, atlantis, etc. New project: nort | |
| dwks is offline | |
| | #9 |
| Registered User Join Date: Jun 2008
Posts: 5
| Thanks for the help, I really appreciate it. The program now works just like I want it to. Final code is: Code: // Counts the number of characters in the input
#include <stdio.h>
int main()
{
long nc;
nc = 0;
printf("Please type a phrase then press ENTER to see how many charcters it has.\n\nInput: ");
while (getchar() != '\n')
++nc;
printf("\nThe input has %d characters.\n\nPress ENTER to exit", nc);
getchar(); // Used to prevent DOS window from closing automatically
return 0;
}
Last edited by Wilder; 06-22-2008 at 11:33 PM. |
| Wilder is offline | |
| | #10 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| Do you even read links that are posted?
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #11 |
| Frequently Quite Prolix Join Date: Apr 2005 Location: Canada
Posts: 7,629
| Well, because it was just a bare link with no explanation, you could think by accident that it was related to fgets() . . . . But still, Elysia is right, of course. You should use "int main()" and add a "return 0;" statement to the end of main(). The way you have it is relying on the implicit int rule, which is deprecated. But I'm sure I'm just repeating Elysia's link, which I urge you to read. http://cpwiki.sourceforge.net/Implicit_main [edit] I just know it's going to start a "main" debate, but int main() is actually a better idea than int main(void), because main() is going to be passed some parameters whether you like it or not, and stating int main(void) tells the compiler that no parameters will be passed, while int main() says that anything could be passed. At least, this is what someone said in the last debate. (Was it brewbuck?) I don't buy into it, myself . . . . [/edit]
__________________ dwk Seek and ye shall find. quaere et invenies. "Simplicity does not precede complexity, but follows it." -- Alan Perlis "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra "The only real mistake is the one from which we learn nothing." -- John Powell Other boards: DaniWeb, TPS Unofficial Wiki FAQ: cpwiki.sf.net My website: http://dwks.theprogrammingsite.com/ Projects: codeform, xuni, atlantis, etc. New project: nort Last edited by dwks; 06-22-2008 at 11:17 PM. |
| dwks is offline | |
| | #12 | ||
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| Quote:
Just not implicit main.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| ||
| Elysia is offline | |
| | #13 |
| Registered User Join Date: Jun 2008
Posts: 5
| Ok i edited it a bit. Should be all right now? |
| Wilder is offline | |
| | #14 |
| Frequently Quite Prolix Join Date: Apr 2005 Location: Canada
Posts: 7,629
| Perfect! Well, just about . . . ![]() %d is for ints; nc is a long. nc should either be an int, or you should use the format specifier for longs: %ld. Code: printf("%ld\n", nc);
You see, consoles are typically line-buffered; that is, they might only display what you've printed to them when you print a newline. So, in some cases, your message might not be printed. You could fix this by ending the printf with a newline, which I would recommend. You could also use fflush(stdout) to force the output to be written to the screen, but that's probably just too complicated for something that probably will never happen. ![]() [edit] This "buffer" thing needs elaboration. With files and other sources of input and output, it's generally inefficient to read just one character at a time. For this reason, the operating system often reads or writes data in largish chunks. When you write one character to a file, it's stored in a buffer instead of being written immediately. When this buffer gets full, then, and only then, is the data written to the file. It's more efficient this way. Unfortunately, this can cause some trouble for us programmers. For example, if you terminate a program that is writing to a file, the latest changes might not have been written to the file. In your case, data you write to the screen might not be visible unless you print a newline. But buffering is still a good thing. Your computer would be very slow without it. ![]() Fortunately, you can force whatever's in the buffer to be written to the file or to the screen immediately. Which I indicated, with fflush(). Since the screen is line-buffered, you can also just print a newline to force the buffer to be flushed. [/edit]
__________________ dwk Seek and ye shall find. quaere et invenies. "Simplicity does not precede complexity, but follows it." -- Alan Perlis "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra "The only real mistake is the one from which we learn nothing." -- John Powell Other boards: DaniWeb, TPS Unofficial Wiki FAQ: cpwiki.sf.net My website: http://dwks.theprogrammingsite.com/ Projects: codeform, xuni, atlantis, etc. New project: nort Last edited by dwks; 06-22-2008 at 11:44 PM. |
| dwks is offline | |
![]() |
| Tags |
| character, count, counting, newb |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| need help with 'character counting from a .txt file' | master_vst | C Programming | 5 | 11-09-2008 02:17 PM |
| Newb question - Clear command line screen? | Fujimitsu | C Programming | 3 | 04-12-2008 01:47 PM |
| Just a quick question about character arrays | Welshy | C Programming | 3 | 04-03-2006 07:20 AM |
| UNICODE and GET_STATE | Registered | C++ Programming | 1 | 07-15-2002 03:23 PM |
| Character counting program | TankCDR | C++ Programming | 5 | 04-05-2002 10:01 PM |