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.