Thread: do while loop keeps looping

  1. #16
    Registered User
    Join Date
    May 2009
    Posts
    39
    Nope, just pointing out that fact...Allready finished school-good exercise though
    Last edited by ninety3gd; 06-13-2009 at 10:08 PM. Reason: typo

  2. #17
    Registered User
    Join Date
    Jun 2009
    Posts
    9
    while (digit = '\0') wrong XXXXXXXXXXXXXXX
    digit is an int. value and '\0' is a character so loop will be infinite , second u aren't comparing u are assigning values , i see that the condition should be digit==0,and in if their is ma missing statement that is printf("product is 0");

  3. #18
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by mostafa faisal View Post
    while (digit = '\0') wrong XXXXXXXXXXXXXXX
    digit is an int. value and '\0' is a character so loop will be infinite
    No, you are wrong. The decimal value of '\0' is 0, and "digit" could equal 0. Test it yourself and see the ASCII table. You will notice the first entry in that table is "null"; that is the byte value used as the "null terminator" ('\0') in C.

    You are right about the ==.
    Last edited by MK27; 06-14-2009 at 09:03 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #19
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    '\0' is an int. Character literals in C are ints.

  5. #20
    Registered User
    Join Date
    Jun 2009
    Posts
    53
    Hi, Im experiencing a slight issue, this maybe what you guys were discussing. The loop works nicely except that when two zeros are multiplied the loop breaks. Is "digit != '\0'" not the correct condition for this?

  6. #21
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by wankel View Post
    Hi, Im experiencing a slight issue, this maybe what you guys were discussing. The loop works nicely except that when two zeros are multiplied the loop breaks. Is "digit != '\0'" not the correct condition for this?
    Well, it's the right condition for "stop when the product is 0". As you noticed, that doesn't necessarily mean you're done with the problem. You need to stop when the number you're multiplying by has been reduced to 0.

  7. #22
    Registered User
    Join Date
    Jun 2009
    Posts
    53
    Quote Originally Posted by tabstop View Post
    Well, it's the right condition for "stop when the product is 0". As you noticed, that doesn't necessarily mean you're done with the problem. You need to stop when the number you're multiplying by has been reduced to 0.
    that makes a lot of sense, so I changed it from "digit" to "number2", but nothings changed?

    example:

    101
    x101
    -----
    101
    -----
    10201


    when its supposed to be:

    101
    x101
    ------
    101
    10100
    ------
    10201

    edit: nevermind, im so dumb. got it solved.

    Last question, im having it display the product only if its not the final answer or a zero, so I have this condition:

    if (product != final_product || product != 0)
    {
    print product
    }

    however, only the first part of that condition works. zeros are still displayed. Is the formatting wrong? Im not getting any errors when im compiling it
    Last edited by wankel; 06-15-2009 at 04:44 PM.

  8. #23
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Try AND.


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

  9. #24
    Registered User
    Join Date
    Jun 2009
    Posts
    53
    Quote Originally Posted by quzah View Post
    Try AND.


    Quzah
    Thanks Quzah, that did the trick. You guys all rock, thank you so much for all the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 12-01-2008, 10:09 AM
  2. return to start coding?
    By talnoy in forum C++ Programming
    Replies: 1
    Last Post: 01-26-2006, 03:48 AM
  3. loop needed also how to make input use letters
    By LoRdHSV1991 in forum C Programming
    Replies: 3
    Last Post: 01-13-2006, 05:39 AM
  4. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM
  5. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:15 AM