Thread: aughhhhhhhhhhhhhhhhhhhh!!! (program problems)

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    48

    help!!! (program problems)

    5. Write a program that determines the number of digits in a number:

    Enter a number: 374
    The number 374 has 3 digits

    You may assume that the number has no more than 4 digits. HINT: Use if statements to test the number. For example, if the number is between 0 and 9, it has one digit, in between 10 and 99 = 2 digits, etc.

    ... i havent been able to get anything right for this code except:

    /* Program that determines the number of digits in a number */
    /* DATE: 07-22-02 */

    #include <stdio.h>

    int main()
    {
    int number;

    printf("Enter a number: ");
    scanf("%d", & number);

    ... here's where it gets loco

    if (0>= number>= 9);
    printf("The number %d has 1 digit", number);


    .... am i going ab this totally wrong or what?
    helppppppppppppp!!!
    Last edited by JohnMayer; 07-22-2002 at 05:29 PM.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: aughhhhhhhhhhhhhhhhhhhh!!! (program problems)

    Originally posted by JohnMayer
    5. Write a program that determines the number of digits in a number:

    Enter a number: 374
    The number 374 has 3 digits

    You may assume that the number has no more than 4 digits. HINT: Use if statements to test the number. For example, if the number is between 0 and 9, it has one digit, in between 10 and 99 = 2 digits, etc.
    If you have to use the if statements, as suggested by the hint, it's going to be a bit lame, there are better ways. Just keeping dividing the number by 10, counting the number of times you can do it..... I'll leave the exact details for you to work out.

    [EDIT]And please use the subject field for something constructive, other than venting your anger!
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    You may want to write valid statements.
    if (0>= number>= 9);
    if(number >= 0 && number <= 9)
    ...
    The divide by 10 method is a good way to go. However, with a limitation of 4 digits, I'm sure this problem was meant to get you into thinking about the uses of the if statement.

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    48
    thanks hammer & cshot (cshot, your advice ab the && helped... i didn't know you had to write out number twice!!! but thanks so much you guys~!!!!

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Hehehe...

    sprintf( buf, "%d", x );

    Or, if it's being read directly into a buffer, you can omit that.
    Code:
    if( strlen( buffer ) < 2 )
        printf("%d is one digit in length.\n", x );
    else
    if( strlen( buffer ) < 3 )
        printf("%d is two digits in length.\n", x );
    else
    if( strlen( buffer ) < 4 )
        printf("%d is three digits in length.\n", x );
    else
        printf("%d", is four digits in length.\n"), x );
    There ya go. You've used if statements.

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

  6. #6
    Registered User
    Join Date
    Jul 2002
    Posts
    48
    awwww, thanx Quzah~! you guys def made my day with helping me find the problem with this program.... btw, what languages do you guys know (and what class(es) for comp sci are you guys enrolled at the undergrad/grad level this fall?

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >what languages do you guys know
    English, a little French, bull....

    >(and what class(es) for comp sci are you guys enrolled at the undergrad/grad level this fall?
    Yeah, right, like I go to school ahhh, those were the days......
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User
    Join Date
    Jul 2002
    Posts
    48
    Hammer-
    so you're done with undergrad and/or grad? (i'm guessing you have a comp sci related job then~!

  9. #9
    Registered User moonwalker's Avatar
    Join Date
    Jul 2002
    Posts
    282

    i was thinking....

    how can we make this code work for any number of digits ??

    i was thinking... we can consider only integers...
    input the whole thing as a char array.. and then
    the length is
    strlen(thatarray) - 1

    and first, we'll check and eliminate all the 0s in the beginning..

    for example... if someone enters 007,
    it deletes 00
    takes strlen(array) which is 2 ('cause there's 7 and '\0') and then -1 of that.. which is 1 .. which is the number of significant digits..

    is there another way to do this ?

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There is no need to trim the leading zeros. If you use:

    sprintf( buf, "%d", value );

    Assuming that 'value' is an integer, it will have no preceeding zeros. Additionally, the length will be 'strlen' length, not 'strlen-1'.

    There is also a way to do it with a loop by repeating the division process. Divide by ten until there is no remainder, or until the remainder is less than 10. The number of divisions + 1 is the number of digits.

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

  11. #11
    Registered User moonwalker's Avatar
    Join Date
    Jul 2002
    Posts
    282
    Originally posted by quzah
    Divide by ten until there is no remainder, or until the remainder is less than 10. The number of divisions + 1 is the number of digits.
    How do we do this ? we can't use number%10 ....
    then how do we know the remainder ?

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How do we do this ? we can't use number%10 ....
    then how do we know the remainder ?
    What's wrong with people? Doesn't anyone know how to try something on their own first? **** man, how long does it take to throw some test code together:

    Code:
    #include <stdio.h>
    int main ( void )
    {
        int x = 1234,y,z;
        printf("%d / 10 = %d, %d % 10 = %d", x, y=x/10, x, z=x%10 );
    
        return 0;
    }
    Stick it in a loop and play around with it. Arrrg. I'm so freeking tired of every one wanting everything handed to them.

    The whole point of programming is so you can figure out how to solve a problem. How is me giving you the freeking answer helping you learn problemsolving?

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

  13. #13
    Registered User moonwalker's Avatar
    Join Date
    Jul 2002
    Posts
    282

    hey..

    Originally posted by quzah


    What's wrong with people? Doesn't anyone know how to try something on their own first? **** man, how long does it take to throw some test code together:

    The whole point of programming is so you can figure out how to solve a problem. How is me giving you the freeking answer helping you learn problemsolving?

    Quzah.
    Watch your language pal

    There's also another reason (besides getting their code done by someone else) why people might ask questions, it is because they can't figure out how.

    if you're not willing to help me, then don't!!! but never assume i've never tried and never use bad language.. remember, it's as easy for me to type the four letter words as it is for you.
    but we all know it is not a good habit, don't we ?

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    but never assume i've never tried and never use bad language.. remember, it's as easy for me to type the four letter words as it is for you.
    Personally I don't give a **** what you say. Nothing anyone can say or type to me can possibly offend me, because people are irrelevant.

    I show up, I answer questions. (Over and over and over, the exact same thing, all day long! Because no one can read a FAQ or use a search engine or do virtually anything on their own.) I help people who help themselves first.

    I am not a babysitter. I am not here to lead people by the hand and do everything for them. I present a solution to a problem with enough information that any but the brain dead should be able to figure out the answer.

    As for your not knowing how, what exactly is it you don't know?

    You don't know how to divide?
    You don't know how to use the % operator?
    You don't know how to use printf?
    You don't know how to create a variable?

    I mean give me a break, these have got to be the most basic steps in programming there are. Surely your book has covered them by know?

    If not, then what on earth are you trying to use a loop for? I mean, that must just blow your feeble mind.

    As for telling me not to use "bad language". Get real. You have no rule over me. I'll do and say whatever I please. After all, I happen to live in a supposedly free country. And that aside, manners are for people who actually care what others think of them.

    I don't care what any of you think.

    Quzah.
    ... Cranky bastard extraordinare.
    Last edited by quzah; 07-22-2002 at 10:26 PM.
    Hope is the first step on the road to disappointment.

  15. #15
    Registered User moonwalker's Avatar
    Join Date
    Jul 2002
    Posts
    282

    shhh...

    Originally posted by quzah


    Personally I don't give a **** what you say. Nothing anyone can say or type to me can possibly offend me, because people are irrelevant.

    I show up, I answer questions. (Over and over and over, the exact same thing, all day long! Because no one can read a FAQ or use a search engine or do virtually anything on their own.) I help people who help themselves first.

    I am not a babysitter. I am not here to lead people by the hand and do everything for them. I present a solution to a problem with enough information that any but the brain dead should be able to figure out the answer.

    As for your not knowing how, what exactly is it you don't know?

    You don't know how to divide?
    You don't know how to use the % operator?
    You don't know how to use printf?
    You don't know how to create a variable?

    I mean give me a break, these have got to be the most basic steps in programming there are. Surely your book has covered them by know?

    If not, then what on earth are you trying to use a loop for? I mean, that must just blow your feeble mind.

    As for telling me not to use "bad language". Get real. You have no rule over me. I'll do and say whatever I please. After all, I happen to live in a supposedly free country. And that aside, manners are for people who actually care what others think of them.

    I don't care what any of you think.

    Quzah.
    ... Cranky bastard extraordinare.
    Now this is definitely my mistake for letting you so go far..
    Pride goes before a fall.
    I give up teaching ethics to idiots.
    Thanks a ton for the guy who invented the ignore button!
    Last edited by moonwalker; 07-22-2002 at 10:46 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Problems with DLLEXPORT while updating a program
    By pirata in forum C++ Programming
    Replies: 3
    Last Post: 09-05-2008, 01:00 PM
  3. having problems with my card program
    By mac025 in forum C Programming
    Replies: 4
    Last Post: 01-31-2006, 04:26 PM
  4. structure problems in windows program.
    By Bajanine in forum Windows Programming
    Replies: 3
    Last Post: 04-19-2004, 06:18 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM