Is this program doing what is supposed to do ?

Discussion in 'C Programming' started by devkays, Jan 30, 2011.

  1. devkays

    devkays Guest

    So I wrote the following program in C, but I'm not sure that it is doing what it is supposed to do. I verified the source code against the solution in the back of the book and I'm pasting here what the solution in the back of the book is.


    #include<stdio.h>
    #include<stdlib.h>

    int main(){

    int ch;
    int at_beginning = 1;
    int line = 0;

    while( (ch==getchar())!= EOF){

    if(at_beginning == 1){

    at_beginning = 0;
    line+=1;
    printf("%d ", line);

    }

    putchar(ch);

    if(ch == '\n')
    at_beginning = 1;
    }
    return EXIT_SUCCESS;
    }
     
    devkays, Jan 30, 2011
    #1
    1. Advertising

  2. devkays

    Ian Collins Guest

    On 01/31/11 10:02 AM, devkays wrote:
    > So I wrote the following program in C, but I'm not sure that it is doing what it is supposed to do. I verified the source code against the solution in the back of the book and I'm pasting here what the solution in the back of the book is.
    >

    Well what is it supposed to do?

    > #include<stdio.h>
    > #include<stdlib.h>
    >
    > int main(){
    >
    > int ch;
    > int at_beginning = 1;
    > int line = 0;
    >
    > while( (ch==getchar())!= EOF){
    >
    > if(at_beginning == 1){
    >
    > at_beginning = 0;
    > line+=1;
    > printf("%d ", line);
    >
    > }
    >
    > putchar(ch);
    >
    > if(ch == '\n')
    > at_beginning = 1;
    > }
    > return EXIT_SUCCESS;
    > }



    --
    Ian Collins
     
    Ian Collins, Jan 30, 2011
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ThunderMusic

    How I'm I supposed to use string tables?

    ThunderMusic, Oct 15, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    449
    John Saunders
    Oct 15, 2004
  2. Simon Harvey
    Replies:
    5
    Views:
    449
    Scott M.
    Nov 16, 2003
  3. Jan Nielsen
    Replies:
    7
    Views:
    525
    Jan Nielsen
    Feb 8, 2005
  4. Replies:
    3
    Views:
    1,512
  5. John
    Replies:
    157
    Views:
    2,285
    Tim Rentsch
    Feb 23, 2011
Loading...

Share This Page