Nisarg
Nisarg

Reputation: 407

displaying numbers and strings from a structure in c

#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 to Telephone Directory.\n\nPlease enter the name of the contact.\n");
    scanf("%s", &contact.Name);

    printf("Please enter the number of the contact.\n");
    scanf("%ld", &contact.Number);

    printf("Please enter the address of the contact.\n");
    scanf("%d %s", &contact.HouseNumber, &contact.street);

    answer1 = &contact;
    answer2 = &contact;
    answer3 = &contact;
    answer4 = &contact;

    printf("Would you like to obtain information on your 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 you for using Telephone Directory.\n");
    }

}

I'm trying to make a contacts program in C. I want the program to print the user's house address. I have the "HouseNumber" variable in the structure and the "street" variable in the structure, setting "answer3" as an int variable to display the "HouseNumber" and "answer4" as a char variable to display "street". Together, I was hoping they will print the user's address in a single string, but when I run the program and enter "yes" to display the contact information after entering the house number and street, the program crashes, and displays lldb with a bad thread error. It seems like everything is right, because my compiler says that there are no issues with the code, but it crashes. Can somebody please help me with this?

Upvotes: 1

Views: 3774

Answers (3)

Raju
Raju

Reputation: 1169

The mistake i have observed in your code is, you have created name,street and answer variables as char but tried to store strings in them by using %s resulting into crashing. In those places either you have to use char * or character array.

    char *name //(or) char name[15]

The modified code looks like 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 to Telephone Directory.\n\nPlease enter the name of the                                          contact.\n"); 

        scanf("%s", contact.Name); 

        printf("Please enter the number of the contact.\n"); 

        scanf("%ld", &contact.Number); 

        printf("Please enter the address and street of the 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 you for using Telephone Directory.\n"); 
    } 

   return 0; 
    }

Upvotes: 1

Paul Rubel
Paul Rubel

Reputation: 27232

When you do scanf("%s", &answer);

You're writing the user's input into a char answer; The argument to scanf needs to have enough space to hold the entire string plus one null byte. Try something like:

char answer[256];

This assumes that input will never be more than 256 chars, but seems reasonable in a simple program like this. Note that gcc supports non-standard %a that will allocate a string for you if you pass in a char*.

When you try to store strings you need to ensure there's enough space for the data you're putting there, like in Dictionary only holding a char, or else you're going to overflow and stomp on some other memory you may need later.

Upvotes: 2

KrisSodroski
KrisSodroski

Reputation: 2852

struct Directory {
    char * Name;
    long int Number;
    int HouseNumber;
    char * street;
};

inside main:

struct Directory Contact;
Contact.Name = malloc(sizeof(char * sizeOfName));

 scanf("%s", contact.Name);

Then you can copy in your Name/street etc.
Since Name is a char*, you won't use the address, but you'll just pass the pointer to scanf. This will fill that malloc'd memory with the string the user enters.

Upvotes: 0

Related Questions