Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm stuck on this code and have searched the web for an answer and I didn't find anything.
The code get compiled but in options 1 and 2 the code doesn't get the real answer, I don't know if the problem is about the all thing like double and float or because of a stupid mistake.
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#define PI 3.141
void twoPoints ();
void hypotenuseInTriangular ();
void areaAndScopeInCircle (float radius);
void areaOfSquare ();
void areaOfRectangle ();
int main()
{
int num=0 ;
float radius=0;
double ans1= 0,x1=0,x2=0,y1=0,y2=0;
printf("hello friend, please enter one of the options: \n");
printf("press 1 to calculate length between two points\n");
printf("press 2 to calculate hypotenuse in right tringle \n");
printf("press 3 to calculate area and scope in circle \n");
printf("press 4 to calculate area of square \n");
printf("press 5 to calculate area of rectangle \n");
printf("press 6 to exit from the code \n");
scanf("%d",&num);
switch (num)
{
case 1:
twoPoints(x1,x2,y1,y2);
break;
case 2:
hypotenuseInTriangular();
break;
case 3:
areaAndScopeInCircle (radius);
break;
case 4:
areaOfSquare();
break;
case 5:
areaOfRectangle();
break;
case 6:
printf("bye!\n");
break;
default:
printf("not good number \n");
break;
}
system("PAUSE");
return (0);
}
/**
this funcion calculate the length between two points.
input: two points (x1,y1) (x2,y2).
output: the length between them.
*/
void twoPoints (double x1,double x2,double y1,double y2)
{
double ans1=0,calculate1=0,calculate2=0;
printf("enter two integer points like this: x1 y1 x2 y2\n");
scanf("%1f %1f %1f %1f",&x1,&y1,&x2,&y2);
calculate1 = (x1 - x2) * (x1 - x2);
calculate2 = (y1 - y2) * (y1 - y2);
calculate1 += calculate2;
calculate2 = sqrt(calculate1);
printf("the length between your two points is: %f\n",calculate2);
}
/**
this funcion calculate the hypotenuse in right tringle using two ribs.
input: two integer numbers of the tringle.
output: the hypotenuse length.
*/
void hypotenuseInTriangular (double sideA,double sideB)
{
double sideC=0.0,sum=0.0;
printf("enter two ribs from the triangle: \n");
scanf("%1f %1f",&sideA,&sideB);
sum = (sideA * sideA + sideB * sideB);
sideC = sqrt(sum);
printf("your hyptenuse is %f \n",sideC);
}
/**
this funcion calculate area and scope using radius.
input: a radius number.
output: the area and the scope of this circle.
*/
void areaAndScopeInCircle (float radius)
{
float area = 0,scope =0;
printf("enter radius: \n");
scanf("%f",&radius);
area = PI * radius * radius;
scope = PI * radius * 2;
printf("the area of your circle is: %.1f\n",area);
printf("the scope of your circle is: %.1f\n",scope);
}
/**
this funcion calculate area of square using two ribs.
input: two integer numbers.
output: the area of the square.
*/
void areaOfSquare (int rib)
{
printf("enter one of the square rib: \n");
scanf("%d",&rib);
printf("the area of your square is: %d\n",(rib*rib));
}
/**
this funcion calculate area of rectangle using two ribs.
input: two integer numbers.
output: the area of the rectangle.
*/
void areaOfRectangle (int length,int width)
{
printf("enter length and than width\n");
scanf("%d %d",&length,&width);
printf("the area of your rectangle is: %d\n",(length*width));
}
In your function twoPoints() you have used incorrect format specifiers with
scanf("%1f %1f %1f %1f",&x1,&y1,&x2,&y2);
these could be typos or copying errors because a 1 (numeral one) looks a lot like an l (letter ell). The line should be
scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2);
and after making this correction the function works.
Note that the lower-case L in the format specifier stands for long. In scanf() and friends, for a float variable input you use %f and for a double variable you use %lf. However in printf() and friends, you use %f for double and for float because any float argument is automatically promoted to double.
In your function twoPoints -
scanf("%1f %1f %1f %1f",&x1,&y1,&x2,&y2);
instead of above try this -
scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2); // %lf not %1f
And similar in other funcitons .
Note - Also , your function when defined expect arguments , but when you call them in main , you don't pass them any arguments. Such as this -
hypotenuseInTriangular(); // expects 2 double arguments
Call them with correct arguments , and also declare prototypes correctly .
Related
#include <stdio.h>
int a, b, h, area, perimeter;
float pi;
void rectangle() {
printf("enter rectangular base length");
scanf("%d", &a);
printf("enter rectangular base height\n");
scanf("%d", &b);
area = a * b;
perimeter = (a + b) * 2;
printf("area of a rectangle = %d\n", area);
printf("perimeter of a rectangle = %d", perimeter);
}
void circle() {
pi = 3.14;
printf("enter the length of the circle radius\n");
scanf("%d", &a);
area = pi * a * a;
perimeter = 2 * pi * a;
printf("area of the circle = %d\n", area); //I want this part to conversion from int to float
printf("perimeter of the circle = %d", perimeter);
}
int main() {
printf("Choose which way you will operate\n");
printf("circle=1\nrectangle=2\n");
scanf("%d", &h);
switch (h) {
case 1:
printf("you chose a circle\n");
circle();
break;
case 2:
printf("you chose a rectangle\n");
rectangle();
break;
}
}
As I wrote in the code, I want the part I specified to be converted from int to float. How can I do it?
I was know like that but it didn't work -> (float)area = a * b;
You would probably want to define PI as a constant. Prefer to minimize scope of variables (now local to the function that needs them). Use floating point types (float, double etc) when needed to store fractional values with more than integer precision. Make sure you use the %f to print them (optionally, specify how many digits you want to see here %.1f means 1 fractional digit):
#define PI 3.14
void circle() {
printf("enter the length of the circle radius\n");
float a;
scanf("%f", &a);
float area = PI * a * a;
float perimeter = 2 * pi * a;
printf("area of the circle = %.1f\n", area);
printf("perimeter of the circle = %.1f", perimeter);
}
I found that your code here:
area = pi * a * a;
where "pi" you defined it as a float constant, so compiler will warn at this line something like "assign float to a integer", you should know that no matter how many integers in a formula, even one float number in it, the type of final value will be automatically converted to float
means the result of:
pi * a * a
will be a float (or double) value. But you assign this value to a int variable "area", so that value then cast to int.So you should declare area as:
float area; // or double area
By your comment in this line:
printf("area of the circle = %d\n", area); //I want this part to conversion from int to float
I guess you want to print a number with decimal part.
You can try this:
printf("area of the circle = %.3f\n", area);
".3" after "%" means print 3 decimal digits, or to say the precision is 3; "f" after ".3" means print argument 1 as a float number.
Hello guys I started learning C few weeks ago and I am trying to make my first useful program, I am trying to create a squared equation calculator but no matter what I type in the input it always outputs "no solution2" can anyone take a look and help me ?
examples for input :
0 0 0=
1 4 1=
#include <stdio.h>
#include <math.h>
int main()
{
printf("enter a\n");
double a; scanf("%1f", &a);
printf("\nenter b\n");
double b; scanf("%1f", &b);
printf("\nenter c\n");
double c; scanf("%1f", &c);
if (a==0)
{
if (b==0)
{
if (c==0)
printf("x can be every number\n");
else
printf("no solution1\n");
}
else
{
printf("x equals %.2f\n", ((-1)*c) / b);
}
}
else
{
double delta = (b*b)-(4*(a*c));
if (delta>0)
{
double sqrtDlt = sqrt(delta);
printf("x1 = %4.2f\n", (((-1)*b) + sqrtDlt) / 2 * a);
printf("x2 = %4.2f\n", (((-1)*b) - sqrtDlt) / 2 * a);
}
else
printf("no solution2\n");
}
return 0;
}
For double use specifier %l(ell)f not %1(one)f in scanf.
Note that this is one place that printf format strings differ substantially from scanf (and fscanf, etc.) format strings. For output, you're passing a value, which will be promoted from float to double when passed as a variadic parameter. For input you're passing a pointer, which is not promoted, so you have to tell scanf whether you want to read a float or a double, so for scanf, %f means you want to read a float and %lf means you want to read a double.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
But the calculation doesn't work or change when I run it and plug in any random number...need some guidance. I'm a novice to C and programming in general so please include easy to understand help.
#include <stdio.h>
double const change_celcius = 32.0;
double const change_kelvin = 273.15;
void temperatures(double n);
int main(void)
{
int q = 'q';
double user_number;
printf("Enter the fahrenheit: \n");
scanf("%f", &user_number);
while (user_number != q)
{
temperatures(user_number);
printf("\n");
printf("Enter the fahrenheit: \n");
scanf("%f", &user_number);
}
}
void temperatures(double n)
{
double celsius, kelvin;
celsius = 5.0 / 9.0 * (n - change_celcius);
kelvin = 5.0 / 9.0 * (n - change_celcius) + change_kelvin;
printf("fahrenheit: %.2f - celsius is: %.2f - kelvin is: %.2f",
n, celsius, kelvin);
}
I don't believe the all the use %lf instead of %f comments, by themselves, fix your program. The handling of q (for "quit") is also problematic so let's fix that too. First, we'll use POSIX function getline() to read it into a string and test if it's "q". If not, we'll sscanf it into a double and use it as our temperature:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
double const change_celsius = 32.0;
double const change_kelvin = 273.15;
void temperatures(double n)
{
double celsius = 5.0 / 9.0 * (n - change_celsius);
double kelvin = 5.0 / 9.0 * (n - change_celsius) + change_kelvin;
printf("fahrenheit: %.2f - celsius is: %.2f - kelvin is: %.2f\n", n, celsius, kelvin);
}
int main(void)
{
char *user_string = NULL;
ssize_t user_string_length;
size_t user_string_capacity = 0;
while (1)
{
printf("Enter the fahrenheit: ");
if ((user_string_length = getline(&user_string, &user_string_capacity, stdin)) < 1)
break;
if (strncmp(user_string, "q\n", (size_t) user_string_length) == 0)
break;
double user_number;
if (sscanf(user_string, "%lf", &user_number) == 1)
temperatures(user_number);
}
if (user_string != NULL)
free(user_string); // free memory allocated by getline()
if (user_string_length == -1)
putchar('\n'); // output courtesy newline if user used ^D to exit
return(0);
}
We check the return value of sscanf so that bad input won't cause the program to recalculate using the last good input. Instead, it will just prompt again for input.
You need to use "%lf" in scanf() and print() to read and write the value of type double.
Note that the printf() will work with "%f" too.
For more details please refer : Why does scanf() need "%lf" for doubles, when printf() is okay with just "%f"?
There are several issues that can be addressed in your code. First, always (Always, in case it wasn't clear) check the return of scanf. That is the only way you know whether the expected number of conversions took place -- and whether you have an actual value to work with in your code.
The return also holds the key to exiting the loop when the user enters 'q' (or anything that causes the conversion to double to fail). By simply checking
if (scanf(" %lf", &user_number) == 1)
You can determine whether to process the value as a temperature, or tell the user has indicated exit.
Another tip, never (Never) write:
printf ("\n");
Why would you want to call a variadic function simply to output a single char? That is what putchar (or fputc) is for, e.g.:
putchar ('\n');
Putting those pieces together, and noting that %lf is used as the format specifier for double, you can rewrite your code, and format the output in quite a bit fewer lines, e.g.
#include <stdio.h>
double const change_celcius = 32.0;
double const change_kelvin = 273.15;
void temperatures (double n);
int main(void)
{
double user_number;
while (printf ("\nEnter temp in degrees fahrenheit: ") &&
scanf(" %lf", &user_number) == 1)
temperatures(user_number);
return 0; /* main() is type 'int' and returns a value to the shell */
}
void temperatures (double n)
{
double celsius, kelvin;
celsius = 5.0 / 9.0 * (n - change_celcius);
kelvin = 5.0 / 9.0 * (n - change_celcius) + change_kelvin;
printf(" fahrenheit: % 7.2lf\n celsius is: % 7.2lf\n kelvin is : % 7.2lf\n",
n, celsius, kelvin);
}
Example Use/Output
$ ./bin/temps
Enter temp in degrees fahrenheit: 212
fahrenheit: 212.00
celsius is: 100.00
kelvin is : 373.15
Enter temp in degrees fahrenheit: 0
fahrenheit: 0.00
celsius is: -17.78
kelvin is : 255.37
Enter temp in degrees fahrenheit: 68
fahrenheit: 68.00
celsius is: 20.00
kelvin is : 293.15
Enter temp in degrees fahrenheit: q
Always compile your code with at minimum -Wall -Wextra warnings enabled (and if you really want to drill down, add -pedantic). Read the warnings and fix them. All of your code should compile without warning before you consider your code reliable at this stage of the game.
Look all answers over, and let me know if you have any questions.
To read Double use %lf instead of %f.
for double printf() will work with %f also.
The calculation seems to be okay; however the you are not reading in the words properly
scanf("%f", &user_number);
You are stating you are reading in a float, yet you are declaring user_name as a double. If you wanted to use a float, you would need to change the user_name declaration from double to float. If you wanted to use a double use "%f".
After compiling, my GetInt function causes the printf statements within the function to be printed on the screen three times. I believe this was caused when I initialized all radius, base, and height to GetInt(void) but I see no other way of accurately initializing those variables. Please help!
#define _CRT_SECURE_NO_WARNINGS
#define PI 3.14159
#include <stdio.h>
#include <math.h>
int GetInt(void);
double CalcTriangleArea(int base, int height);
double CalcCircleArea(int radius);
int main(void)
{
int radius, base, height;
double triangleArea;
double circleArea;
radius = GetInt();
base = GetInt();
height = GetInt();
triangleArea = CalcTriangleArea(base, height);
circleArea = CalcCircleArea(radius);
return(0);
}
int GetInt(void)
{
int x;
{
printf("Please enter a radius: \n\n");
scanf("%d", &x);
printf("Please enter a base: \n\n");
scanf("%d", &x);
printf("Please enter a height: \n\n");
scanf("%d", &x);
}
return(x);
}
double CalcTriangleArea(int base, int height)
{
double triangleArea;
printf("Triangle area is %.2f \n\n", triangleArea = .5*base*height);
return(0);
}
double CalcCircleArea(int radius)
{
double circleArea;
printf("Area is %.4f \n\n", radius, circleArea = PI * pow(radius, 2));
return(0);
}
A rule of thumb is to avoid repeating yourself whereever possible and don't repeat yourself. Imagine you want to change from two new lines (\n\n) to three (\n\n\n)? You would need to make that change three times.
Looking at the bare bones of GetInt, you are printing a prompt, two new lines, get a value and returning it. Thus, we can write the new function like this:
void getInt(char* prompt)
{
int x, numberOfConversions; // numConversions is the number of int's read from the keyboard buffer
printf("%s: \n\n", prompt);
numberOfConversions = scanf("%d", &x);
while (numberOfConversions != 1) // while the user did not enter a number
{
printf("Please enter a number: ");
numberOfConversions = scanf("%d", &x)"
}
return x; // Always returns a valid number
}
GetInt asks for, and reads, 3 distinct values, yet returns only the last one, every time it is called.
I think what you really want is to have GetInt ask for and return just 1 value, either passing it the prompt to print or printing it before calling it.
I have made a verry simple calculation prog to teach myself C. but i have an odd bug now and would like to ask some advice.
About an hour ago i also asked help here to get rid of global variables and got some suggestions (again thanks for that).
Now i have implemented those suggestions but when i now input a diameter in the calculation it returns with a verry large and odd number for al 3 functions.
And that number is the same for all 3 functions.
I don't understand where the function is getting that number from as i define the diameter variable in the cir_user_input() (input.c) first and only call it again in getRadius(float diameter) (circlefunctions.c). i have tried several things like pointers or make use of the EXTERN statement but that did not gave me the result that i wanted/expected. Hopefully someone can point me out on what i do wrong here or can suggest some documentation on how to solve this?
main.c
#include <stdio.h>
#include "menu/menu.h"
#include "circle/circlemenu.h"
#include "input/input.h"
int main(void)
{
while(1)
{
menu();
switch(menu_user_input())
{
case 1:
info_top();
cir_user_input();
info_bottom();
break;
case 2:
system("cls");
break;
case 3:
system("cls");
break;
case 8:
system("cls");
break;
case 9:
system("cls");
break;
case 0:
return(0);
default:
system("cls");
printf("\n **Wrong choice try again...**\n");
break;
}
}
return 0;
}
circlemenu.c
#include <stdio.h>
#include "circlemenu.h"
void info_top()
{
system("cls");
printf(" ----------------------------------------\n");
printf(" Typ the diameter of the circle: ");
}
void info_bottom(double diameter)
{
printf(" ----------------------------------------\n");
printf(" The radius = %f \n\n" , getRadius(diameter));
printf(" The surface = %f \n\n" , getSurface(diameter));
printf(" The outline = %f \n" , getOutline(diameter));
printf(" ----------------------------------------\n");
}
circlefunctions.c
#include "circlefunctions.h"
#include "../input/input.h"
#define PI 3.14
double getRadius(float diameter)
{
double radius = diameter / 2;
return radius;
}
double getSurface(float diameter){
double radius = getRadius(diameter);
return PI * (radius * radius);
}
double getOutline(float diameter){
double radius = getRadius(diameter);
return 2 * PI * radius;
}
input.c
#include <stdio.h>
#include "input.h"
int menu_user_input()
{
int number;
scanf(" %d", &number);
return number;
}
float cir_user_input()
{
float diameter;
scanf(" %e", &diameter);
return diameter;
}
edit
I forgot to mention that im verry new to programming and im just trying to learn it myself. Some things may seem to be odd but im just trying to tackle and understand one problem at the time.
info_bottom(); you should pass diameter as argument i.e. info_bottom(diameter);
Adding to #JerryGoyal 's answer, In your main.c
case 1:
info_top();
cir_user_input();
info_bottom(); // here
break;
In the commented line, you need to give info_bottom() an argument when you call it. Something like info_bottom( diameter ); where diameter is a variable of type double you must declare in your main.c
If you had your compiler Warnings turned on, it should have warned you about this.
You should also make the diameter in
double getRadius(float diameter)
double getSurface(float diameter)
and
double getOutline(float diameter)
of type double. In other words, the arguments should be double diameter