How to solve Error : else without a previous if [closed] - c

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I've been trying to solve this error for half an hour now.
How do I get this code to work?
Here's the input, please point out my mistake(s).
#include <stdio.h>
#include <math.h>
void main()
{
const float pi = 3.142;
int choice;
float radius, volume, volts, ohms, watts, mass, accel, force;
printf("\n1. Calculate the volume of a sphere");
printf("\n2. Calculate the power of a circuit");
printf("\n3. Calculate the force of an object");
printf("\n-----------------------------------\n");
printf("What is your choice? : ");
scanf("%d", &choice);
if(choice==1)
printf("Enter value of radius (cm) : ");
scanf("%f", &radius);
volume = 4/3 * pi * pow(radius,3);
printf("Volume of a sphere is %.2f", volume);
else if(choice==2)
printf("Enter value of voltage (volts) : ");
scanf("%f", &volts);
printf("Enter value of resistance (ohms) : ");
scanf("%f", &ohms);
watts = pow(volts,2) / ohms;
printf("Power of circuit is : %.2f watts", watts);
else if(choice==3)
printf("Enter mass of object (kg) : ");
scanf("%f", &mass);
printf("Enter acceleration (meters per second squared) : ");
scanf("%f", &accel);
force = mass * accel;
printf("The force of the object is : %.2f Neutons", force);
else
printf("You've entered an invalid choice...");
getch();
}

Use braces if a if/else block has multiple lines:
if (choice==1) {
printf("Enter value of radius (cm) : ");
scanf("%f", &radius);
volume = 4/3 * pi * pow(radius,3);
printf("Volume of a sphere is %.2f", volume);
} else if (choice==2) {
printf("Enter value of voltage (volts) : ");
scanf("%f", &volts);
printf("Enter value of resistance (ohms) : ");
scanf("%f", &ohms);
watts = pow(volts,2) / ohms;
printf("Power of circuit is : %.2f watts", watts);
} else if (choice==3) {
printf("Enter mass of object (kg) : ");
scanf("%f", &mass);
printf("Enter acceleration (meters per second squared) : ");
scanf("%f", &accel);
force = mass * accel;
printf("The force of the object is : %.2f Neutons", force);
} else printf("You've entered an invalid choice...");
Without braces your code is interpreted like
// Standalone if block
if (choice==1)
printf("Enter value of radius (cm) : ");
// End of the if block
// ...
// else without previous if (syntax error)
else if (choice==2)
printf("Enter value of voltage (volts) : ");

Use if..else if..else block properly with braces.
If you are using GCC compiler then use following code and compile code with math library.
For example (correct way): gcc test.c -lm
otherwise it gives error : undefined reference to 'pow' (math library function)
#include <stdio.h> //Standard IO functions
#include <math.h> //math functions
int main()
{
const float pi = 3.142;
int choice;
float radius, volume, volts, ohms, watts, mass, accel, force;
printf("\n1. Calculate the volume of a sphere");
printf("\n2. Calculate the power of a circuit");
printf("\n3. Calculate the force of an object");
printf("\n-----------------------------------\n");
printf("What is your choice? : ");
scanf("%d", &choice);
if(choice==1)
{
printf("Enter value of radius (cm) : ");
scanf("%f", &radius);
volume = 4/3 * pi * pow(radius,3);
printf("Volume of a sphere is %.2f", volume);
}
else if(choice==2)
{
printf("Enter value of voltage (volts) : ");
scanf("%f", &volts);
printf("Enter value of resistance (ohms) : ");
scanf("%f", &ohms);
watts = pow(volts,2) / ohms;
printf("Power of circuit is : %.2f watts", watts);
}
else if(choice==3)
{
printf("Enter mass of object (kg) : ");
scanf("%f", &mass);
printf("Enter acceleration (meters per second squared) : ");
scanf("%f", &accel);
force = mass * accel;
printf("The force of the object is : %.2f Neutons", force);
}
else
printf("You've entered an invalid choice...");
return 0;
}

missing brackets in if else command
#include <stdio.h>
void main()
{
const float pi = 3.142;
int choice;
float radius, volume, volts, ohms, watts, mass, accel, force;
printf("\n1. Calculate the volume of a sphere");
printf("\n2. Calculate the power of a circuit");
printf("\n3. Calculate the force of an object");
printf("\n-----------------------------------\n");
printf("What is your choice? : ");
scanf("%d", &choice);
if(choice==1)
{
printf("Enter value of radius (cm) : ");
scanf("%f", &radius);
volume = 4/3 * pi * pow(radius,3);
printf("Volume of a sphere is %.2f", volume);
}
else if(choice==2)
{
printf("Enter value of voltage (volts) : ");
scanf("%f", &volts);
printf("Enter value of resistance (ohms) : ");
scanf("%f", &ohms);
watts = pow(volts,2) / ohms;
printf("Power of circuit is : %.2f watts", watts);
}
else if(choice==3)
{
printf("Enter mass of object (kg) : ");
scanf("%f", &mass);
printf("Enter acceleration (meters per second squared) : ");
scanf("%f", &accel);
force = mass * accel;
printf("The force of the object is : %.2f Neutons", force);
}
else
printf("You've entered an invalid choice...");
getch();
}

You are missing the curly braces {}.
Do: if() { ... } else if() { ...}. You should take a look at: if statement without brackets

Related

I want to create a menu that shows 1.Client ID, 2.Property details,3.Exit

`#include<stdio.h>
#include<conio.h>
void main()
{
char L,F,H;
float length,breadth,CID,Aoc;/*Pte*/
float Cost_per_sqft,Total_cost, GST;
int dicnt,age,ch;
char Pte;
printf("Enter the Client ID\n");
scanf("%f", &CID);
printf("Enter the age of client\n");
scanf("%d", &age);
if (age >=60)
{
printf("The client is eligible for a discount\n");
}
else if (age<60)
{
printf("The client is not eligible for a discount\n");
}
{ printf("Select Porperty type\nF=Flat\nL=Land\nH=House\n");
scanf(" %c", &Pte);
printf("Enter length\n");
scanf("%f", &length);
printf("Enter breadth\n");
scanf("%f", &breadth);
if (Pte=F)
{
Cost_per_sqft = 5000.0;
printf("Cost per sqft = %f\n", Cost_per_sqft);
}
else if (Pte=L)
{
Cost_per_sqft = 6000.0;
printf("Cost per sqft = %f\n", Cost_per_sqft);
}
else if (Pte=H);
{
Cost_per_sqft = 9000.0;
printf("Cost per sqft = %f\n", Cost_per_sqft);
}
printf("Please select the menu option\n");
printf("1.Client ID\n");
printf("2.Property details\n");
printf("3.Exit\n");
scanf("%d", &ch);
switch(ch)
{
case 1:
printf("Client ID = %f\n", CID);
printf("Age of client = %d", age);
break;
case 2:
printf("Property type = %c\n", Pte);
printf("Cost per square feet = %f\n", Cost_per_sqft);
Total_cost = (length*breadth*Cost_per_sqft);
printf("Total cost = %f", Total_cost);
break;
case 3:
;//add content
break;
}
}
}
`
The program is taking value of cost per sqft of House which is 9000, even if I enter Flat or Land variables. another problem is the total cost of the property. instead of taking the desired property types cost per sqft, I believe its multiplying cost per sqft for all values F,L,H. and in the property details menu, the type of property doesn't show either.
I would suggest you to use commenting on your code it'll be easy to find the bug, even for you.
else if (Pte=H);
{
Cost_per_sqft = 9000.0;
printf("Cost per sqft = %f\n", Cost_per_sqft);
}
Try removing the termination operator (';') from the else if statement.
Also use '==' instead of '=' in conditional statements.
eg.
in if (Pte=F) use if (Pte=='F') instead
That should just help you make your code run.

fputs or fprintf not writing into a file for strings

Why fputs() or fprintf() are not writing into the file.txt for %s? I tried both functions but they don't seem to write into the file. However, when using the same functions for %d, it does work currently. As for the expected output versus what currently happens, please find below an example when choosing option 1 in the program, and entering a square side of 2.
Currently happening:
1
2
4
8
Expected output:
You have chosen option: 1
The side of the square is: 2
The square area is: 4
The square perimeter is: 8
Any ideas as to what may be causing this? Thank you!
N.b. This code is for learning purposes, so you may find inconsistencies in the way I work with variables (i.e. passing by reference or value). Please disregard that.
#include <stdio.h>
#include <locale.h>
#include <string.h>
#define PI 3.14
void square_functions (int side, int *square_perimeter, int *square_area);
float circle_functions (float radius, float * area_circle, float * circumference, FILE *fich);
int main()
{
float radius, area_circle, circumference;
int side, square_area, square_perimeter, choice;
FILE *fich; /* esto como variable global penaliza! */
fich=fopen("file.txt","w");
while (1)
{
printf("Enter 1 to calculate the square\n");
printf("Enter 2 to calculate the circumference\n");
printf("Enter your option:\n");
scanf("%d",&choice);
fprintf(fich,"%s\n", "You have chosen option:");
fprintf(fich,"%d\n",choice);
switch (choice)
{
case 1:
printf("Please enter the side of the square: ");
scanf("%d", &side);
fprintf(fich,"%s\n", "The side of the square is:");
fprintf(fich,"%d\n",side);
if(side > 0)
{
square_functions (side, &square_perimeter, &square_area);
printf("\nSquare area: %d", square_area);
printf("\nSquare perimeter: %d\n", square_perimeter);
fprintf(fich,"%s\n", "The square area is:");
fprintf(fich,"%d\n", square_area);
fputs("The square perimeter is",fich);
fprintf(fich,"%d\n", square_perimeter);
}
else
{
printf("The value is invalid, the operation has been cancelled");
}
break;
case 2:
printf("\n\nPlease enter the radius fo the circle: ");
scanf("%f", &radius);
if(radius > 0)
{
circle_functions (radius, &area_circle, &circumference, fich);
printf("Circle area: %f", area_circle);
printf("circumference: %f", circumference);
}
else
{
printf("The value is invalid, the operation has been cancelled");
}
break;
}
}
fclose(fich);
return 0;
}
void square_functions (int side, int *square_perimeter, int *square_area)
{
*square_perimeter = side * 4;
*square_area = side * side;
}
float circle_functions (float radius, float * area_circle, float * circumference, FILE *fich)
{
*area_circle = PI * radius * radius;
*circumference = 2 * PI * radius;
fputs("The area of the circle is",fich);
fprintf(fich,"%f\n",*area_circle);
fputs("The circumference is",fich);
fprintf(fich,"%f\n",*circumference);
return 0;
}
The problem is you're not closing the file. Note: You have fclose(fich);, but it's unreachable. Add a third condition, such as the following, at it should work:
case 3:
fclose(fich);
return 0;
An alternative would be to not use an infinite loop (while(1)) and use a condition instead. For example:
bool run = true;
while(run)
{
...
case 3:
run = false;
break;
...
}
Why not try fprintf(fich,"The square area is:\n");
Your code as is, is fairly complete, but requires a few adjustments
Requires a way to exit,
In the second case (circles) the function printf() is used as opposed to fprintf().
fclose() is never accessed due to infinite loop
The following addresses these, by using a boolean variable running as a flag to run, and a third case to access it to exit, and adds the fprintf function in the second case, and opens/closes file each iteration:
#define FILENAME "file.txt"
int main(void)
{
float radius, area_circle, circumference;
int side, square_area, square_perimeter, choice;
BOOL running = TRUE;
FILE *fich; /* esto como variable global penaliza! */
//create file for write:
fich=fopen(FILENAME,"w");//test before using
if(fich)
{
fprintf(fich, "Beginning of file:\n\n");
fclose(fich);
}
while (running)
{
printf("Enter 1 to calculate the square\n");
printf("Enter 2 to calculate the circumference\n");
printf("Enter 3 to quit program\n");
printf("Enter your option:\n");
scanf("%d",&choice);
fprintf(fich,"%d\n",choice);
switch (choice)
{
case 1:
fich=fopen(FILENAME,"a");//test before using
if(fich)
{
printf("Please enter the side of the square: ");
scanf("%d", &side);
fprintf(fich,"%d\n",side);
if(side > 0)
{
square_functions (side, &square_perimeter, &square_area);
printf("\nSquare area: %d", square_area);
printf("\nSquare perimeter: %d\n", square_perimeter);
fprintf(fich,"%s\n", "The square area is: %d\n");
fprintf(fich,"The perimeter is: %d\n", square_perimeter);
}
else
{
printf("The value is invalid, the operation has been cancelled");
}
fclose(fich);
}
break;
case 2:
fich=fopen(FILENAME,"a");//test before using
if(fich)
{
printf("\n\nPlease enter the radius of the circle: ");
scanf("%f", &radius);
if(radius > 0)
{
circle_functions (radius, &area_circle, &circumference, fich);
printf("Circle area: %f", area_circle);
printf("circumference: %f", circumference);
fprintf(fich, "Circle area: %f", area_circle);
fprintf(fich, "circumference: %f", circumference);
}
else
{
printf("The value is invalid, the operation has been cancelled");
}
fclose(fich);
}
break;
case 3:
running = FALSE;
break;
}
}
return 0;
}
When I run the program on Microsoft Windows, the output to stdout works, but not the output to fich. The problem is that (at least with me), stdout is line buffered, whereas fich is fully buffered. Since you never call fclose (because it is outside the infinite loop), the buffer of fich never gets flushed.
To fix this problem, you can either explicitly flush the buffer with fflush( fich ); or you can implicitly flush the buffer, by ensuring that fclose gets called (for example by exiting the loop).
Alternatively, you can disable buffering completely, by calling setbuf( fich, NULL); Please note that this line must appear immediately after the call to fopen. See the documentation on the function setbuf for further information. However, this should normally not be done, because buffering can be important for performance.
Thanks to #AndreasWenzel comments I have added below the final pieces of code that work; first version using fclose(fich); and second one using fflush(fich);.
fclose(fich); version: the file is being closed every time is opened to write in it.
#include <stdio.h>
#include <locale.h>
#include <string.h>
#define PI 3.14
void square_functions (double side, double *square_perimeter, double *square_area);
void circle_functions (double radius, double *area_circle, double *circumference);
int main()
{
double radius, area_circle, circumference, side, square_area, square_perimeter;
int choice;
FILE *fich;
while (1)
{
printf("\nEnter 1 to calculate the square");
printf("\nEnter 2 to calculate the circumference");
printf("\nEnter your option:");
scanf("%d",&choice);
fich = fopen("file.txt","a+");
fprintf(fich,"%s", "\n\nThe option chosen is: ");
fprintf(fich,"%d\n",choice);
fclose(fich);
switch (choice)
{
case 1:
printf("Please enter the side of the square: ");
scanf("%lf", &side);
fich = fopen("file.txt","a+");
fprintf(fich,"%s", "\nThe side of the square is: ");
fprintf(fich,"%lf",side);
if(side > 0)
{
square_functions (side, &square_perimeter, &square_area);
printf("\nSquare area: %lf", square_area);
printf("\nSquare perimeter: %lf\n", square_perimeter);
fprintf(fich,"%s", "\nThe square area is: ");
fprintf(fich,"%lf", square_area);
fprintf(fich,"%s", "\nThe square perimeter is: ");
fprintf(fich,"%lf", square_perimeter);
fclose(fich);
}
else
{
printf("The value is invalid, the operation has been cancelled");
}
break;
case 2:
printf("\n\nPlease enter the radius fo the circle: ");
scanf("%lf", &radius);
fich = fopen("file.txt","a+");
fprintf(fich,"%s", "\nThe radius of the circle is: ");
fprintf(fich,"%lf",radius);
if(radius > 0)
{
circle_functions (radius, &area_circle, &circumference);
printf("\nCircle area: %lf\n", area_circle);
printf("\nCircumference: %lf\n", circumference);
fprintf(fich,"%s", "\nThe circle area is: ");
fprintf(fich,"%lf",area_circle);
fprintf(fich,"%s", "\nThe circumference is: ");
fprintf(fich,"%lf",circumference);
fclose(fich);
}
else
{
printf("The value is invalid, the operation has been cancelled");
}
break;
}
}
return 0;
}
void square_functions (double side, double *square_perimeter, double *square_area)
{
*square_perimeter = side * 4;
*square_area = side * side;
}
void circle_functions (double radius, double *area_circle, double *circumference)
{
*area_circle = PI * radius * radius;
*circumference = 2 * PI * radius;
}
fflush(fich); version: the buffer is flushed immediately before the switch statement and once immediately after the switch statement (as the last statement of the while loop).
#include <stdio.h>
#include <locale.h>
#include <string.h>
#define PI 3.14
void square_functions (double side, double *square_perimeter, double *square_area);
void circle_functions (double radius, double *area_circle, double *circumference);
int main()
{
double radius, area_circle, circumference, side, square_area, square_perimeter;
int choice;
FILE *fich;
fich = fopen("file.txt","a+");
while (1)
{
printf("\nEnter 1 to calculate the square");
printf("\nEnter 2 to calculate the circumference");
printf("\nEnter your option:");
scanf("%d",&choice);
fprintf(fich,"%s", "\n\nThe option chosen is: ");
fprintf(fich,"%d\n",choice);
fflush( fich );
switch (choice)
{
case 1:
printf("Please enter the side of the square: ");
scanf("%lf", &side);
fprintf(fich,"%s", "\nThe side of the square is: ");
fprintf(fich,"%lf",side);
if(side > 0)
{
square_functions (side, &square_perimeter, &square_area);
printf("\nSquare area: %lf", square_area);
printf("\nSquare perimeter: %lf\n", square_perimeter);
fprintf(fich,"%s", "\nThe square area is: ");
fprintf(fich,"%lf", square_area);
fprintf(fich,"%s", "\nThe square perimeter is: ");
fprintf(fich,"%lf", square_perimeter);
}
else
{
printf("The value is invalid, the operation has been cancelled");
}
break;
case 2:
printf("\n\nPlease enter the radius fo the circle: ");
scanf("%lf", &radius);
fprintf(fich,"%s", "\nThe radius of the circle is: ");
fprintf(fich,"%lf",radius);
if(radius > 0)
{
circle_functions (radius, &area_circle, &circumference);
printf("\nCircle area: %lf\n", area_circle);
printf("\nCircumference: %lf\n", circumference);
fprintf(fich,"%s", "\nThe circle area is: ");
fprintf(fich,"%lf",area_circle);
fprintf(fich,"%s", "\nThe circumference is: ");
fprintf(fich,"%lf",circumference);
}
else
{
printf("The value is invalid, the operation has been cancelled");
}
break;
}
fflush( fich );
}
return 0;
}
void square_functions (double side, double *square_perimeter, double *square_area)
{
*square_perimeter = side * 4;
*square_area = side * side;
}
void circle_functions (double radius, double *area_circle, double *circumference)
{
*area_circle = PI * radius * radius;
*circumference = 2 * PI * radius;
}

C program Always print 0.000000

I have faced a problem with c program.
In this case, whatever value I entered it's only output is 0.000000.
Please check this and give me a solution.
{
long double x,y,m,E,c;
printf("\t\t\t Enter a mass Hydrogen atoms: ");
scanf("%Lf", &x);
printf("\t\t\t Enter a mass Helium atoms: ");
scanf("%Lf", &y);
m=x-y;
c=3*(1*10^27);
E=m*c;
printf("\t\t\t Energy: %Lf", &E);
return 0;
}
You can change the length of the format in the output, using the following notation:
printf("\t\t\t Energy: %i.dlf", &E);
where "i" are the integers you want to show and "d" are the decimals.
Replace "i" and "d" with other numbers and experiment.
while you are getting input from user its become 0 and it's result into 0.0 cause you are using %Lf insted of %lf and also to power 10 by 27 you need to use pow function of math.h
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main(){
double x,y,m,E,c;
clrscr();
printf("\t\t\t Enter a mass Hydrogen atoms: ");
scanf("%lf", &x);
printf("\t\t\t Enter a mass Helium atoms: ");
scanf("%lf", &y);
m=x-y;
c=3*(1*pow(10,27));
printf("\n%lf and %lf",x, y);
E=m*c;
printf("\n\t\t\t Energy: %lf", E);
getch();
return 0;
}

Function do not return proper value

The function converterm(met, bri); when called is not returning proper values. The code is still incomplete but it works for some options. Just type-in the values and whenever asked which option to select, select option 1 and see the results.
At this line conm.m = conb.ft / 3.2808; it do not return expected values.
//METRIC_BRITISH - BUILD 1.0
#include<stdio.h>
#include<stdlib.h>
#include<process.h>
//GLOBAL-STRUCTURES DECLARATION
struct metric
{
float m;
float cm;
};
struct british
{
float ft;
float in;
};
//GLOBAL-STRUCTURE-VARIABLE DECLARATION
struct metric met = { 0,0 };
struct british bri = { 0,0 };
int q = 0;
int w = 0;
//USER-DEFINED FUNCTION
struct metric converterm(struct metric conm, struct british conb);
struct british converterb(struct british conb, struct metric conm);
void header();
void header()
{
printf("*-*-*-*-*METRIC_BRITISH*-*-*-*-*");
printf("\n\n");
}
//PROGRAM STARTS HERE
main()
{
//VARIABLE-DECLARATION
int n = 0, c = 0, b = 0, v = 0, i = 0;
//FUNCTION CALL-OUT
header();
printf("Format : Metric \n\n");
printf("Enter the Value for Metres : ");
scanf_s("%f", &met.m);
printf("\n");
printf("Enter the Value for Centimetres : ");
scanf_s("%f", &met.cm);
printf("\n");
printf("*--------------------------------------------*\n\n");
printf("Format : British \n\n");
printf("Enter the Value for Feet : ");
scanf_s("%f", &bri.ft);
printf("\n");
printf("Enter the Value for Inches : ");
scanf_s("%f", &bri.in);
printf("\n\n");
printf("In which Format would you like to add other value? \n");
printf("1. Metric \n");
printf("2. British \n");
printf("Enter any Option : ");
while (i == 0)
{
printf("\n");
scanf_s("%d", &n);
switch (n)
{
case 1:
printf("In which Unit you want to add value? \n");
printf("1. Metres \n");
printf("2. Centimetres \n");
printf("Enter any Option : ");
scanf_s("%d", &c);
q = c;
met = converterm(met, bri);
i = 1;
break;
case 2:
printf("In which Unit you want to add value? \n");
printf("1. Feet \n");
printf("2. Inch \n");
printf("Enter any Option : ");
scanf_s("%d", &c);
q = c;
bri = converterb(bri, met);
i = 1;
break;
default:
printf("INVALID OPTION. \n");
printf("Please Enter Correct Option.");
i = 0;
break;
}
}
printf("Values for Metric : \n");
printf("Metres = %d \n", met.m);
printf("Centimetre = %d \n", met.cm);
printf("\n*--------------------------------------------*\n\n");
printf("Values for British : \n");
printf("Feet = %d \n", bri.ft);
printf("Inch = %d \n", bri.in);
//TERMINAL-PAUSE
system("pause");
}
struct metric converterm(struct metric conm, struct british conb)
{
int i = 0;
switch (q)
{
case 1:
printf("\n");
printf("Would you like to Add? \n");
printf("1. Add Feet to Metre \n");
printf("2. Add Inch to Metre \n");
printf("Enter any Option : ");
scanf_s("%d", &i);
break;
case 2:
printf("\n");
printf("Would you like to Add? \n");
printf("1. Add Feet to Centimetre \n");
printf("2. Add Inch to Centimetre \n");
printf("Enter any Option : ");
scanf_s("%d", &i);
break;
}
if (i == 1)
{
conm.m = conb.ft / 3.2808;
//conm.m = conb.in / 39.370;
}
else
{
conm.cm = conb.ft / 0.032808;
//conm.cm = conb.in / 0.39370;
}
return(conm);
}
struct british converterb(struct british conb, struct metric conm)
{
int i = 0;
switch (w)
{
case 1:
printf("\n");
printf("Would you like to Add? \n");
printf("1. Add Metre to Feet \n");
printf("2. Add Centimetre to Feet \n");
printf("Enter any Option : ");
scanf_s("%d", &i);
break;
case 2:
printf("\n");
printf("Would you like to Add? \n");
printf("1. Add Metre to Inch \n");
printf("2. Add Centimetre to Inch \n");
printf("Enter any Option : ");
scanf_s("%d", &i);
break;
}
if (i == 1)
{
conb.ft = conm.m*3.2808;
//conb.ft = conm.cm*0.032808;
}
else
{
conb.in = conm.m*39.370;
//conb.in = conm.cm*0.39370;
}
return(conb);
}
The problem is in the part where you try to print out the values. In case of
printf("Metres = %d \n", met.m);
printf("Centimetre = %d \n", met.cm);
and
printf("Feet = %d \n", bri.ft);
printf("Inch = %d \n", bri.in);
You're using %d format specifier to print the value of the variables of float types. You should be using %f instead.
FWIW, using inappropriate type of argument for a format specifier invokes undefined behavior.

Program is not responding in Code::blocks after reading my input

I am trying to make a program within code::blocks that will allow me to select multiple unit conversions to do. However, whenever I build and compile and reach the point in the program where it scans in my input for the variable "choice", code::blocks displays a window immediately afterwards that says that my .exe has stopped working and I can't figure out why this is the case. I am using the GNU GCC Compiler. Any help would be enormously appreciated.
#include <stdio.h>
int main()
{
float fahrenheit;
float celsius;
float pound;
float kilogram;
float inch;
float centimeter;
float mph;
float kph;
int foc;
int pok;
int ioc;
int mok;
int choice;
printf("\n1. Temperature Conversion\n");
printf("\n2. Weight Conversion\n");
printf("\n3. Length Conversion\n");
printf("\n4. Speed Conversion\n");
printf("\n5. Exit Program\n");
printf("\n");
printf("\nEnter the number of the program you would like to run :\n");
printf("\n");
scanf("%d", choice);
//Temperature Conversion
if(choice == 1) {
printf("\n1. Convert from Celsius to Fahrenheit\n");
printf("\n2. Convert from Fahrenheit to Celsius\n");
printf("\nEnter the number that corresponds with the conversion you would like to do:\n");
printf("\n");
scanf("%d", &foc);
if(foc == 1) {
//option 1
printf("\nEnter your temperature in Celsius : ");
scanf("%f", &celsius);
fahrenheit = 32 + (celsius * 1.8);
printf("\nYour temperature in Fahrenheit : %f ", fahrenheit);
}
else {
//option 2
printf("\nEnter your temperature in Fahrenheit : ");
scanf("%f", &fahrenheit);
celsius = (fahrenheit - 32) * 0.55555555;
printf("\nYour temperature in Celsius : %f ", celsius);
}
}
//Weight Conversion
else if(choice == 2) {
printf("\n1. Convert from Pound to Kilogram ");
printf("\n2. Convert from Kilogram to Pound ");
printf("\nEnter the number that corresponds with the conversion you would like to do: ");
printf("\n ");
scanf("%d", &pok);
if(pok == 1) {
//option 1
printf("\nEnter your weight in pounds : ");
scanf("%f", &pound);
kilogram = (2.20462 * pound);
printf("\nYour weight in kilograms : %f ", kilogram);
}
else {
//option 2
printf("\nEnter your weight in kilograms : ");
scanf("%f", &kilogram);
pound = (kilogram/2.20462);
printf("\nYour weight in pounds : %f ", celsius);
}
}
//Length Conversion
else if(choice == 3) {
printf("\n1. Convert from inches to centimeters ");
printf("\n2. Convert from centimeters to inches ");
printf("\nEnter the number that corresponds with the conversion you would like to do: ");
printf("\n ");
scanf("%d", &ioc);
if(ioc == 1) {
//option 1
printf("\nEnter your length in inches : ");
scanf("%f", &inch);
centimeter = (inch/2.54);
printf("\nYour length in centimeters : %f ", centimeter);
}
else {
//option 2
printf("\nEnter your length in centimeters : ");
scanf("%f", &centimeter);
inch = (centimeter*2.54);
printf("\nYour length in inches : %f ", inch);
}
}
//Speed Conversion
else if(choice == 4) {
printf("\n1. Convert from mph to kph ");
printf("\n2. Convert from kph to mph ");
printf("\nEnter the number that corresponds with the conversion you would like to do: ");
printf("\n ");
scanf("%d", &mok);
if(mok == 1) {
//option 1
printf("\nEnter your speed in mph : ");
scanf("%f", &mph);
kph = (mph/1.60934);
printf("\nYour speed in kilometers: %f ", kph);
}
else {
//option 2
printf("\nEnter your speed in kph : ");
scanf("%f", &kph);
mph = (1.60934*kph);
printf("\nYour length in inches : %f ", mph);
}
}
else if(choice == 5) {
printf("\nProgram has ended. Thanks for your time!");
}
else {
printf("\nThat is not a valid program number. ");
}
}
You have to pass the pointer to the choice variable (i.e. &choice) to your scanf() call, just as you've done for your other scanf() uses.
I.e. instead of
scanf("%d", choice);
use
scanf("%d", &choice);
scanf() reads a value from the user and the address of the variable must be represented in the syntax
if not represented the program takes the garbage value so

Resources