Home > Back-end >  Why is my code funtion not working as i want it to?
Why is my code funtion not working as i want it to?

Time:10-17

Here the printAdd function is not working and I am not sure why. There is no error shown in the terminal it just doesn't print the info.

#include<stdio.h>

struct address {
    int houseNo;
    int block;
    char city[100];
    char state;
};

void printAdd(struct address add);

int main(){
    struct address add[5];
    printf("enter info for person 1 : \n");
    scanf("%d", &add[0].houseNo);
    scanf("%d", &add[0].block);
    scanf("%s", &add[0].city);
    scanf("%s", &add[0].state);


    printAdd(add[0]);

    
    return 0;
}

void printAdd(struct address add){
    printf("address is : %d, %d, %s, %s\n", add.houseNo, add.block, add.city, add.state);
}

CodePudding user response:

Very likely this is the problem:

scanf("%s", &add[0].state);

Here you ask scanf to read a string into the single-character state member. Either read a character with %c or make state an array that can hold at least two characters (for a single-character "state" designation).

CodePudding user response:

These calls of scanf

scanf("%s", &add[0].city);
scanf("%s", &add[0].state);

are incorrect.

You should write

scanf("           
  • Related