Why is this prgram not working in C? - c

I program doesn't work after entering P or A. why?
it's challenge from udemy course. I am just a beginner in programming :)
#include <stdio.h>
#include <stdlib.h>
int main()
{
float height=0;
float width=0;
float area=0;
float perimeter=0;
printf("Enter height of rectangle :");
scanf("%f",&height);
printf("Enter width of rectangle :");
scanf("%f", &width);
char choice;
printf("Enter P for perimeter and A for area: ");
scanf(" %c", choice);
if(choice=='P' || choice=='p'){
printf("Width: %f", width);
printf("Height: %f", height);
perimeter= 2.0*(height*width);
printf("Perimeter of rectangle is: %f", perimeter);
}else if(choice=='A' || choice=='a'){
printf("Width: %f", width);
printf("Height: %f", height);
area= (height*width);
printf("Area of rectangle is: %f", area);
}else
printf("Invalid Input");
}

Correct this as follows:
scanf(" %c", &choice);
&choice will point to an address where you want to store the value.

This is not working Because you forgot & in scanf(" %c", choice); . It should be scanf(" %c", &choice); . In scanf function , you have to provide the address of the item to be scanned (Similar to scanf("%f", &height);).
Modified code :-
#include <stdio.h>
#include <stdlib.h>
int main()
{
float height = 0;
float width = 0;
float area = 0;
float perimeter = 0;
printf("Enter height of rectangle :");
scanf("%f", &height);
printf("Enter width of rectangle :");
scanf("%f", &width);
char choice;
printf("Enter P for perimeter and A for area: ");
scanf(" %c", &choice);
if (choice == 'P' || choice == 'p')
{
printf("Width: %f", width);
printf("Height: %f", height);
perimeter = 2.0 * (height * width);
printf("Perimeter of rectangle is: %f", perimeter);
}
else if (choice == 'A' || choice == 'a')
{
printf("Width: %f", width);
printf("Height: %f", height);
area = (height * width);
printf("Area of rectangle is: %f", area);
}
else
printf("Invalid Input");
}
Output :-
Enter height of rectangle :4
Enter width of rectangle :5
Enter P for perimeter and A for area: A
Width: 5.000000Height: 4.000000Area of rectangle is: 20.000000
I recommend to add \n before your printf() statements for better readability.

Related

My switch case is not working when I enter 1 my case doesnt work

When I run my code my switch cases are not working when I enter 1 it doesn't print case 1 and so on I try every thing but didn't work can someone help me please
When I run my code my switch cases are not working when I enter 1 it doesn't print case 1 and so on I try every thing but didn't work can someone help me please
#include <stdio.h>
#include <math.h>
int main ()
{
int choice;
float area;
printf("1 for area of Square\n");
printf(" 2 for area of Circle\n");
printf(" 3 for finding area of rectangle\n");
switch(choice) {
case 1: {
float side,area;
printf("Enter Sides of Square");
scanf("%f",&side);
area=(float)side*side;
printf("Area of Square is %f",area);
break;
}
case 2: {
float radius,area;
printf("Enter Radius of Circle");
scanf("%f",&radius);
area=(float)3.14159*radius*radius;
printf("Area of Circle %f",area);
break;
}
case 3: {
float len,breadth,area;
printf("Enter Length and Breadth of Rectangle");
scanf("%f %f",&len,&breadth);
area=(float)len*breadth;
printf("Area of Rectangle is %f",area);
break;
}
default: {
printf("Invalid Choice");
break;
}
}
return 0;**strong text**
}
you are missing to take user input. I have added scanf below line and it works now
scanf("%d", &choice);
complete version of your code
#include <stdio.h>
#include <math.h>
int main ()
{
int choice;
float area;
printf ("1 for area of Square\n");
printf (" 2 for area of Circle\n");
printf (" 3 for finding area of rectangle\n");
scanf("%d", &choice);
switch (choice)
{
case 1:
{
float side, area;
printf ("Enter Sides of Square");
scanf ("%f", &side);
area = (float) side *side;
printf ("Area of Square is %f", area);
break;
}
case 2:
{
float radius, area;
printf ("Enter Radius of Circle");
scanf ("%f", &radius);
area = (float) 3.14159 *radius * radius;
printf ("Area of Circle %f", area);
break;
}
case 3:
{
float len, breadth, area;
printf ("Enter Length and Breadth of Rectangle");
scanf ("%f %f", &len, &breadth);
area = (float) len *breadth;
printf ("Area of Rectangle is %f", area);
break;
}
default:
{
printf ("Invalid Choice");
break;
}
}
return 0;
}
Output:
1 for area of Square
2 for area of Circle
3 for finding area of rectangle
1
Enter Sides of Square2
Area of Square is 4.000000

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;
}

Scanf always returning 0.000000

I'm trying to make a simple program to calculate the body mass index, but the scanf(s) always return 0.00000, no matter what i try. I searched everywhere, tried many things,
Thanks to everyone.
#include <stdio.h>
#include <stdlib.h>
int main() {
float height;
float initialheight;
float weight;
float bmi;
float nothing;
printf("What's your weight? ");
scanf("%lf", &weight);
printf("%f", &weight);
printf("What's your height? ");
scanf("%lf", &initialheight);
printf("%f", &initialheight);
height = (initialheight * initialheight);
printf("%f", &height);
bmi = (weight / height);
printf("Your BMI is ");
printf("%f", &bmi);
scanf("%f", nothing); //just to keep the program open
return 0;
}
If you print a value you dont have to print the adress!
So change this:
printf("%f", &weight);
to this:
printf("%f", weight);
So that you actually print the value
An also you have to change %lf to %f in your scanf
So your program should look something like this:
#include <stdio.h>
#include <stdlib.h>
int main(){
float height, initialheight, weight, bmi;
printf("What's your weight?\n>");
scanf(" %f", &weight);
printf("%.2f\n\n", weight);
printf("What's your height?\n>");
scanf(" %f", &initialheight);
printf("%.2f\n\n", initialheight);
height = (initialheight * initialheight);
bmi = (weight / height)*10000;
printf("Your BMI is ");
printf("%.2f\n\n", bmi);
system("pause");
return 0;
}
As an example with the input:
70 and 175
The result/ BMI is:
22.86
Side Note:
BMI = mass(kg) / (height(m) * height(m))
BMI = mass(lb) / (height(in) * height(in)) * 703
Well you have to change two things. First, change printf("%f", &weight) to printf("%f", weight). And also change scanf("%lf", &weight) to scanf("%f", &weight) will make your program fine.

Implementing a BMI Calculator in C

I am having trouble making the BMI calculator program and I am wondering what I haven't done correctly. I am a beginner so go easy on me, thanks!
#include <stdio.h>
main()
{
// Variables for height, weight, and bmi
float height;
float weight;
float bmi;
printf("\aEnter your height: ");
scanf(" %f", height);
printf("\a\nEnter your weight: ");
scanf(" %f", weight);
bmi = (height * 4.88) / (weight * weight);
printf("\a\nYour BMI is: %f", bmi);
getchar();
return 0;
}
scanf requires a pointer to parameters following the format string.
Use the & operator thus:
printf("\aEnter your height: ");
scanf(" %f", &height);
printf("\a\nEnter your weight: ");
scanf(" %f", &weight);
You need to pass the address of your variables to scanf, so that it can modify the values at that address:
scanf(" %f", &height);
^
\ Address-of operator (Returns the memory address of the float)
And:
scanf(" %f", &weight);

What am I doing wrong with this C program?

I have the following code, and the only thing I can physically see it evaluating is line 18 which is the call to printf(). It doesn't go any further.
#include <stdio.h>
#include <stdlib.h>
int main (void) {
int cylNum;
double disp, pi, stroke, radius;
pi = 3.14159;
printf("Welcome to the Engine Displacement Calculator!\n");
cylNum = scanf("Enter number of cylinders (then press enter): \n");
stroke = scanf("Enter stroke: \n");
radius = scanf("Enter radius: \n");
disp = radius * radius * pi * stroke * cylNum;
printf("Displacement is: %f", disp);
getchar();
printf("Press any key to exit!");
return 0;
}
the variables you're trying to read should be parameters to "scanf()", not the result of scanf():
printf("Enter number of cylinders (then press enter): ");
scanf("%d", &cylNum);
...
The scanf function is to read in values.
So the line
cylNum = scanf("Enter number of cylinders (then press enter): \n");
should be the following lines
printf("Enter number of cylinders (then press enter): \n");
scanf("%d", &cylNum);
You need to check the return value of scanf to make sure that it is 1, i.e. conversion has taken place.
So perhaps the code should read
do {
printf("Enter number of cylinders (then press enter): \n");
} while (scanf("%d", &cylNum) != 1);
For the variables disp, pi, stroke, radius you need to use "%lf" in the scanf function instead of "%d.
See scanf and printf
"scanf" doesn't take parameter like the way you are trying.
printf("Enter number of cylinders (then press enter): \n");
scanf(" %d", &cylNum);
printf("Enter stroke: \n");
scanf(" %lf", &stroke);
#include <stdio.h>
#include <stdlib.h>
int main (void) {
int cylNum;
float disp, pi, stroke, radius;
pi = 3.14159;
printf("Welcome to the Engine Displacement Calculator!\n\n");
printf("Enter number of cylinders (then press enter): ");
scanf("%d", &cylNum);
printf("Enter stroke: ");
scanf("%f", &stroke);
printf("Enter radius: ");
scanf("%f", &radius);
disp = radius * radius * pi * stroke * cylNum;
printf("Displacement is: %f\n\n", disp);
return 0;
}

Resources