I'm attempting to pass a value determined by one function to another function that comes after it. Essentially: Function 1 determines a value, lets say x. Then X is passed to another function that specifically displays X as text. It's in a separate function to be used by other functions who can also determine the value of X. However, i'm only getting 0 instead of the variable x. What is wrong here?
float fahToCel(int userInput)
{
float result = 0;
do
{
printf("\nPlease input your number in Fahrenheit units:");
scanf("%d", &userInput);
} while (result = 0);
result = (float)(((float)5.0f / (float)9.0f) * (float)(userInput - 32));
return result;
}
float celToFah(int userInput){
float result = 0;
do
{
printf("\nPlease input your number in Celcius units:");
scanf("%d", &userInput);
} while (result = 0);
result = (float)(((float)9.0f / (float)5.0f) * (float)(userInput + 32));
return result;
}
float displayResult(float result) {
printf("\nThe equivalent tempature is: %f\n", result);
return result;
}
void inputInformation() {
printf("1. Convert temperature input from the user in degrees Fahrenheit to
degrees Celsius.\n");
printf("2. Convert temperature input from the user in degrees Celsius to
degrees Fahrenheit.\n");
printf("3. Quit.\n ");
}
int main()
{
int menuChoice = 0;
int userInput = 0;
int result = 0;
while (1)
{
inputInformation();
printf("Please make a selection now:");
scanf("%d", &menuChoice);
switch (menuChoice)
{
case 1:
fahToCel(userInput);
displayResult(result);
break;
case 2:
celToFah(userInput);
displayResult(result);
break;
case 3:
return 0;
default:
printf("\nThat is not a choice!\n");
break;
}
}
}
You never assign the result to result. Change like this:
result = fahToCel(userInput);
displayResult(result);
Related
The code allows you to choose between star and delta resistive network conversions. There is also an exit option. I wanted to validate the values for R_a, R_b, R_c etc. however, I have run into some trouble with my do while loop. The lower limit is 1000 and the upper is 1000000.
I intend to have the program carry on if the input is within range and prompt for another input from the user if it is not. However, as of now, the program continues if the value is within range, but also continues after giving a warning when it is not - when I want it to loop back to the first input prompt.
Once correct, I will add the loop to all inputs.
If anyone is able to fix/find the issue, it would be greatly appreciated.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void)
{
printf("\n\n\t\tDelta and Star Converter\n\n\n");
int choice, num, i;
unsigned long int fact;
while(1)
{
printf("1. Star \n");
printf("2. Delta\n");
printf("0. Exit\n\n\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:;
float R_a=0,R_b=0,R_c=0,R_ab,R_bc,R_ac;
printf("Please enter the value of the Star connected resistors:\n");
do {
printf("R_a = ");
scanf("%f",&R_a);
if (R_a > 1000000) {
printf("Please");
} else if (R_a < 1000) {
printf("Number to low\n");
}else {
}
}while(R_a = -0);
printf("R_b = ");
scanf("%f",&R_b);
printf("R_c = ");
scanf("%f",&R_c);
R_ab=R_a+R_b+(R_a*R_b)/R_c;
R_bc=R_b+R_c+(R_b*R_c)/R_a;
R_ac=R_a+R_c+(R_a*R_c)/R_b;
printf("the equivalent Delta values are: \n");
printf("R_ab = %.2f Ohms\n",R_ab);
printf("R_bc = %.2f Ohms\n",R_bc);
printf("R_ac = %.2f Ohms\n",R_ac);
break;
case 2:;
printf("Please enter the values of the Delta connected resistors:\n");
printf("R_ab = ");
scanf("%f",&R_ab);
printf("R_bc = ");
scanf("%f",&R_bc);
printf("R_ac = ");
scanf("%f",&R_ac);
R_a = (R_ab*R_ac)/(R_ab + R_bc + R_ac);
R_b = (R_ab*R_bc)/(R_ab + R_bc + R_ac);
R_c = (R_ac*R_bc)/(R_ab + R_bc + R_ac);
printf("the equivalent Star values are: \n");
printf("R_a = %.2f Ohms\n",R_a);
printf("R_b = %.2f Ohms\n",R_b);
printf("R_c = %.2f Ohms\n",R_c);
break;
case 0:
printf("\n\nAdios!!\n\n\n");
exit(0); // terminates the complete program execution
}
while (0) ; }
printf("\n\n\t\t\tThank you!\n\n\n");
return 0;
}
while(R_a = -0)
= is the assignment operator. This assigns -0 to R_a, and evaluates to the same falsy value, ending the loop.
Change the do ... while to an infinite loop, and break the loop when the value is in range.
while (1) {
printf("R_a = ");
if (1 != scanf("%f", &R_a))
exit(EXIT_FAILURE);
if (R_a > 1000000) {
fprintf(stderr, "Number too high.\n");
} else if (R_a < 1000) {
fprintf(stderr, "Number to low.\n");
} else {
break;
}
}
I have a converter, but it converts all the units that are in the program.how can i make that the user who opens the project to choose what type of unit to convert?
#include<stdio.h>
#include<conio.h>
int main()
{
float m, f, l, g, cm, inch;
printf("Type meter : ");
scanf("%f",&m);
f = 3.2808399 * m;
printf("feets: %f",f);
printf("\nType gallons : ");
scanf("%f",&g);
l = 3.78541178 * g;
printf("litres: %f",l);
printf("\ninches : ");
scanf("%f", &inch);
cm = 2.54 * inch;
printf("cm: %f", cm);
return 0;
}
This code below is definitely not the best one in terms of complexity, portability, optimisation (Memory/Time) and many other aspects of programming, but it should get you going.
I have added comments to explain the code. Almost all of the code with all those printfs is self-explanatory.
#include<stdio.h>
int main(void)
{
// We need just 3 variables
// This one is for getting the user option
int choice = 0;
// We need these to float variables for user input and an output
float Input = 0.0, Output = 0.0;
// Following code till `while(1)` is optional.
printf("\nThis is a converter with a fixed set of functions.");
printf("\nNote: This converter does support floating point inputs.");
printf("\nNote: Floating point inputs and outputs are truncated to 2 digits after decimal place.");
printf("\nNote: Press any key to acknowledge!");
getchar();
// To get user input multiple times, you'll need to loop
while(1)
{
printf("\n\nFollowing functions are supported, enter a suitable choice form the list below.");
printf("\nPress `1` for Converting Metres to Feet.");
printf("\nPress `2` for Converting Gallons to Litres.");
printf("\nPress `3` for Converting Inches to Centimetres.");
printf("\nPress `0` for Exiting the program.");
printf("\nEnter your Option : ");
scanf("%d", &choice);
// Lets implement a switch-case statement to get the job done
switch(choice)
{
case 1:
printf("Enter input value (in Metres) : ");
scanf("%f",&Input);
Output = 3.2808399 * Input;
printf("%0.2f Metres is equal to %0.2f Feets", Input, Output);
break;
case 2:
printf("Enter input value (in Gallons) : ");
scanf("%f",&Input);
Output = 3.78541178 * Input;
printf("%0.2f Gallons is equal to %0.2f Litres", Input, Output);
break;
case 3:
printf("Enter input value (in Inches) : ");
scanf("%f",&Input);
Output = 2.54 * Input;
printf("%0.2f Inches is equal to %0.2f Centimetres", Input, Output);
break;
case 0:
printf("Thank you. The program will exit now!\n\n");
return 0;
break;
// This default case should take care of the invalid set of choices entered by user
default:
printf("Option you entered is either invalid or is not supported as of now!");
}
}
return 0;
}
Another way to implement this could be using an if-else if-else ladder.
So you can remove swtich-case statements from the code below and replace it with the following:
if(choice == 0)
{
printf("Thank you. The program will exit now!\n\n");
return 0;
}
else if(choice == 1)
{
printf("Enter input value (in Metres) : ");
scanf("%f",&Input);
Output = 3.2808399 * Input;
printf("%0.2f Metres is equal to %0.2f Feets", Input, Output);
}
else if(choice == 2)
{
printf("Enter input value (in Gallons) : ");
scanf("%f",&Input);
Output = 3.78541178 * Input;
printf("%0.2f Gallons is equal to %0.2f Litres", Input, Output);
}
else if(choice == 3)
{
printf("Enter input value (in Inches) : ");
scanf("%f",&Input);
Output = 2.54 * Input;
printf("%0.2f Inches is equal to %0.2f Centimetres", Input, Output);
}
else
{
// This should take care of the invalid set of choices entered by user
printf("Option you entered is either invalid or is not supported as of now!");
}
char unit;
printf("Which unit do you want to convert (input:m,g,i): ");
scanf("%c",&unit);
if(unit == 'm') ...
else if( if(unit == 'g'))....
else if( if(unit == 'i'))....
else printf("wrong input ! \n");
I am a bit new to c programming. For my project I have been tasked with developing a simple calculator that asks the user to input an option then that option declares what operation is to be performed (i.e. if the user enters 1, this corresponds to selecting the addition option which then allows the user to add two numbers they choose. I have the majority of the code worked out, but the catch is that I need the calculator to toggle between int and double variables. When the user enters 5, the calculator should now work with integers then if the user hits 5 again, the calculator switches back to doubles and vice versa so long as you want to switch back and forth. The calculator automatically works with doubles. So, more specifically, if I wanted to use integer variables, I would enter 5, then lets say I wanted to switch back to doubles, I should enter five and receive the message "Calculator now works with doubles." So here is my code thus far:
#include<stdio.h>
#include<math.h>
#include<stdbool.h>
int main()
{
int integermode, doublemode, m, a, b, sum2, difference2, product2,
quotient2;
double i, j, sum1, difference1, product1, quotient1;
printf("This program implements a calculator.");
while(m !=6) {
printf("Options:\n");
printf("1 - addition\n2 - subtraction\n3 - multiplication\n4 - division\n5 - toggle calculator type\n6 - exit program\n");
printf("Please enter your option: ");
scanf("%d", &m);
if(m > 6) {
printf("Invalid option.\n");
printf("Options:\n");
printf("1 - addition\n2 - subtraction\n3 - multiplication\n4 - division\n5 - toggle calculator type\n6 - exit program\n");
printf("Please enter your option: ");
scanf("%d", &m);
}
switch (m) {
case 1:
if (integermode == true) {
printf("Enter first term: ");
scanf("%d", &a);
printf("Enter second term: ");
scanf("%d", &b);
printf("The sum is: %d\n", a+b);
break;
}
if (integermode == false) {
printf("Enter first term: ");
scanf("%lf", &i);
printf("Enter second term: ");
scanf("%lf", &j);
printf("The sum is: %.15lf\n", i+j);
}
break;
case 2:
if (integermode == true) {
printf("Enter first term: ");
scanf("%d", &a);
printf("Enter second term: ");
scanf("%d", &b);
printf("The difference is: %d\n", a-b);
break;
}
if (integermode == false) {
printf("Enter first term: ");
scanf("%lf", &i);
printf("Enter second term: ");
scanf("%lf", &j);
printf("The difference is: %.15lf\n", i-j);
}
break;
case 3:
if (integermode == true) {
printf("Enter first term: ");
scanf("%d", &a);
printf("Enter second term: ");
scanf("%d", &b);
printf("The product is: %d\n", a*b);
break;
}
if (integermode == false) {
printf("Enter first term: ");
scanf("%lf", &i);
printf("Enter second term: ");
scanf("%lf", &j);
printf("The product is: %.15lf\n", i*j);
}
break;
case 4:
if (integermode == true) {
printf("Enter first term: ");
scanf("%d", &a);
printf("Enter second term: ");
scanf("%d", &b);
if (b != 0) printf("The quotient is: %d\n", a/b);
if (b == 0) printf("Cannot divide by zero!\n");
break;
}
if (integermode == false) {
printf("Enter first term: ");
scanf("%lf", &i);
printf("Enter second term: ");
scanf("%lf", &j);
if(j != 0) printf("The quotient is: %.15lf\n", i/j);
if(j == 0) printf("Cannot divide by zero!\n");
break;
}
case 5:
if (m = 5) {
integermode = true;
printf("Calculator now works with integers.\n");
}
if (m != 5) integermode = false;
}
}
return 0;
}
General improvements
First off, you should notice your main function is massive for what it needs to accomplish, and takes a ton of indent levels (when properly indented). That is mostly because you've got a ton of duplicated functionality. You could easily extract the code that asks for the input numbers into a function and reuse it, changing only the operation performed in each case. Or probably even better, you could ask for the input numbers as a part of the main while loop, and then perform a different operation with them depending on the operation mode you're in.
You're not using <math.h>, so you can leave it out.
Another thing I've noticed is you don't initialize the m variable before using it in the while loop. That could potentially cause a lot of problems, so you should initialize it to some value that isn't a valid mode, like for example 0 or -1.
You're also using two consecutive if statements when if/else could be used.
if (integermode == true) {
.....
}
if (integermode == false) {
.....
}
is the same as
if (integermode == true) {
.....
} else {
.....
}
but harder to read and less efficient, as you're doing two comparisons instead of one.
Implementation of double/integer modes
About the functionality you want to implement, you could very well define an enumeration with the possible modes:
typedef enum _datamode {
INTEGER_MODE,
DOUBLE_MODE
} data_mode;
and then have a variable holding the current mode:
data_mode datamode = INTEGER_MODE /* Initializing this to the default mode */
then use integers or doubles during the calculation and output depending on the current value of the variable. Of course, this could be done in a shorter way with an integer instead of an enum and a typedef, but I find this to be a more verbose way.
For the inputs and the result, you could use the union C type, which reserves memory for the biggest type you specify inside it and lets you store any of them in it. For example:
union io {
int i;
double d;
} a, b, result;
declares three variables that have the io union as type. That way, you can use them as integers or doubles as you like (as long as you don't mix types up and, for example, store an integer and read it as a double)
Full working implementation
The following would be my implementation of the program you have to do. It could be accomplished in a much shorter way (take into account the switches dealing with enums take a fair bit of the total space) but I think this is way more elegant and verbose.
I've respected your print statements and the flow of your application, even though I think they could be better (maybe this is something your assignment imposes on you?)
Please take this as an opportunity to learn and don't just copy-paste the code.
#include <stdio.h>
#include <stdlib.h>
typedef enum _datamode {
INTEGER_MODE,
DOUBLE_MODE
} data_mode;
typedef enum _operationmode {
ADDITION,
SUBSTRACTION,
MULTIPLICATION,
DIVISION,
TOGGLE,
EXIT
} operation_mode;
operation_mode ask_mode () {
int input = -1;
while (1) {
printf("Options:\n");
printf("1 - addition\n2 - subtraction\n3 - multiplication\n4 - division\n5 - toggle calculator type\n6 - exit program\n");
printf("Please enter your option: ");
scanf("%i", &input);
if (input < 1 || input > 6) {
printf("Invalid option.\n");
} else {
switch (input) {
case 1:
return ADDITION;
case 2:
return SUBSTRACTION;
case 3:
return MULTIPLICATION;
case 4:
return DIVISION;
case 5:
return TOGGLE;
case 6:
return EXIT;
default:
exit(EXIT_FAILURE); /* Error must've occurred in order for a different value to be present */
}
}
}
}
union _io {
int i;
double d;
};
void get_inputs_integer (int *a, int *b) {
printf("Enter first term: ");
scanf("%i", a);
printf("Enter second term: ");
scanf("%i", b);
}
void get_inputs_double (double *a, double *b) {
printf("Enter first term: ");
scanf("%lf", a);
printf("Enter second term: ");
scanf("%lf", b);
}
int main (int argc, char **argv) {
union _io operand1, operand2, result;
operation_mode o_mode;
data_mode d_mode = INTEGER_MODE;
printf("This program implements a calculator.");
do {
o_mode = ask_mode();
if (o_mode == TOGGLE) {
if (d_mode == INTEGER_MODE) {
d_mode = DOUBLE_MODE;
printf("Calculator now on double mode\n");
} else {
d_mode = INTEGER_MODE;
printf("Calculator now on integer mode\n");
}
} else if (o_mode != EXIT) {
if (d_mode == INTEGER_MODE) {
get_inputs_integer(&operand1.i, &operand2.i);
switch (o_mode) {
case ADDITION:
result.i = operand1.i + operand2.i;
break;
case SUBSTRACTION:
result.i = operand1.i - operand2.i;
break;
case MULTIPLICATION:
result.i = operand1.i * operand2.i;
break;
case DIVISION:
result.i = operand1.i / operand2.i;
break;
default:
exit(EXIT_FAILURE); /* Error must've occurred in order for a different value to be present */
}
printf("The result is %i\n", result.i);
} else {
get_inputs_double(&operand1.d, &operand2.d);
switch (o_mode) {
case ADDITION:
result.d = operand1.d + operand2.d;
break;
case SUBSTRACTION:
result.d = operand1.d - operand2.d;
break;
case MULTIPLICATION:
result.d = operand1.d * operand2.d;
break;
case DIVISION:
result.d = operand1.d / operand2.d;
break;
default:
exit(EXIT_FAILURE); /* Error must've occurred in order for a different value to be present */
}
printf("The result is %lf\n", result.d);
}
}
} while (o_mode != EXIT);
}
I need to ask an input from a user and he/she should have the ability to write a float number, and I need to do some calculation on the 2 numbers, but I'm having a problem after the isdigit test...even if I enter an integer its going to the continue;
This is my code:
#include <stdio.h>
#include <ctype.h>
char get_choice(void);
float calc(float number1, float number2);
int main()
{
float userNum1;
float userNum2;
get_choice();
printf("Please enter a number:\n");
while ((scanf("%f", &userNum1)) == 1)
{
if (!isdigit(userNum1))
{
printf("Please enter a number:\n");
continue;
}
printf("Please enter another number:\n");
while ((scanf("%f", &userNum2) == 1))
{
if (!isdigit(userNum2))
{
printf("Please enter a number:/n");
continue;
}
else if (userNum2 == '0')
{
printf("Please enter a numer higher than 0:\n");
continue;
}
}
}
calc(userNum1, userNum2);
return 0;
}
float calc(float number1, float number2)
{
int answer;
switch (get_choice())
{
case 'a':
answer = number1 + number2;
break;
case 's':
answer = number1 - number2;
break;
case 'm':
answer = number1 * number2;
break;
case 'd':
answer = number1 / number2;
break;
}
return answer;
}
char get_choice(void)
{
int choice;
printf("Enter the operation of your choice:\n");
printf("a. add s. subtract\n");
printf("m. multiply d. divide\n");
printf("q. quit\n");
while ((choice = getchar()) == 1 && choice != 'q')
{
if (choice != 'a' || choice != 's' || choice != 'm' || choice != 'd')
{
printf("Enter the operation of your choice:\n");
printf("a. add s. subtract\n");
printf("m. multiply d. divide\n");
printf("q. quit\n");
continue;
}
}
return choice;
}
Apologize for uploading the functions as well, but since I'm a newbie the problem might be there.
Cheers.
isDigit() only takes a character input.
The best way to check that you are getting input correctly is to use
if(scanf("%f", &userNum) != 1) {
// Handle item not float here
}
Since scanf() will return the number of items correctly scanned.
IsDigit() Function check only if the character is decimal digit or not.
Let say:
int isdigit ( int c );
Checks whether c is a decimal digit character.
Decimal digits can be any of: 0 1 2 3 4 5 6 7 8 9.
The isdigit() function will return non-zero if c is a decimal digit; otherwise, it shall return 0.
You can also try something like this:
int get_float(char *val, float *F){
char *eptr;
float f;
errno = 0;
f = strtof(val, &eptr);
if(eptr != val && errno != ERANGE){
*F = f;
return 1;
}
return 0;
If val points to float number function return 1 & put this number to *F, else it returns 0.
My brain is in pain. I cannot figure what is happening here. I would pay if allowed the person with the best answer.
I have this Weight program for school and it allows user to input starting weight and than through a switch it calls various functions which are either allows user to gain weight or lose weight. Some functions return what I am expecting and some don't. These functions return what I expect cakeGain(), hardWorkout(), mediumWorkout(), but I cant figure out why I am getting incorrect returns on runLoss(), burgerGain(), pizzaGain() and easyWorkout(). I cannot seem to figure out any big difference between the functions and I have tried all kinds of options. I was originally passing my weight variable by reference to all the functions and letting them change the value but my instructor did not want all the pointers.
This is not beautiful code and I have commented out some sections which I will fix later. I know a lot of my work is bad form (system calls etc.) but my instructor has not taught us alternative methods or does not care. This is still considered a beginner course. All I am curious on is why some functions work when others do not.
Thanks again,
Mike (I really appreciate all that take time from their schedules to help people like me out. I cannot wait for the day that I am competent enough to help out others).
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#define pause system ("pause")
#define cls system ("cls")
#define flush fflush(stdin)
//prototype functions here
void mainMenu(void);
char getUserChoice();
double enterWeight();
double runLoss(double usersWeight);
double burgerGain(double usersWeight);
double eatPizza(double usersWeight);
double eatCake(double usersWeight);
double hardWorkout(double usersWeight);
double mediumWorkout(double usersWeight);
double easyWorkout(double usersWeight);
//void weightChecker(double usersWeight);
int main() {
//declare main variables here
char userChoice = ' ';
double usersWeight;
do {
userChoice = getUserChoice();
switch (userChoice) {
case'A': //get weight
usersWeight = enterWeight();
cls;
printf("Your starting weight is: %dlbs.\n\n",usersWeight);
break;
case'B': //run
usersWeight = runLoss(usersWeight);
cls;
printf("Your weight is now %d.\n", usersWeight);
//weightChecker(usersWeight);
break;
case'C': //eat burger
usersWeight = burgerGain(usersWeight);
cls;
printf("Your weight is now %d.\n", usersWeight);
//weightChecker(usersWeight);
break;
case'D': //eat pizza
usersWeight = eatPizza(usersWeight);
cls;
printf("Your weight is now %d.\n", usersWeight);
//weightChecker(usersWeight);
break;
case'E': //eat cake
usersWeight = eatCake(usersWeight);
cls;
printf("Your weight is now %d.\n", usersWeight);
//weightChecker(usersWeight);
break;
case'F': //hard workout
usersWeight = hardWorkout(usersWeight);
cls;
printf("Your weight is now %d.\n", usersWeight);
//weightChecker(usersWeight);
break;
case'G': //medium workout
usersWeight = mediumWorkout(usersWeight);
cls;
printf("Your weight is now %d.\n", usersWeight);
//weightChecker(usersWeight);
break;
case'H': //easy workout
usersWeight = easyWorkout(usersWeight);
cls;
printf("Your weight is now %d.\n", usersWeight);
//weightChecker(usersWeight);
break;
case'I': //quit
exit(0);
}
} while(userChoice != 'I');
return 0;
} //end of main
char getUserChoice() {
char result;
do {
mainMenu();
scanf("%c", &result);
flush;
result = toupper (result);
} while (result < 'A' || result > 'I');
return result;
}
void mainMenu(void) {
//cls;
printf("A) \tEnter starting weight or change weight\n");
printf("B) \tGo running\n");
printf("C) \tEat Hamburgers\n");
printf("D) \tEat pizza\n");
printf("E) \tEat cake\n");
printf("F) \tDo a hard workout\n");
printf("G) \tDo a medium workout\n");
printf("H) \tDo a easy workout\n");
printf("I) \tQuit\n\n");
printf("Enter your selection: ");
}
double enterWeight() {
double startingWeight = 0.0;
printf("Please enter your starting weight.\n");
scanf("%i", &startingWeight);
flush;
return startingWeight;
}
double runLoss(double usersWeight) {
double result;
double milesRan = 0.0;
double runMinus = 0.0;
printf("How many miles did you run?\n");
scanf("%i", &milesRan);
flush;
runMinus = milesRan * .005;
result = usersWeight - runMinus;
return result;
}
double burgerGain(double usersWeight) {
double result;
double hamburgersAte = 0.0;
double hamburgerGain = 0.0;
printf("How many hamburgers did you eat?\n");
scanf("%i", &hamburgersAte);
flush;
hamburgerGain = hamburgersAte * .005;
if (hamburgersAte > 2) {
hamburgerGain = hamburgerGain + .5;
}
result = usersWeight + hamburgerGain;
return result;
}
double eatPizza(double usersWeight) {
double result;
double pizzas = 0.0;
double pizzaGain = 0.0;
printf("How many pizza's did you eat?\n");
scanf("%i", &pizzas);
flush;
pizzaGain = pizzas * .075;
if (pizzas > 2) {
pizzaGain = pizzaGain + 1;
}
result = usersWeight + pizzaGain;
return result;
}
double eatCake(double usersWeight) {
double result;
double cakesEaten = 0.0;
double cakeGain = 0.0;
printf("How many cakes did you eat?\n");
scanf("%i", &cakesEaten);
flush;
cakeGain = cakesEaten * 2;
result = usersWeight + cakeGain;
return result;
}
double hardWorkout(double usersWeight) {
double result;
double hardWorkoutHours = 0.0;
double hardWorkoutLoss = 0.0;
printf("How many hours did you perform a hard workout?\n");
scanf("%i", &hardWorkoutHours);
flush;
hardWorkoutLoss = hardWorkoutHours * 1;
if (hardWorkoutHours > 2) {
hardWorkoutLoss = hardWorkoutLoss + 1;
}
result = usersWeight - hardWorkoutLoss;
return result;
}
double mediumWorkout(double usersWeight) {
double result;
double mediumWorkoutHours = 0.0;
double mediumWorkoutLoss = 0.0;
printf("How many hours did you perform a medium workout?\n");
scanf("%i", &mediumWorkoutHours);
flush;
mediumWorkoutLoss = mediumWorkoutHours * .5;
if (mediumWorkoutHours > 2) {
mediumWorkoutLoss = mediumWorkoutLoss + .25;
}
result = usersWeight - mediumWorkoutLoss;
return result;
}
double easyWorkout(double usersWeight) {
double result;
double easyWorkoutHours = 0.0;
double easyWorkoutLoss = 0.0;
printf("How many hours did you perform a easy workout?\n");
scanf("%i", &easyWorkoutHours);
flush;
easyWorkoutLoss = easyWorkoutHours * .025;
result = usersWeight - easyWorkoutLoss;
return result;
}
/*void weightChecker(double usersWeight) {
if (usersWeight >= 200 && usersWeight <= 400)
{
printf("You are obese, time to start working out.\n");
}
else if (usersWeight >= 150 && usersWeight < 200)
{
printf("Your weight is considered heavy, time to cut down on junk food.\n");
}
else if (usersWeight >= 125 && usersWeight <150)
{
printf("You are fit, keep up the good work.\n");
}
else if (usersWeight >= 80 && usersWeight <125)
{
printf("You are thin, time to start considering eating more.\n");
}
else
{
printf("You are dead due to improper weight management. Game Over!\n");
pause;
exit(0);
}
pause;
} */
When you print a double or a float value you shoud use %f
float x;
x = eatNutella();
printf("x is %f\n", x);
%d is for decimal values (like int)