C scanf int into struct

45,802

You read into b.y twice:

scanf("%d%d%d", &(b.x), &(b.y), &(b.y));

The last one is supposed to be b.z, otherwise b.y is set to 6, then gets overwritten to 2, while b.z is never set (and happens to be 0).

Share:
45,802
Timothy Williams
Author by

Timothy Williams

Updated on July 09, 2022

Comments

  • Timothy Williams
    Timothy Williams almost 2 years

    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.

    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

    The two vectors used are: [3 2 1] and [5 2 0]

    I have tried spaces around the %d in scanf, and no parentheses around &a.x etc.

    Thanks for looking and any help is appreciated. Just for full disclosure, this is for a C programming class I am attending.

  • Timothy Williams
    Timothy Williams almost 10 years
    Do you have any idea how many times I read through those 4 lines of code trying to figure out what was going on? DOH!!!! Well, hopefully that is the extent of my Homer Simpson moves for the week. HAPPY MONDAY EVERYBODY!!!!
  • Spikatrix
    Spikatrix almost 10 years
    Everyone makes mistakes you know...One tiny mistake is enough to cause a painful headache!! :)
  • Zoey Hewll
    Zoey Hewll almost 5 years
    A good idea to debug in this situation is to abstract out common parts, eg make a function that reads 3 ints and returns a struct, and test that part in isolation as well as testing the whole