Like Tree1Likes
  • 1 Post By stahta01

C In a Unix Environment - Small Program Help

This is a discussion on C In a Unix Environment - Small Program Help within the C Programming forums, part of the General Programming Boards category; Hi I was wondering if I could get a little help with some programs I'm currently making. The first is ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    22

    C In a Unix Environment - Small Program Help

    Hi

    I was wondering if I could get a little help with some programs I'm currently making.

    The first is a program designed to calculate BMI by inputting in stones and pounds and converting to kg and metres.

    This is what i have so far but unsure as to why it isn't working.....

    Code:
    #include <stdio.h>
    int main() { 
    int Feet;
    int Inches;
    int Stones;
    int Pounds;
    double Kilo = 0;
    double Meter = 0;
    char BMI = 0;
    printf("Please enter your height in UK Feet and Inches: "); 
    scanf("%d %d", &Feet, &Inches);
    printf("Please enter your weight in Stones and Pounds: "); 
    scanf("%d %d", &Stones, &Pounds);
    Kilo =((Stones *14)*0.4536)+(Pounds*0.4536);
    Meter = ((Feet * 0.3048) + (Inches * 25.4));
    BMI = (Kilo / ( Meter * Meter) );
    printf("%c", BMI);
    return 0;
    }
    My other program is designed to calculate an area of a shape.(prompting user input first) This program doesn't compile and won't run in Cygwin....any suggestions on the error in my code?

    Code:
    #include <stdio.h>
    int main(){
    float Rlength, Rwidth, Slength, Swidth, Cradius, AOR, AOC, AOS;
    char Sha;
    char R, S, C;
    printf ("\nArea of Shapes Calculator");
    printf ("\nPlease enter for calculation, S=Square, R=Rectangle, C=Circle");
    scanf  ("%c", &Sha);    
    if (Sha == 'R')
            printf ("\nEnter the Length and width of the Rectangle:");
            scanf("%f,%f", &Rlength, &Rwidth);
            AOR = Rlength * Rwidth;
            printf("%f\n", AOR);
            endif;
    if (Sha =='S')
            printf ("\nEnter the length and width of the Square:");
            scanf ("%f,%f", &Slength, &Swidth);
            AOS = Slength * Swidth;
            printf("%f\n", AOS);
            endif;
    if(Sha == 'C')
           printf("\nEnter the Radius of the Circle:");
            scanf("%f", &Cradius);
            AOC = 3.141 * Cradius * Cradius;
            printf("%f\n", AOC);
            endif;
    return 0;
    }

    I would appreciate any help as i'm fairly new to C!

    Thanks

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,478
    Second program; there is no "endif" in C programming (outside of the preprocessor)

    If Statements in C - Cprogramming.com

    Just a guess; but, it is very unlikely that BMI is supposed to be a char.
    You NEED to state the problem; What is NOT working right in program 1.
    Code:
    printf("%c", BMI);
    Tim S.
    Last edited by stahta01; 02-19-2012 at 02:11 PM.
    rags_to_riches likes this.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    22
    Quote Originally Posted by stahta01 View Post
    Second program; there is no "endif" in C programming (outside of the preprocessor)

    If Statements in C - Cprogramming.com

    Just a guess; but, it is very unlikely that BMI is supposed to be a char.
    You NEED to state the problem; What is NOT working right in program 1.
    Code:
    printf("%c", BMI);
    Tim S.
    "Please enter your height in UK Feet and Inches: 12 12
    Please enter your weight in Stones and Pounds: 12 12"

    When i run the BMI program the above 2 lines are executed. I have typed in the 12 as an example. The program then stops, and doesn't output a BMI value.....I don't know whats wrong, but that what is being produced by that code.

    Thanks

  4. #4
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,648
    Repeating stahta1:

    it is very unlikely that BMI is supposed to be a char.

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    22
    Quote Originally Posted by rags_to_riches View Post
    Repeating stahta1:
    Yeh agreed and changed. The program seems to be working and outputting correctly.....however the value being outputted isn't correct. I honestly can't see which sum is wrong.... values being outputted range high 70's when realistic values should be between 19 and 30. Any suggestions?

    Thanks (Below is the new code)

    Code:
    #include <stdio.h>int main() { 
    int Feet;
    int Inches;
    int Stones;
    int Pounds;
    float Kilo, Meter, BMI;
    printf("Please enter your height in UK Feet and Inches: "); 
    scanf("%d %d", &Feet, &Inches);
    printf("Please enter your weight in Stones and Pounds: "); 
    scanf("%d %d", &Stones, &Pounds);
    Kilo =((Stones * 14)*0.4536)+(Pounds*0.4536);
    Meter =((Feet * 0.3048) + (Inches * 2.54));
    BMI = (Kilo /  Meter * Meter );
    printf("Your BMI is: %f\n", BMI);
    return 0;
    }

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    The BMI formula is body mass / (height * height). At the moment, you are computing (body mass / height) * height.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Feb 2012
    Posts
    22
    Code:
     BMI = (Kilo /(Meter * Meter));
    - I've made this alteration, but the BMI values being outputted are very strange. They are completely inaccurate and very random, considering small changes to the input values. Thanks for the replies! Really stuck on this now....

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    Use your debugger and check the values that you are working with. Do you find anything strange about the computed values of Kilo and/or Meter? Could you have forgotten something in a formula?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Feb 2012
    Posts
    22
    Quote Originally Posted by laserlight View Post
    Use your debugger and check the values that you are working with. Do you find anything strange about the computed values of Kilo and/or Meter? Could you have forgotten something in a formula?
    Hmmmm. Where do i find my debugger? I'm currently using two printf's for both calculations. from what i can gather my problem lies within my height calculation, sometimes it gives me a value that is correct, but one number out and it can give me an output value of nearly number!! haha it's driving me crazy, thanks for the responses.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    Heheh. Okay, let's examine your formula:
    Code:
    Meter =((Feet * 0.3048) + (Inches * 2.54));
    Now, I am 0 feet and 1 inch tall. Therefore, I am 2.54 metres tall! Yay!

    Quote Originally Posted by cajones1990
    Where do i find my debugger?
    It depends. Are you using an IDE?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Feb 2012
    Posts
    22
    haha i've finally sorted it! of course it has to match either side in terms of both being in cm or metres.
    Code:
     Meter =(Feet * 0.3048)+(Inches*0.0254);
    That seems to work fine and gives me the correct output. Thanks for your help. Is it possible to ask a few more questions?

    I'm required to develop a program that acts as a cash dispenser. So for example i input an integer of 88, and it will of course output -
    1 x 50
    1 x 20
    1 x 10
    1 x 5
    1 x 2
    1 x 1

    It uses the biggest denomination first. No idea where to start. Can you put me in the right direction?

    Thanks

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    I suggest that you start a new thread

    Also, search the Web for "greedy algorithm".
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-13-2009, 03:25 PM
  2. Files in Unix environment.
    By kwikness in forum C Programming
    Replies: 3
    Last Post: 10-06-2007, 04:12 PM
  3. A small problem with a small program
    By Wetling in forum C Programming
    Replies: 7
    Last Post: 03-25-2002, 08:45 PM
  4. Windows or Unix environment
    By ghe1 in forum Windows Programming
    Replies: 5
    Last Post: 02-19-2002, 10:37 AM
  5. Path / Environment in unix
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 10-10-2001, 04:52 AM

Tags for this Thread


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