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;
}
Related
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
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.
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");
This question already has answers here:
Why do interleaved scanf() + printf() statements result in both scanf() calls executing first, then both printf() calls?
(2 answers)
Closed 5 years ago.
I have made this calculator.
Everything works fine.
However, I want to use a while loop as follows:
char cont = 'y'
while (cont == 'y') {
/code/
}
printf("Do you want to continue (y/n)")
scanf("%c", & cont)
This prints:
Do you want to continue (y/n)
But when I enter something the program ends unexpectedly.
Full Code:
#include < stdio.h >
#include < conio.h >
void main() {
float x, y, result;
int select;
char cont = 'y';
clrscr();
while (cont == 'y') {
clrscr();
printf(
"Please Enter The Respective Number For Following Operation\n1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n"
);
scanf("%d", & select);
clrscr();
switch (select) {
case 1:
{
printf("\nEnter The First Number To Add\n");
scanf("%f", & x);
printf("\nEnter The Second Number To Add\n");
scanf("%f", & y);
clrscr();
result = x + y;
printf("Addition of two numbers %f and %f is %f", x, y,
result);
break;
}
case 2:
{
printf("\nEnter The First Number To Subtract\n");
scanf("%f", & x);
printf("\nEnter The Second Number To Subtract\n");
scanf("%f", & y);
clrscr();
result = x - y;
printf("Subtraction of two numbers %f and %f is %f", x, y,
result);
break;
}
case 3:
{
printf("\nEnter The First Number To Multiply\n");
scanf("%f", & x);
printf("\nEnter The Second Number To Multiply\n");
scanf("%f", & y);
clrscr();
result = x * y;
printf("Multiplication of two numbers %f and %f is %f", x,
y, result);
break;
}
case 4:
{
printf("\nEnter The Numerator\n");
scanf("%f", & x);
printf("\nEnter The Denominator\n");
scanf("%f", & y);
clrscr();
result = x / y;
printf("\nDivision of two numbers %f and %f is %f", x, y,
result);
break;
}
default:
{
printf("Invalid Choice");
break;
}
}
printf("\n\nCalculator By XXX\n\nDo you want to Continue (y/n)\n");
scanf("%c", & cont);
}
getch();
}
Your while loop terminates because the scanf("%c",&cont); reads the leftover \n from the buffer which makes your while statement to fail.
You should modify your scanf to scanf(" %c",&cont);.
Put that scanf statement inside while loop or you can use do..while loop in place of while.
do {
// code
printf("Do you want to continue (y/n)"); scanf("%c",&cont);
} while(cout=="y");
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