I am using the command line to get input of 3 floats separated by spaces. e.g. "1.234 5.678 9.012".
I am currently using:
float xyz[3] = {0};
scanf("%f", xyz);
print(%f %f %f, xyz[0], xyz[1], xyz[2]);
However the output is "1.234000 0.000000 0.000000".
Expected output is "1.234000 5.678000 9.012000"
EDIT: I need to use floats instead of integers.
Maybe this
#include <stdio.h>
int main(int argc, char** argv)
{
float vals[3];
printf("input 3 float values: ");
int res = scanf("%f %f %f", &vals[0], &vals[1], &vals[2]);
if(res == 3)
{
printf("read 3 floats %f %f %f\n", vals[0], vals[1], vals[2]);
} else
{
printf("failed to read 3 float values\n");
}
return 0;
}
If you use command line arguments , simply try like
#include<stdio.h>
#include<stdlib.h>
main(int c, char **v)
{
float xyz[3]={0};
xyz[0]=atof(v[1]);
xyz[1]=atof(v[2]);
xyz[2]=atof(v[3]);
printf("%f %f %f\n",xyz[0],xyz[1],xyz[2]);
}
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"
i have this data in file
ATOM 1 N MET A 1 25.909 16.164 -8.037
ATOM 2 CA MET A 1 25.498 14.786 -8.206
ATOM 3 C MET A 1 26.612 13.934 -8.806
ATOM 4 O MET A 1 27.442 14.402 -9.588
this is my full code
#include<stdio.h>
#include<math.h>
typedef struct prot {
char atom[10];
int atomno;
char site[10];
char residue[10];
char chain;
int residueno;
float x, y, z;
} Td;
int main()
{
FILE*fd;
fd=fopen("2zoi.pdb","r+");
Td data[1300];
char buffer[1300];
int a=0;
while(fgets(buffer, 1300, fd))
{
sscanf(buffer, "%s %d %s %s %c %d %f %f %f",data[a].atom,&data[a].atomno,data[a].site,
data[a].residue,&data[a].chain,&data[a].residueno,&data[a].x, &data[a].y, &data[a].z);
printf("%s %d %s %s %c %d %6.3f %6.3f %6.3f \n",data[a].atom,data[a].atomno,data[a].site,
data[a].residue,data[a].chain,data[a].residueno,data[a].x, data[a].y, data[a].z);
a++;
}
//---user input
int fr,sr;
int fa,sa;
int i=0;
float x1,x2,y1,y2,z1,z2;
float distance=0;
printf("Enter first no of atom :");
scanf("%d",&fa);
printf("Enter second no of atom :");
scanf("%d",&sa);
while(data[i].atomno>=0)
{
if(fa==data[i].atomno)
{
x1=data[i].x;
y1=data[i].y;
z1=data[i].z;
}
else if(sa==data[i].atomno)
{
x2=data[i].x;
y2=data[i].y;
z2=data[i].z;
}
i++;
}
printf("%f %f %f",x1,y1,z1);
printf("%f %f %f",x2,y2,z2);
//distance= sqrt(pow((x2 - x1), 2) + pow((y2 - y1), 2) + pow((z2 - z1), 2));
//printf("%6.3f",distance);
return 0;
}
the problem is at this part
printf("%f %f %f",x1,y1,z1);
printf("%f %f %f",x2,y2,z2);
i try to return to values from above loop where the value for x1,y1,z1 is for the first atom and x2,y2,z2 for second atom.
when i input 3 and 4, it gives answer
26.612 13.934 -8.806
27.442 14.402 -9.588
which is correct. but when i input 1 and 2, it gives rubbish answer. it seems like i cant input number 2 also number 10.did i do any mistake?
You have a big problem with data array values: not all elements of the array are set to 0 then the final loop can be broken.
You can use memset to reset to 0 all bytes of data array.
#include<stdio.h>
#include<math.h>
#include <string.h>
typedef struct prot
{
char atom[10];
int atomno;
char site[10];
char residue[10];
char chain;
int residueno;
float x, y, z;
} Td;
int main()
{
FILE*fd;
Td data[1300];
char buffer[1300];
size_t a=0;
fd=fopen("2zoi.pdb","r+");
if (fd != NULL)
{
memset(data, 0x00, sizeof(data));
while ((fgets(buffer, 1300, fd)) && (a < sizeof(data)/sizeof(data[0])))
{
sscanf(buffer, "%s %d %s %s %c %d %f %f %f",data[a].atom,&data[a].atomno,data[a].site,
data[a].residue,&data[a].chain,&data[a].residueno,&data[a].x, &data[a].y, &data[a].z);
printf("%s %d %s %s %c %d %6.3f %6.3f %6.3f \n",data[a].atom,data[a].atomno,data[a].site,
data[a].residue,data[a].chain,data[a].residueno,data[a].x, data[a].y, data[a].z);
a++;
}
//---user input
int fr,sr;
int fa,sa;
size_t i=0;
float x1,x2,y1,y2,z1,z2;
float distance=0;
printf("Enter first no of atom :");
scanf("%d",&fa);
printf("Enter second no of atom :");
scanf("%d",&sa);
while ((data[i].atomno>=0) && (i < sizeof(data)/sizeof(data[0])))
{
if(fa==data[i].atomno)
{
x1=data[i].x;
y1=data[i].y;
z1=data[i].z;
}
else if(sa==data[i].atomno)
{
x2=data[i].x;
y2=data[i].y;
z2=data[i].z;
}
i++;
}
printf("%f %f %f\n",x1,y1,z1);
printf("%f %f %f\n",x2,y2,z2);
}
else
{
fprintf(stderr, "Error opening file\n");
exit(1);
}
return 0;
}
You should check the fopen return value (as you can see in code posted), but as a standard rule: you should always check functions return values.
#include <stdio.h>
#include <math.h>
int main()
{
int i;
int result;
float f;
while((result = scanf("%d", &i)) != EOF)
{
scanf("%f", &f);
printf("%.0f %.0f %.0f\n", floor(f), round(f), ceil(f));
}
printf("Done.\n");
return 0;
}
Hi,
I just began with C and I'm having a problem solving a question.
The problem is that with the user input, I need to get three sets of numbers that are floored, rounded, and ceiled. This process must be ongoing until the user stops by EOF command (Ctrl-D).
When I run my code above, input a value 3.1415, I get 0 0 1 as an output, which is wrong because it's supposed to be 3 3 4.
Any suggestions or help on how to fix the problem would be appreciated.
According to your code, you first need to input an integer value and then enter a float value.
OR, you can start accepting float value like this:
#include <stdio.h>
#include <math.h>
int main()
{
int result;
float f;
while((result = scanf("%f", &f)) != EOF)
{
printf("%.0f %.0f %.0f\n", floor(f), round(f), ceil(f));
}
printf("Done.\n");
return 0;
}
According to your code you need to input integer first then float, But if you enter float value first, that value read by i first and return 0 that is !=EOF, so second scanf does not wait for input, because it is inside the while loop. So always you will get 0 0 1 for all inputs!
To scan a number inside while loop use-
if (scanf("%f", &f) == 0) {
printf("Err. . .\n");
do {
c = getchar();
}
while (!isdigit(c));
ungetc(c, stdin);
Else scan float value first instead of int and float. Try this code-
while((result = scanf("%f", &f)) != EOF)
{
printf("%.0f %.0f %.0f\n", floor(f), round(f), ceil(f));
}
printf("Done.\n");
I have customer data such as customer no, the coordinates of location etc. And there are 25 customers in the text file.
Here's my code. This gives me an output of zeros when I print it.
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#define customerCount 25
struct customerData
{
int customerNo;
double xCoordinate;
double yCoordinate;
double demand;
double readyTime;
double dueTime;
double serviceTime;
};
int main()
{
int i;
struct customerData allSubscriber[customerCount];
FILE *dosya;
dosya = fopen("c:\\solomon_c101.txt", "r");
for(i=1; i<=customerCount; i++)
{
fscanf(dosya, "%d %f %f %f %f %f %f", &allSubscriber[i].customerNo, &allSubscriber[i].xCoordinate, &allSubscriber[i].yCoordinate, &allSubscriber[i].demand, &allSubscriber[i].readyTime, &allSubscriber[i].dueTime, &allSubscriber[i].serviceTime);
}
fclose(dosya);
for(i=1; i<=customerCount; i++)
{
printf("%f\n", &allSubscriber[i].xCoordinate);
}
getch();
return 0;
}
Use this:
printf("%f\n", allSubscriber[i].xCoordinate);
instead of
printf("%f\n", &allSubscriber[i].xCoordinate);
As another user pointed out,use %lf as the format specifier for double in fscanf().In printf() it gets promoted to double.
Don't use conio.h and getch().Those are not standard C.
use the %lf to read a double in scanf
printf("%f\n", &allSubscriber[i].xCoordinate); //remove &
I am trying to do what's been done here Read co-ordinates from a txt files using C Program . The data that I am trying to input is in this format:
f 10 20 21
f 8 15 11
. . . .
f 11 12 25
The only difference in my point structure is that I have a an extra char to store the letter in the first column (which may or may not be the letter f). I guess im either declaring my char wrong, or I'm calling it in printf incorrectly. Either way, I only get the first line read and then my program terminates. Any ideas ?
Here is my MWE below
#define FILEPATHtri "/pathto/grid1DT.txt"
#define FILEPATHorg "/pathto/grid1.txt"
#define MAX 4000
#include <stdio.h>
#include <stdlib.h>
#include "math.h"
typedef struct
{
float x;
float y;
float z;
char t[1];
}Point;
int main(void) {
Point *points = malloc( MAX * sizeof (Point) ) ;
FILE *fp ;
fp = fopen( FILEPATHtri,"r");
int i = 0;
while(fscanf(fp, "%s %f %f %f ", points[i].t, &points[i].x, &points[i].y, &points[i].z ) == 4 )
{
i++;
}
fclose(fp);
int n;
for (n=0; n<=i; n++){
printf("%c %2.5f %2.5f %2.5f \n", points[i].t, points[n].x, points[n].y, points[n].z ); }
printf("There are i = %i points in the file \n And I have read n = %i points ",i,n);
return 0;
}
Since there's only 1 char in there, not a string just use a single char in your code:
char t;
}Point;
Then when you read it in:
while(fscanf(fp, "%c %f %f %f ", &points[i].t, &points[i].x, &points[i].y, &points[i].z ) == 4 )
{
I'll note that having an array of 1 char, at the end of a structure, sets you up for the struct hack which might not have been your intentions... A good reason to use just char t instead of char t[1]
Also this line:
for (n=0; n<=i; n++){
Should be
for (n=0; n<i; n++){
One last note... if you wanted to print the character out that you read in the prints at the bottom, you should be using n:
// note your previous code was points[i].t
printf("%c %f %f %f \n", points[n].t, points[n].x, points[n].y, points[n].z ); }
Check this
while(fscanf(fp, "%c %f %f %f ", points[i].t, &points[i].x, &points[i].y, &points[i].z ) == 4 )
{
i++;
}
fclose(fp);
int n;
for (n=0; n<i; n++){
printf("%c %2.5f %2.5f %2.5f \n", points[n].t, points[n].x, points[n].y, points[n].z ); }
printf("There are i = %i points in the file \n And I have read n = %i points ",i,n);
getch();
return 0;
}
modification are since only a single character is read %s modified to %c also in printf its not points[i].t its points[n].t . Also the limit checking in for loop is also corrected to n<i