I am reading from a txt file using fscanf.
for (int i = 0; i < totalLines; ++i)
{
fscanf(fp,"%s %s %f %f[^\n]", &cp[i].name, &cp[i].animal, &cp[i].coordinates.lat, &cp[i].coordinates.lng);
printf("Name: %s\nAnimal: %s\nLat: %.6f\nLong: %.6f\n\n", cp[i].name, cp[i].animal, cp[i].coordinates.lat, cp[i].coordinates.lng);
}
The problem is it does not set/print the innermost structs members or variables which are 2 doubles lng and lat are not being assigned. What the best way to make this work? am I on the right track? I tried to cast it with a (double) but that through errors. any ideas?
UPDATE
I have updated the code to how it should be and im still getting a 0 value on all. any ideas why?
Considering your structure is as follows:
/* Structure Holding Coordinates */
typedef struct location
{
double lat;
double longt;
}location_t;
typedef struct geo
{
char name[64];
char animal[64];
location_t loc;
} geo_t;
Your Reading is to be done as follows:
geo_t arr[NO_ENTRIES]; /* NO_ENTRIES is total lines in file */
for(i = 0; i< NO_ENTRIES; i++)
fscanf(fp, " %s %s %lf %lf", arr[i].name, arr[i].animal, &arr[i].loc.lat, &arr[i].loc.longt);
printf("After Reading\n\n\n");
for(i = 0; i< NO_ENTRIES; i++)
printf(" %s %s %f %f\n", arr[i].name, arr[i].animal, arr[i].loc.lat, arr[i].loc.longt);
See it here: http://ideone.com/99dmvL
For double, use %lf, just like what you did in the printf. So change the code to:
fscanf(fp,"%s %s %lf %lf", cp[i].name, cp[i].animal, &cp[i].coordinates.lat, &cp[i].coordinates.lng);
As u are trying to read long double values u should use %lf instead of %s where u are scanning double values
example : fscanf(fp,"%lf",&double_value);
use %f instead %s for double values.
fscanf(fp,"%s %s %f %f", cp[i].name, cp[i].animal, &cp[i].coordinates.lat, &cp[i].coordinates.lng);
Also your printf will have an error. I suggest you do these:
printf("Name: %s\nAnimal: %s\nLat: %f\nLong: %f\n\n", cp[i].name, cp[i].animal, cp[i].coordinates.lat, cp[i].coordinates.lng);
Don't use "%lf". Your variables are doubles, so you should use "%f".
In your printf calls, if needed use a format specifier like this: "%.5f". It tells printf to output 5 digits after the decimal point.
You can also refer the fscanf & fprintf documentation.
You have to get the value using the floating point control string. Not a string control string.
fscanf(fp,"%s %s %lf %lf", cp[i].name, cp[i].animal, &cp[i].coordinates.lat, &cp[i].coordinates.lng);
Related
I've tried to build a simple calculator for physics force experiments.
//
// main.c
#include <stdio.h>
int main() {
char name[20];
int month,date;
int difference_percentage1, difference_percentage2, difference_percentage3;
double force1, force2, force3;
scanf("%s", name);
scanf("%d %d", &month, &date);
scanf("%lf %lf %lf", &force1, &force2, &force3);
double Favg=(force1 + force2 + force3)/3.000;
puts(name);
difference_percentage1=100*(force1-Favg)/Favg;
difference_percentage2=100*(force2-Favg)/Favg;
difference_percentage3=100*(force3-Favg)/Favg;
printf("%d %d %d\n", difference_percentage1, difference_percentage2, difference_percentage3);
getchar();
return 0;
}
The calculate doesn't match what I've typed for scanf().
The o's of the % mark for the %f look a bit larger to me than for the %d, so my guess is that you used the wrong % for the %f format specifier which isn't recognized by the compiler; it interprets the double value as int.
Edit: one more reason to post text instead of a screenshot of the code! (o;
Edit2: Yup, your % mark in %f is actually a "EF BC 85" in hex but should be "25"
My array not taking inputs (Ex- 5.21).I am trying to take a float input for each of my array elements but this isn't doing this. and Also this function is not working with the array. please help
float maxJump (double jumpRecord[], int attempts)
{
float max = jumpRecord[0];
for(int i=0; i<attempts; ++i)
{
if(jumpRecord[i]>max)
max = jumpRecord[i];
}
printf("Longest Jump = %.2f meter\n",max);
}
int main()
{
double jumpRecord[5];
printf("Jumping information (in meter): \n");
for(int i=0; i<5; i++)
{
printf("Attempt %d. = ",i+1);
scanf("%.2f",&jumpRecord[i]);
}
maxJump(jumpRecord,5);
return 0;
}
When used with scanf(), %.2f (%f) format specifier is for reading float values.
The elements of jumpRecord are double, so you should use %lf (%.2lf) format specifier instead.
Note that %f should be used for printing double via printf(). %lf is allowed in C99 or later, but %f is more portable.
In my code, squaring the variable R always gives zero.
It does not matter if it's pow(R, 2) or R*R.
Why does that happen?
(If I change it to float, it works, but I need it to be a double for the precision.)
This is the code:
int main () {
double R,PI,a;
PI = 3.14159;
scanf(" %d", &R);
a = PI * (pow(R,2));
printf ( "A= %0.4d \n", a );
return 0;
}
You're using the wrong format specifier in scanf and printf.
The %d format specifier expects the address of an int for scanf, and an int for printf. You're instead passing in the address of a double and a double respectively. Using the wrong format specifiers invokes undefined behavior.
You need to use %lf in scanf and %f in printf for a double:
scanf("%lf", &R);
a = PI * (pow(R,2));
printf ( "A= %0.4f \n", a );
I am very new to C, and while working on a project which requires pulling an indeterminate amount of values from the console, I am finding that it is not pulling the correct values. It seems like addresses, which I believe means it is a pointer issue, but I can't seem to find it.
int getVals(int degree){
double sum;
double x;
double coefs[degree];
for(int counter = 0; counter<=degree; counter = counter+1){
double nxt;
scanf(" %d", &nxt);
coefs[counter] = nxt;
printf("coefs[%d] = %d\n", counter, coefs[counter]);
}
printf(" x ? ");
scanf(" %d", &x);
printf("degree %d x %d\n", degree, x);
sum = poly(x, degree, coefs);
printf ("polynomial evaluate to: %lf\n", sum);
int newDegree;
scanf(" %d", &newDegree);
degree = newDegree;
if(degree>-1){
getVals(degree);
}
else
return degree;
}
Note: poly returns a double result of the evaluated polynomial
I am getting the following infinite loop after entering a degree of 1 and a coefficient of 1.5. It does not allow me to enter an x.
Infinite loop
In scanf(" %d", &newDegree); you should use the "%lf" format specifier (since your values is a double, not an int). Change the format specifier in all your calls to scanf() and "%f" in calls to printf().
Please refer to the documentation at this links printf(3), scanf(3).
I am trying to read a file that has the following contents:
1.0000000e+01 2.9265380e+03 5.0821200e+02 4.3231640e+01
2.0000000e+01 1.0170240e+04 9.2798610e+02 4.0723180e+01
3.0000000e+01 2.1486260e+04 1.1832420e+03 1.0328000e+01
4.0000000e+01 3.3835080e+04 1.1882285e+03 -9.3307000e+00
5.0000000e+01 4.5250830e+04 1.0899705e+03 -1.0320900e+01
6.0000000e+01 5.5634490e+04 9.8935650e+02 -9.8019000e+00
7.0000000e+01 6.5037960e+04 8.9134700e+02 -9.8000000e+00
but I can't seem to find a proper way to read the scientific notation. Here is what I have of the code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
// This is the array to store the input
double time[24], altitude[24], velocity[24], acceleration[24];
double var1, var2, var3, var4;
//This is the pointer declaration for opening a file
FILE * fp = fopen("rocket.txt", "r");
int i = 0;
while(fscanf(fp,"%g %f %f %f", &var1, &var2, &var3, &var4) > 0){
time[i] = var1;
altitude[i] = var2;
velocity[i] = var3;
acceleration[i] = var4;
printf("Time: %f \n", &time[i]);
i++;
}
printf("Time: %f", &time[0]);
fclose(fp);
return(0);
}
I've tried multiple combinations of %f, %g, %d to try and print out the result but I can never get the right thing.
If anyone can point me on the right direction I will greatly appreciate it.
What you want to use is %lf for input and %e for output in scientific notation:
scanf("%lf", &input);
printf("%e\n", input);
You could use a, e, f, or g in the conversion specifier like this:
fscanf(fp, "%a", &input); // NOTE: only with C99 compilers
fscanf(fp, "%e", &input);
fscanf(fp, "%f", &input);
fscanf(fp, "%g", &input);
They will all work for parsing floats, but for doubles you will need to use the length modifier "l" like this:
fscanf(fp, "%le", &input);
To print the values, you can use any of the specifiers but you don't need the length modifier "l":
printf("%e ", input); // or f or g (or a C99 compilers only)
printf("%le ", input); // produces the same thing
A really helpful reference is here:
http://en.cppreference.com/w/c/io/fscanf