Loops keep printing 0

This is a discussion on Loops keep printing 0 within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char words[200]; int d,i,k=0; while(scanf("%s",words)==1){ k=strlen(words); d=0; for(i=0; i<k; i++) ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    6

    Loops keep printing 0

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main()
    {
        char words[200];
        int d,i,k=0;
        while(scanf("%s",words)==1){
        k=strlen(words);
        d=0;
         for(i=0; i<k; i++)
        {
         if(words[i]=='\0')
         {
         d++;
    
         }
        }
        printf("%d",d);
        }
        return 0;
    }
    This code is made to count words. But instead of displaying numbers of words, it prints 0.
    =_=

    eg;
    input : hi my name is bob!

    instead of
    output: 4(Words)

    it outputs 0000

    Much help needed. Extremely newbie programmer.
    Last edited by kaopei; 12-02-2010 at 06:14 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You need to edit that and indent your code. You set d to 0 at the start of each iteration of the while loop.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    6
    I put it inside the while loop to rest the counter everytime a new input is entered.

    @_@ it keeps giving a

    0000
    output for a supposed to be
    4
    and it doesnt print only one 0. it prints multiple 0s for what is supposed to be an integer =_=. eg:

    000 for 3 .. smth like tht

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why do you want the counter reset? Why are you trying to print it more than once, if you just want it to print once? Think about what's happening each time the loop goes through.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    6
    Since i'm planning for the input to be entered multiple times. Thats why i needed to reset the value to 0.. If i don't it will take up the last value from the previous cycle.

    That's not the problem though..the problem's in the output.. I tried changing the d to 1. It ended up printing multiple 1s.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Of course it prints multiple times:
    Code:
        while(scanf("%s",words)==1){
            d=0;
            ... other stuff ...
            printf("%d",d);
        }
    Every time it passes through your while loop, it sets it to 0, does stuff, and prints whatever it is. If you only want it to print once, then it needs to be out of your loop, and you need to stop resetting the value to 0 every time.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,451
    Are you trying to count whole words (i.e. chunks of letters/numbers separated by white space) or individual characters? Your example input of 0000 and output of 4 suggests you're counting characters. What should my output be for the input "Hello world!"? How about "This is a test"?

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by kaopei View Post
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main()
    {
        char words[200];
        int d,i,k=0;
        while(scanf("%s",words)==1){
        k=strlen(words);
        d=0;
         for(i=0; i<k; i++)
        {
         if(words[i]=='\0')
         {
         d++;
    
         }
        }
        printf("%d",d);
        }
        return 0;
    }
    This code is made to count words. But instead of displaying numbers of words, it prints 0.
    You are testing for words[i]==\0 you should be looking for spaces...
    It's a simple thing to do, count the spaces and add 1...

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    xian china
    Posts
    31
    what do you want to print out?
    d is counting number of '\0' in each word

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    Maybe you should start with
    Code:
    while(scanf("%s",words)==1) {
      printf("The word is ==%s==\n", words);
    }
    To learn what scanf %s actually gives you for a line of input.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Jesius View Post
    what do you want to print out?
    d is counting number of '\0' in each word
    If I gave you a string with 10 words in it, how many 0s would it have?

  12. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,451
    Quote Originally Posted by CommonTater View Post
    You are testing for words[i]==\0 you should be looking for spaces...
    It's a simple thing to do, count the spaces and add 1...
    True, assuming there is exactly 1 space between each word (and no punctuation without spaces). Look into strtok if you want, or if you want to roll your own, set a flag to designate whether you're in a word or in between words. Initialize it to off, turn it on when you hit your first letter. When you hit a non-letter or the end of string, turn your flag off and increment your word count.

    Quote Originally Posted by Jesius View Post
    what do you want to print out?
    d is counting number of '\0' in each word
    Actually, it doesn't count the number of null characters. It doesn't count anything at all because the program can never reach the line of code that increments d. If you read carefully, k is equal to strlen(words), and the loop checks for for i = 0 to i < k. The null byte is at words[k], but i is never equal to k in that loop, it ends at k-1. So the code doesn't count anything.

  13. #13
    Registered User
    Join Date
    Nov 2010
    Posts
    6
    If I gave you a string with 10 words in it, how many 0s would it have?
    10 zeros.. oddly.

    You are testing for words[i]==\0 you should be looking for spaces...
    It's a simple thing to do, count the spaces and add 1...
    >_< i thought the \0 stands for blank characters(Spaces?)... i changed it to ASCII dec 32.. but it still can't detect the spaces


    Actually, it doesn't count the number of null characters. It doesn't count anything at all because the program can never reach the line of code that increments d. If you read carefully, k is equal to strlen(words), and the loop checks for for i = 0 to i < k. The null byte is at words[k], but i is never equal to k in that loop, it ends at k-1. So the code doesn't count anything.
    so i have to make it at i<=k right? >_< i see my bad..

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by kaopei View Post
    10 zeros.. oddly.
    Actually it would have only one, at the end, where strlen() won't take you.

    >_< i thought the \0 stands for blank characters(Spaces?)... i changed it to ASCII dec 32.. but it still can't detect the spaces
    Nope...' \n' stands for 0... you want ' '... which will look at spaces.
    i.e. when looking for spaces, actually look for spaces.

    so i have to make it at i<=k right? >_< i see my bad..
    Nope... you had that much right.

  15. #15
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    You are totally ignoring the root cause.
    Read what salem had posted.
    scanf %s stops reading at space.
    So If you entered "hello world", scanf() will return when encountered space and you'll only have "hello". next scanf() call will give you "world".
    If your definition of word is group of characters separated by space, each scanf() call gives you one word....
    Last edited by Bayint Naung; 12-03-2010 at 09:17 AM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C# Printing Problem
    By silverlight001 in forum C# Programming
    Replies: 0
    Last Post: 03-23-2009, 01:13 AM
  2. generic printing preferences dialog box
    By stanlvw in forum Windows Programming
    Replies: 8
    Last Post: 06-27-2008, 02:20 AM
  3. printing data to a file
    By coralreef in forum C Programming
    Replies: 3
    Last Post: 11-02-2006, 07:10 PM
  4. need help relating printing?
    By omarlodhi in forum Linux Programming
    Replies: 0
    Last Post: 03-03-2006, 03:46 AM
  5. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 03:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21