|
|
|
Register •
FAQ
• Search • Login
|
|
Page 1 of 1
|
[ 6 posts ] |
|
| Author |
Message |
|
Vertex78
Joined: Tue Oct 26, 2004 7:01 am Posts: 7
|
 c programming
I have not programmed in c for about a year now, I never was that good at it but I made it through a sams book on c programming. Well I decided to start from the beginning and start relearning it. So I got a copy of kernighan and ritchies book and I am determined to learn c programming and stick with it this time lol. So hopefully no one will mind a bunch a amatuer questions from me on the subject.
So here is my first newb question, in this program: [code] #include <stdio.h> #include <stdlib.h>
int main() { char c; int blanks; int newlines; int tabs; blanks = newlines = tabs = 0; while ((c = getchar()) != EOF ){ if (c == ' '){ ++blanks; } if (c == '\n'){ ++newlines; } if (c == '\t'){ ++tabs; } } printf("%d blanks, %d newlines, %d tabs\n", blanks, newlines, tabs); system("PAUSE"); return 0; }
One thing that is confusing me is this, say I run it and hit the space bar 4 times then hit backspace for time and then type hello hit return then ^z to exit. The count I get is not what I expected. Why does it not count the first four blanks that I backspaced. The way I am looking at it each time if hit a character that results in a true if statement then it shoul increment the corresponding variable.
-for some reason anything in the brackets for the includes does not show up but they are in my code
[ October 26, 2005: Message edited by: Vertex78 ]</p>
|
| Wed Oct 26, 2005 8:38 pm |
|
 |
|
Hackin
Site Admin
Joined: Tue Aug 06, 2002 7:01 am Posts: 24 Location: Thunder Bay, ON, CA
|
 c programming
Well, my first guess would probably be this line:
while ((c = getchar()) != EOF ){
I would put a c = getchar() function before this and then call it again at the end of each iteration such as:
while(c != EOF) { ... c = getchar(); }
At the moment you're testing to see if c actually equalled getchar(), not the actual value of c.
While I don't think this should affect you're outcomes it is good coding practice.
However, in order to pick apart the code we'll probably need to know exactly what values you are getting from this.
Hope this helps [img]images/smiles/icon_smile.gif[/img]
[ October 26, 2005: Message edited by: Hackin' ]</p>
|
| Thu Oct 27, 2005 1:33 am |
|
 |
|
Vertex78
Joined: Tue Oct 26, 2004 7:01 am Posts: 7
|
 c programming
I think I know why its not doing what I exected now. Tell if I am wrong but when the first getchar() is encountered the program stops and waits for an input if there is not anything in the text stream yet right? So then the text stream is empty until I hit enter. Looking at it from this view it makes sense that the getchar() function would never encounter any backspaces.
|
| Thu Oct 27, 2005 11:25 am |
|
 |
|
sygnus
Joined: Wed Oct 20, 2004 7:01 am Posts: 2 Location: Ontario, Canada
|
 c programming
Wouldn't a double loop, and a kbhit() call be a little better? ie:
while ( c != EOF ) {
while ( !kbhit() ) {
Look at me.. I do absolutely nothing but waste time. If I were a game, this is where all my logic would go.
} c = getchar();
Insert code detection code in here, ie: whether or not it's a space, backspace, etc.
}
Not entirely sure if this is the kinda loop you're looking for, but it lets you run code while you're waiting for a keypress.
|
| Thu Oct 27, 2005 12:40 pm |
|
 |
|
Vertex78
Joined: Tue Oct 26, 2004 7:01 am Posts: 7
|
 c programming
Yeah, maybe it would, but I am going through the Kerighan and Ritchie book and I was supposed to use getchar() and putchar()
|
| Thu Oct 27, 2005 10:46 pm |
|
 |
|
Cjmovie
Joined: Tue Mar 09, 2004 8:01 am Posts: 5 Location: South Carolina
|
 c programming
<blockquote><font size="1" face="Verdana, Helvetica, sans-serif">code:</font><hr><pre> while ((c = getchar()) != EOF ){
I would put a c = getchar() function before this and then call it again at the end of each iteration such as:
while(c != EOF) { ... c = getchar(); }
At the moment you're testing to see if c actually equalled getchar(), not the actual value of c. </pre><hr></blockquote>
Oops, no!
The line "if((c = getchar()) != EOF))" actually _does_ work as intended. This is because the = operator is 'translated' first, setting C to the value of getchar. Then, the value of C is compared to EOF.
[img]images/smiles/icon_smile.gif[/img]
|
| Sat Oct 29, 2005 1:19 am |
|
 |
|
|
Page 1 of 1
|
[ 6 posts ] |
|
Who is online |
Users browsing this forum: No registered users and 2 guests |
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot post attachments in this forum
|
|