I was fiddling around with a simple program in the first chapter of K&R that looks something like this:
I wanted to modify the program, so that it would take the input, and instead of basically just copying it to the output once, have it copy it out, say like ten times or whatever. I made a few attempts at this modification, but I am not sure of how to get what I want. I added a for loop, to make it look like this:Code:#include <stdio.h> /* copy input to output; 1st version */ int main(void) { int c; c = getchar(); while ( c != EOF ) { putchar( c ); c = getchar(); } return 0; }
This works, but it does not do what I want it to do. If I type in the word 'hello', instead of displaying hello ten times, it gives me this:Code:#include <stdio.h> int c,i; int main(void) { /* copy the input to the output ten times */ while(( c = getchar()) != EOF ) { for ( i = 0; i <= 10; i++ ) putchar ( c ); } return 0; }
hhhhhhhhhhheeeeeeeeeeelllllllllllllllllllllloooooo ooooo
Not exactly what I was after. Even though this is a trivial modification, I would still like to know how to make it work the way I want it to.
kermit