#include <stdio.h> #include <string.h> struct directory { char name; long int number; int housenumber; char street; }; int main(void) { struct directory contact; struct directory *answer1, *answer2, *answer3, *answer4; char answer; printf("welcome telephone directory.\n\nplease enter name of contact.\n"); scanf("%s", &contact.name); printf("please enter number of contact.\n"); scanf("%ld", &contact.number); printf("please enter address of contact.\n"); scanf("%d %s", &contact.housenumber, &contact.street); answer1 = &contact; answer2 = &contact; answer3 = &contact; answer4 = &contact; printf("would obtain information on contact? enter 'yes' or 'no'.\n"); scanf("%s", &answer); if (strcmp(&answer, "yes")==0) { printf("%s\n", &answer1->name); printf("%ld\n", answer2->number); printf("%d", answer3->housenumber); printf("%s", &answer4->street); } if (strcmp(&answer, "no")==0) { printf("thank using telephone directory.\n"); } }
i'm trying make contacts program in c. want program print user's house address. have "housenumber" variable in structure , "street" variable in structure, setting "answer3" int variable display "housenumber" , "answer4" char variable display "street". together, hoping print user's address in single string, when run program , enter "yes" display contact information after entering house number , street, program crashes, , displays lldb bad thread error. seems right, because compiler says there no issues code, crashes. can please me this?
the mistake have observed in code is, have created name,street , answer variables char tried store strings in them using %s resulting crashing. in places either have use char * or character array.
char *name //(or) char name[15]
the modified code looks this
#include <stdio.h> struct directory { char name[20]; long int number; int housenumber; char street[20]; }; int main(int argc, char *argv[]) { struct directory contact; struct directory *answer1, *answer2, *answer3, *answer4; char answer[4]; printf("welcome telephone directory.\n\nplease enter name of contact.\n"); scanf("%s", contact.name); printf("please enter number of contact.\n"); scanf("%ld", &contact.number); printf("please enter address , street of contact.\n"); scanf("%d %s", &contact.housenumber, contact.street); answer1 = &contact; answer2 = &contact; answer3 = &contact; answer4 = &contact; printf("enter yes r no"); scanf("%s",answer); if (strcmp(answer,"yes")==0) { printf("%s\n", answer1->name); printf("%ld\n", answer2->number); printf("%d\n", answer3->housenumber); printf("%s", answer4->street); } else { printf("thank using telephone directory.\n"); } return 0; }
Comments
Post a Comment