noob need help with programming question!

This is a discussion on noob need help with programming question! within the C Programming forums, part of the General Programming Boards category; hey im doing this program for school and its about a user entering their height in centimeters and output it ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    9

    noob need help with programming question!

    hey im doing this program for school and its about a user entering their height in centimeters and output it as height in feet and inches im getting the problem when getting the remainder of the feet to be changed into inches and the compiler said that i have an invalid % operand and i know it has to do with the remainder of the feet thing but i just dont know how to do it.



    Code:
    /* This program converts height in centimeters and 
    displays it into height in centimeters, feet, inches.*/
    
    
    #include<stdio.h>
    const double CENT_FEET=0.033;
    const double CENT_INCHES=0.30;
    int main(void) {
    
         int cent, centf, centi, centl;
    
         printf("COnvert Height Into Feet & Inches!\n");
         printf("Please Enter Your Height In Centimeters:    (<=0 to quit\n");
         scanf("%d", &cent);
         while (cent>0)
    {
         centf=(int) cent*CENT_FEET;
         centi=(int) cent % CENT_FEET*CENT_INCHES;   
    
         printf("%d cm= %.2d feet, .2%d inches\n", cent, centf, centi);
         printf("Enter Another Height to COntinue:    (<=0 to quit)\n"); 
         scanf("%d", &cent);
    }
         printf("BYE!\n");
         return 0;
    }
    /*----------------------------------------------------------------------*/

  2. #2
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    % operator gives you the remainder
    like
    9%7=2
    Can you find me the remainder of 5 when divided by say 1.4.
    I hope you got my point
    Long time no C. I need to learn the language again.
    Help a man when he is in trouble and he will remember you when he is in trouble again.
    You learn in life when you lose.
    Complex problems have simple, easy to understand wrong answers.
    "A ship in the harbour is safe, but that's not what ships are built
    for"

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    9
    i understand that part right but its to get the actualy value of the remainder to be converted to inches ..i dunno if my code is wron glike fom the declaration of from the type of variable i used

  4. #4
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    If you include math.h in your header you can find the fractional part of cent*CENT_FEET using floor. Then using that fractional part, and knowing there are twelve inches in a foot, you can find the amount of inches that fractional part represents. And presto, you're done.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    9
    Code:
    /* This program converts height in centimeters and 
    displays it into height in centimeters, feet, inches.*/
    
    
    #include<stdio.h>
    #include<math.h>
    const double CENT_FEET=0.033;
    const double CENT_INCHES=0.30;
    int main(void) {
    
         int cent, centf, centi, centl;
    
         printf("COnvert Height Into Feet & Inches!\n");
         printf("Please Enter Your Height In Centimeters:    (<=0 to quit\n");
         scanf("%d", &cent);
         while (cent>0)
    {
         centf=(int) floor(cent*CENT_FEET);
         centi=(int) (cent - floor(centf*CENT_FEET))*CENT_INCHES;  
    
         printf("%d cm= %.2d feet, .2%d inches\n", cent, centf, centi);
         printf("Enter Another Height to COntinue:    (<=0 to quit)\n"); 
         scanf("%d", &cent);
    }
         printf("BYE!\n");
         return 0;
    }
    /*----------------------------------------------------------------------*/
    it looks like this now but it still wont work itsays that undefined reference to floor isnt that the right header file thoguh or did i do something wrong...

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Do you need to link with the math library? Does your program compile? It looks good to me.

    Look into a do-while loop; it would reduce the repeated code you have.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    9
    i dont think i need the math library but i don really know ho wto do the odo while loop yet can u help me there?

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Okay. A do-while loop is the same as a while loop, except the condition is executed at the end of the loop, not at the start.
    Code:
    printf("*");
    while(x) {
        printf("*");
        x --;
    }
    is the same as
    Code:
    do {
        printf("*");
        x --;
    } while(x);
    A do-while loop is usually used when the code has to execute at least once.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Your code looks good, except for the unreferenced variable centl, and the "%.2d" in printf().

    [edit]
    And the ".2%d", too. Make them regular "%d"s.
    [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User
    Join Date
    Nov 2005
    Posts
    9
    arite thanx

  11. #11
    Registered User
    Join Date
    Nov 2005
    Posts
    9
    it still says undefined reference to floor i dunno why

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    What compiler are you using?

    If floor() doesn't work, a cast to int does the same thing:
    Code:
    double d = 1.99;
    printf("%f\n", floor(d));
    and
    Code:
    double d = 1.99;
    printf("%f\n", (double)((int)d));
    Output:
    Code:
    1.000000
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    Registered User
    Join Date
    Nov 2005
    Posts
    9
    thanx i got it now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. quick noob question
    By thanatos1 in forum C# Programming
    Replies: 2
    Last Post: 06-17-2009, 08:28 PM
  2. another noob question
    By clb2003 in forum C Programming
    Replies: 4
    Last Post: 02-12-2009, 12:28 PM
  3. Noob printf question
    By lolguy in forum C Programming
    Replies: 3
    Last Post: 12-14-2008, 07:08 PM
  4. Very noob question :(.
    By GamerProduction in forum Tech Board
    Replies: 4
    Last Post: 04-14-2007, 05:40 AM
  5. Noob question ( little dos program )
    By Demon1s in forum C++ Programming
    Replies: 13
    Last Post: 04-04-2003, 08:28 PM

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