Reputation: 141
I need help!
It doesn't matter what I input I get the same output, it's like the program does not read the input from scanf, any ideas?
I tried to get the input separately , to divide the scanf so I will get 8 scanf. But with no luck.
If you could help me out and point out on what I have been doing wrong i would appreciate that!
struct point{
double x;
double y;
}pt1,pt2,pt3,pt4;
struct rect{
struct point pt1;
struct point pt2;
struct point pt3;
struct point pt4;
};
int main()
{
struct rect new_screen={
scanf("%f,%f",&new_screen.pt1.x,&new_screen.pt1.y),
scanf("%f,%f",&new_screen.pt2.x,&new_screen.pt2.y),
scanf("%f,%f",&new_screen.pt3.x,&new_screen.pt3.y),
scanf("%f,%f",&new_screen.pt4.x,&new_screen.pt4.y),
};
printf("the middle between (%.2f,%.2f) and (%.2f,%.2f) is ",new_screen.pt1.x,new_screen.pt1.y,new_screen.pt3.x,new_screen.pt3.y);
print_point(middle(new_screen.pt1,new_screen.pt3));
return 0;
}
struct point middle(struct point pt1, struct point pt2)
{
struct point tmp = {((pt1.x+pt2.x)/2),((pt1.y+pt2.y)/2)};
return tmp;
}
void print_point(struct point pt)
{
printf("(%.2f,%.2f)",pt.x,pt.y);
}
output
the middle between (2.00,2.00) and (0.00,0.00) is (1.00,1.00)
Upvotes: 2
Views: 78
Reputation: 170299
This here initialization...
struct rect new_screen = {
scanf("%f,%f",&new_screen.pt1.x,&new_screen.pt1.y),
scanf("%f,%f",&new_screen.pt2.x,&new_screen.pt2.y),
scanf("%f,%f",&new_screen.pt3.x,&new_screen.pt3.y),
scanf("%f,%f",&new_screen.pt4.x,&new_screen.pt4.y),
};
... doesn't do what you think it does. scanf
returns a single integer (consult its manual page for the meaning of the returned value), but your structure holds struct point
fields. Which sums up to a total of 8 doubles, and 4 calls to scanf
. Due to how initialization works in C, you can omit braces and initializers for some fields. So this is sadly valid code. But it initializes new_screen.pt1
and new_screen.pt2
only (four calls to scanf
initialize the four doubles). The thing you scanned into them is immediately overwritten. The last two points aren't initialized by the braces, so they should be initizlied with zeros, again overwriting anything you may have scanned into them.
The long and short of it, is that assuming all scanf
invocations don't fail (and we ignore the problem with the format specifiers), you essentially wrote this:
struct rect new_screen = {
{ 2, 2 },
{ 2, 2 },
{ 0, 0 },
{ 0, 0 }
};
The less wrong initialization would be:
struct rect new_screen;
scanf("%lf,%lf",&new_screen.pt1.x,&new_screen.pt1.y);
scanf("%lf,%lf",&new_screen.pt2.x,&new_screen.pt2.y);
scanf("%lf,%lf",&new_screen.pt3.x,&new_screen.pt3.y);
scanf("%lf,%lf",&new_screen.pt4.x,&new_screen.pt4.y);
It doesn't check that scanf
succeeded (which is why it's only less wrong) but it will not trample the content of new_screen
with the return value of scanf
. I also fixed the format specifier (you used %f
, but double
variables require %lf
).
The above mistakes are also a testament to how important it is to compile with warnings enabled, and to address them. For instance, when I compiled your code with gcc -pedantic-errors -Wall -Wextra
I got these warnings:
warning: format '%f' expects argument of type 'float *', but argument 2 has type 'double *' [-Wformat=]
scanf("%f,%f",&new_screen.pt1.x,&new_screen.pt1.y),
warning: missing braces around initializer [-Wmissing-braces]
struct rect new_screen={
warning: missing initializer for field 'pt3' of 'struct rect' [-Wmissing-field-initializers]
};
Make a habit of compiling with warnings enabled, and address them until the build is free of compiler diagnostics.
Upvotes: 4