Thread: Going further in C + Comments on simple program

  1. #1
    Registered User doCode()'s Avatar
    Join Date
    Feb 2015
    Location
    Le Universe
    Posts
    13

    Cool Going further in C + Comments on simple program

    Hey there everyone!
    I'm quite new to the C language. I have finished a book that teaches the C basics (syntax and basic concepts.)
    I'm not sure what to do now, but I did read you have to read other people's source code, and things like that.
    But I'm not sure where to start.

    I'de be grateful if you'd point out the right direction to take.

    I uploaded a simple terminal program I coded that calculates BMI.

    Upd8 Okey dokey laserlight!

    Code:
    //
    //  main.c
    //  bmiCalc
    //
    //  Created by Ishraq on 2/27/15.
    //  Copyright (c) 2015 Ishraq. All rights reserved.
    //
    
    
    #include <stdio.h>
    #include <string.h>
    #include <unistd.h>
    #include <ctype.h>
    #include <stdbool.h>
    
    
    int main(int argc, const char * argv[]) {
        
        char  unitChoiceWeight;
        char  unitChoiceHeight;
        float weight;
        float height;
        float bmi;
        
        printf("\nİ Ishraq ( doCode() )");
        printf("\n\n| __BMI Calculator__ |\n\n");
        
        sleep(1);
        
        printf("Choose which unit you want to enter your _weight_ in from the following list.\n\n");
        printf("________________________________________________\n\n");
        printf("Kilograms (kg) - Type in _'kg'_ for kilograms\n");
        printf("Pounds    (lb) - Type in _'lb'_ for pounds\n");
        printf("Stones    (st) - Type in _'st'_ for stones\n");
        printf("________________________________________________\n\n");
        
        sleep(2);
        
        printf("Enter your choice here > ");
            
        scanf("%s", &unitChoiceWeight);
    
    
        if (!strcasecmp(&unitChoiceWeight, "kg")) {
            printf("You have chosen kilograms.\n\n");
        } else if (!strcasecmp(&unitChoiceWeight, "lb")) {
            printf("You have chosen pounds.\n\n");
        } else if (!strcasecmp(&unitChoiceWeight, "st")) {
            printf("You have chosen stones.\n\n");
        } else {
            printf("You have entered an invalid value.");
            return 0;
        }
        
        printf("Now, enter your _weight_ here > ");
        scanf(" %f", &weight);
        
        if (!strcasecmp(&unitChoiceWeight, "lb")) {
            printf("You claim to weigh %2.f lb, which is ", weight);
            weight /= 2.2046;
            printf("%.2f kg. \n\n\n", weight);
        } else if (!strcasecmp(&unitChoiceWeight, "st")) {
            printf("You claim to weigh %.2f st, which is ", weight);
            weight = (weight*14)/2.2046;
            printf("%.2f kg. \n\n\n", weight);
        } else {
            printf("You claim to weigh %.2f kg.\n\n\n", weight);
        }
    
    
        
        sleep(1);
        
        printf("Choose which unit you want to enter your _height_ in from the following list.\n\n");
        
        sleep(2);
        
        
        sleep(1);
        printf("________________________________________________\n\n");
        printf("Meter       (m)  - Type in _'m '_ for meters\n");
        printf("Inch        (in) - Type in _'in'_ for inches\n");
        printf("Foot        (ft) - Type in _'ft'_ for feet\n");
        printf("Millimeter  (mm) - Type in _'mm'_ for millimeters\n");
        printf("________________________________________________\n\n");
        sleep(1);
        
        printf("Enter your choice here > ");
        
        scanf("%s", &unitChoiceHeight);
        
        if (!strcasecmp(&unitChoiceHeight, "m")) {
            printf("You have chosen meters.\n\n");
        } else if (!strcasecmp(&unitChoiceHeight, "in")) {
            printf("You have chosen inches.\n\n");
        } else if (!strcasecmp(&unitChoiceHeight, "ft")) {
            printf("You have chosen feet.\n\n");
        } else if (!strcasecmp(&unitChoiceHeight, "mm")){
            printf("You have chosen millimeters.\n\n");
        } else {
            printf("You have entered an invalid value.");
            return 0;
        }
        
        printf("Now, enter your _height_ here > ");
        scanf(" %f", &height);
        
        
        if (!strcasecmp(&unitChoiceHeight, "in")) {
            printf("You claim to have a height %2.f in, which is ", height);
            height /= 39.370;
            printf("%.2f m. \n\n\n", height);
        } else if (!strcasecmp(&unitChoiceHeight, "ft")) {
            printf("You claim to have a height %.2f ft, which is ", height);
            height /= 3.2808;
            printf("%.2f m. \n\n\n", height);
        }else if (!strcasecmp(&unitChoiceHeight, "mm")) {
            printf("You claim to have a height %.2f mm, which is ", height);
            height /= 1000.0;
            printf("%.2f m. \n\n\n", height);
        } else {
            printf("You claim to have a height of %.2f m.\n\n\n", height);
        }
        sleep(1);
      
        for (int flair = 0; flair < 3; flair++) {
            printf("Processing your BMI...\n");
            sleep(1);
        }
        
        printf("\n");
        
        printf("And your BMI is...");
        sleep(1);
        bmi = (weight) / (height*height);
        sleep(1);
        printf("%f!\n\n", bmi);
        
        if (bmi <= 18.5) {
            printf("You are underweight.\n\n");
        } else if ((bmi >= 18.8) && (bmi <= 25)) {
            printf("You have a normal weight.\n\n");
        } else if ((bmi >= 25.5) && (bmi <= 30)) {
            printf("You are overweight.\n\n");
        } else if (bmi >= 30) {
            printf("You are obese.\n\n");
        }
        
        
    }
        
    
    
    
    
    //      Give weight in kilograms
    ///         (divided by)
    //      Height in meters to the power 2
    //Underweight = <18.5
    //Normal weight = 18.5–24.9
    //Overweight = 25–29.9
    //Obesity = BMI of 30 or greater
    The attached file can be opened using the terminal.
    Attached Files Attached Files
    Last edited by doCode(); 02-28-2015 at 12:04 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If your program is small and simple enough, I suggest that you post it directly within [code][/code] bbcode tags as people may not be so inclined to download and open your zip archive.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
        if (bmi <= 18.5) {
            printf("You are underweight.\n\n");
        } else if ((bmi >= 18.8) && (bmi <= 25)) {
            printf("You have a normal weight.\n\n");
        } else if ((bmi >= 25.5) && (bmi <= 30)) {
            printf("You are overweight.\n\n");
        } else if (bmi >= 30) {
            printf("You are obese.\n\n");
        }

    An BMI of 30 has no clear meaning in you program.
    And, I have no idea what you want 25.1 thru 25.4 to be.

    I suggest using constants or defines instead of literal numbers for the BMI values.


    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    Registered User doCode()'s Avatar
    Join Date
    Feb 2015
    Location
    Le Universe
    Posts
    13
    I used this website as reference.

    Calculate Your BMI - Standard BMI Calculator

    I suggest using constants or defines instead of literal numbers for the BMI values.
    Will consider.

    ....and the right direction to take?

    I wish to make software applications, and the likes.
    E.g. A program that makes a bootable Linux USB. I wanted to try this one, but it is too advanced for me, at this stage.
    Last edited by doCode(); 02-28-2015 at 12:36 PM.

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    The best way to learn is to practice, and to keep learning new techniques, functions, ect. Work your way through these tutorials and take a look at this site.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by doCode() View Post
    I wish to make software applications, and the likes.
    E.g. A program that makes a bootable Linux USB. I wanted to try this one, but it is too advanced for me, at this stage.
    You probably know this, but Linux programs are generally published with source code so you can read them. This includes the popular programs that write the bootloaders (e.g. Grub).

    There is an online manual called "Linux from Scratch" which provides step-by-step instructions of how to build a functioning, bootable Linux system using only the source code. If this topoic is interesting to you it might be worth a look.

  7. #7
    Registered User doCode()'s Avatar
    Join Date
    Feb 2015
    Location
    Le Universe
    Posts
    13
    Thanks AndrewHunter!
    That Eternally Confuzzled - Home site looks good!

    c99tutorial
    , I meant a program like UNetbootin - Homepage and Downloads and https://rufus.akeo.ie/.
    That LFS Project is interesting, will be starting soon.

    And if it will do any good, I am using OS X 10.9.5.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by doCode()
    That Eternally Confuzzled - Home site looks good!
    It is the homepage of Prelude, one of the members here (though admittedly one who has not posted for quite some time)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User doCode()'s Avatar
    Join Date
    Feb 2015
    Location
    Le Universe
    Posts
    13
    E.g. A program that makes a bootable Linux USB. I wanted to try this one, but it is too advanced for me, at this stage.
    Any ideas on this, laserlight?
    Last edited by doCode(); 03-01-2015 at 08:50 AM.

  10. #10
    Registered User doCode()'s Avatar
    Join Date
    Feb 2015
    Location
    Le Universe
    Posts
    13
    I would also like some books on C, books that cover the stuff you have to learn and practice after finishing a beginners book on C.

  11. #11
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by doCode() View Post
    I would also like some books on C, books that cover the stuff you have to learn and practice after finishing a beginners book on C.
    Conveniently enough we have a C Book Recommendations thread.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  12. #12
    Registered User doCode()'s Avatar
    Join Date
    Feb 2015
    Location
    Le Universe
    Posts
    13
    Huh! Nice.

    So, will it be this one > Amazon.com: Let Us C (Computer Science) (9781934015254): Yashavant P. Kanetkar: Books ?

    How much experience will I need if I want to make something like this >
    A program that makes a bootable Linux USB ?

  13. #13
    Registered User
    Join Date
    Jun 2011
    Posts
    88
    Code:
    ...
    
        unitChoiceWeight;
        char  unitChoiceHeight;
    ...
         scanf("%s", &unitChoiceWeight);
    &unitChoiceWeight is not big enough to hold useful string.

  14. #14
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by doCode() View Post
    Check on the bottom of the FAQ page. It links to the source code and instructions on how to compile it.

  15. #15
    Registered User doCode()'s Avatar
    Join Date
    Feb 2015
    Location
    Le Universe
    Posts
    13
    &unitChoiceWeight is not big enough to hold useful string.
    I'm sorry, I don't understand.
    It's only for holding two letters. E.g "kg", "lb" etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Deleting comments in another program
    By chrissy2860 in forum C Programming
    Replies: 4
    Last Post: 02-03-2012, 04:15 PM
  2. program to remove comments from source
    By Abda92 in forum C Programming
    Replies: 12
    Last Post: 12-25-2006, 05:18 PM
  3. comments my program
    By ssharish in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2005, 09:53 AM
  4. I need comments and answers for this C program
    By BadProgrammer in forum C Programming
    Replies: 2
    Last Post: 05-25-2003, 10:57 PM
  5. Replies: 5
    Last Post: 12-05-2001, 07:37 AM

Tags for this Thread