C语言 C scanf int 到结构体
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24868225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C scanf int into struct
提问by Timothy Williams
I am trying to read some integers into a struct. I am having the user enter two 3-dimensional vectors and returning two cross products and the dot product.
我正在尝试将一些整数读入一个结构中。我让用户输入两个 3 维向量并返回两个叉积和点积。
It appears to be skipping the second value of the second vector. Here's my code so far:
它似乎正在跳过第二个向量的第二个值。到目前为止,这是我的代码:
/** Write a program to calculate cross-products and dot products of
** a 3-dimensional vector:
**
** 1. Uses a type definition
** 2. Accepts User input of qty(2) 3-dimensional vectors
** 3. Calculate the cross-product of A x B and B x A
** 4. Calculate the dot product A * B
**
******************************************************************/
/************* Preprocessor Functions **********************/
#include <stdio.h>
#include <stdlib.h>
/************ Structured Data Types ***************************/
typedef struct vector
{
int x;
int y;
int z;
} Vector;
/************* Declare User Functions **********************/
int dot_product(Vector a, Vector b);
Vector cross_product(Vector a, Vector b);
/************ Begin MAIN LOOP *************************/
int main(void)
{
/** Declare variables **/
Vector a, b, c;
printf("Enter the 3 integer components of the first vector: ");
scanf("%d%d%d", &(a.x), &(a.y), &(a.z));
printf("Enter the 3 integer components of the second vector: ");
scanf("%d%d%d", &(b.x), &(b.y), &(b.y));
c = cross_product(a, b);
printf("\n\t(%d %d %d) x (%d %d %d) = (%d %d %d)", a.x,a.y,a.z,b.x,b.y,b.z,c.x,c.y,c.z);
c = cross_product(b, a);
printf("\n\t(%d %d %d) x (%d %d %d) = (%d %d %d)", b.x,b.y,b.z,a.x,a.y,a.z,c.x,c.y,c.z);
printf("\n\t(%d %d %d) * (%d %d %d) = %d\n", a.x,a.y,a.z,b.x, b.y,b.z,dot_product(a, b));
/*********** AND CUT! It's a wrap folks! Take 5! ***********/
return 0;
}
/********** User Functions to perform the calculations ****/
int dot_product(Vector a, Vector b)
{
return((a.x*b.x)+(a.y*b.y)+(a.z*b.z));
}
Vector cross_product(Vector a, Vector b)
{
Vector c;
c.x = (a.y*b.z)-(a.z*b.y);
c.y = (a.z*b.x)-(a.x*b.z);
c.z = (a.x*b.y)-(a.y*b.x);
return(c);
}
If the user enters: 3 2 1 And then enters: 5 6 2
如果用户输入:3 2 1 然后输入:5 6 2
The two vectors used are: [3 2 1] and [5 2 0]
使用的两个向量是:[3 2 1] 和 [5 2 0]
I have tried spaces around the %d in scanf, and no parentheses around &a.x etc.
我在 scanf 中尝试过 %d 周围的空格,而 &a.x 等周围没有括号。
Thanks for looking and any help is appreciated. Just for full disclosure, this is for a C programming class I am attending.
感谢您的关注,任何帮助表示赞赏。只是为了充分披露,这是我正在参加的 C 编程课程。
回答by Mr. Llama
You read into b.ytwice:
你读了b.y两遍:
scanf("%d%d%d", &(b.x), &(b.y), &(b.y));
scanf("%d%d%d", &(b.x), &(b.y), &(b.y));
The last one is supposed to be b.z, otherwise b.yis set to 6, then gets overwritten to 2, while b.zis never set (and happens to be 0).
最后一个应该是b.z,否则b.y设置为6,然后被覆盖为2,而b.z永远不会设置(并且恰好是0)。

