-2

This is the code I wrote:

 int main()
 {

    int nc;
    nc=0;

    while(getchar()!=EOF)
    {
        ++nc;    
        printf("%i\n",nc);
    }
    return 0;
}

The output lists the number of characters like 1, 2, 3 instead of giving a total count. Removing the curly brackets enclosing the while loop or putting the 'printf' statement outside the loop resulted in no output at all.

Community
  • 1
  • 1
  • You're printing a counter after each character read. The third one should be your 'return'. What output did you expect? – Robert Kock Sep 22 '18 at 13:53
  • Welcome to Stack Overflow. Please read the [About] and [Ask] pages soon. You should show your code in the question — and the output too. Please do not use a link to an image for plain text. Please show your code in the question directly — use the **`{}`** button above the edit box to indent code as code. Your problem is easily fixed when the code is shown; it is impossible to fix while we can't see your code (beyond observing that you are printing within the loop and should be printing outside the loop). – Jonathan Leffler Sep 22 '18 at 13:55
  • Your problem is that you don't know how to produce the *EOF*. – Antti Haapala -- Слава Україні Nov 03 '18 at 06:55

1 Answers1

1

Keep the print statement outside the while loop and you'll get the total no of characters entered finally rather than printed each time. PLUS did you press the buttons to pass EOF ( ctrl + d in linux) so that the loop ends?

Apart from that, use an int to get values from getchar() and change while loop to:

 #include<stdio.h>

 int main(void)
 {
     int i;

     while((i=getchar())!=EOF)
     {
         if(i!='\n')
             ++nc;
     }
     printf("%d\n",nc);
     return 0;
 }

Else you'll get the count of characters 1 more than the actual input due to '\n' pressed at the end.

Gaurav
  • 1,570
  • 5
  • 17
  • Thank you for your answer. However, using int i, results in an error that reads "unused variable i". Further, for some reason, there's another error which read, "variable 'nc' set but not used". – Sara Sethia Sep 23 '18 at 02:00
  • @SaraSethia These are warnings not errors - it is good to go, you just need to print `nc`, – Gaurav Sep 23 '18 at 04:43
  • Thank you so much. I have one more question though. Why does the While Loop require an If statement? I mean, why was it not working without the If statement? – Sara Sethia Sep 23 '18 at 05:21
  • @SaraSethia You can make it work without `if` statement if you pass `EOF` character to end the sentence rather than using newline `\n`. – Gaurav Sep 23 '18 at 07:42
  • Another post regarding this suggested using ctrl+D and even ctrl+Z. Both didn't work in my case. – Sara Sethia Sep 23 '18 at 11:28
  • @SaraSethia what OS are you using to run the code? – Gaurav Sep 23 '18 at 11:29
  • Further, on running the code, I am still getting the count of characters separately as 1,2,3 instead of the total count. Additionally, the count of characters is 1 more than the actual input. – Sara Sethia Sep 23 '18 at 11:42
  • I am using Windows. Is using OS a better option? – Sara Sethia Sep 23 '18 at 11:42
  • @SaraSethia What compiler are you using? I have modified the code - rather than going for `EOF` you can press `ENTER` to end the counting of characters entered. – Gaurav Sep 23 '18 at 11:43
  • I am using Code Blocks – Sara Sethia Sep 23 '18 at 11:45