0

I'm attempting to create a simple program that stores ten "pets" into an array. Each stuct contains data that must be accessed through functions. For some reason this doesn't seem to be working the way I would expect. Does anyone know why the program prompts for the name and then runs through the rest of the program without prompting the user again?

#include<stdlib.h>
#include<stdio.h>
#include <string.h>

struct Pet {
    char name[50];            //name
    char type[50];            //type
    char owner[50];           //owner
};

void setPetName(struct Pet *pet, char *name){
    memcpy(pet->name,name, 50);
}

void setPetType(struct Pet *pet, char *type){
    memcpy(pet->type,type, 50);
}

void setOwner(struct Pet *pet, char *owner){
   memcpy(pet->owner,owner, 50);
}

char* getName(struct Pet *pet){
    return pet->name;
}

char* getType(struct Pet *pet){
    return pet->type;
}

char* getOwner(struct Pet *pet){
    return pet->owner;
}

void printPetInfo(struct Pet *pet){
    printf("Pet's name is %s, Pet's type is %s, Pet's owner is %s", pet->name, pet->type, pet->owner);
}

int main(){

    struct Pet Pets[9];
    int index;

    char name[50], type[50], owner[50];
    for (index=0; index<9; index++){
        struct Pet pet;
        printf("Please enter pet's name ");
        scanf("%s\n", name);
        setPetName(&pet, name);
        printf("Please enter pet's type ");
        scanf("%s\n", type);
        setPetType(&pet, type);
        printf("Please enter pet's owner ");
        scanf("%s\n", owner);
        setOwner(&pet, owner);
        printPetInfo(&pet);
        Pets[index]=pet;
   }

    return 0;
}
user3451026
  • 61
  • 1
  • 7
  • You are not using your pets array for anything. – MTilsted Oct 27 '14 at 00:58
  • The scanf problem is one more you have. Answered here: http://stackoverflow.com/questions/9562218/c-multiple-scanfs-when-i-enter-in-a-value-for-one-scanf-it-skips-the-second-s – McKracken Oct 27 '14 at 01:14
  • All function take `struct Pet pet` and should take `struct Pet *pet`. Everything in c is passed by value so struct almost always has to be passed using a pointer. – Maciej Szpakowski Oct 27 '14 at 01:17
  • `scanf("%s\n",` --> `scanf("%s",` also _ten "pets"_ : `Pets[9];` --> `Pets[10];`.. `index<9;` --> `index<10;` – BLUEPIXY Oct 27 '14 at 01:58

1 Answers1

2

First you can't hold a string in a char:

char name, type, owner;

Instead you need an array of char (ie char name[50]; for example)

Then the format to scan a string is %s, not &s

scanf("&s\n", name);

And finally if you want to print a string, use format %s, not %c (%c is to print a single char).

kyflare
  • 864
  • 4
  • 9
  • I made those changes, however, this made other type issues present. Does the updated code reflect the changes you were referring too? – user3451026 Oct 27 '14 at 01:04
  • Yes, that's getting better. But since you changed the type of your fields, you need to change your accessors return type as well (ie getName, etc). Also you forgot to fix the line `char name, type, owner;` – kyflare Oct 27 '14 at 01:05
  • Okay, now the program compiles but it still hangs after prompting the user for the first pet's name – user3451026 Oct 27 '14 at 01:08
  • `char` is the type for a single character. `char *` and `char [50]` represent a string. You want your function getName() to return a string (`char *`). Just because it compiles doesn't mean it's right! – kyflare Oct 27 '14 at 01:10
  • I changed them all to char * but i still get a segmentation fault on the first set function call. – user3451026 Oct 27 '14 at 01:14
  • Oh, i hadn't looked at the set functions, they don't seem to be correct. Are you sure `name = name` is going to do anything? – kyflare Oct 27 '14 at 01:16
  • Finally, since you pass your pet struct as a value and not as a pointer to the set and get functions, your pet is copied into a temporary variable. So if you set the name of pet in setName, it's not going to have any effect outside of the function. – kyflare Oct 27 '14 at 01:19
  • Right, this has been updated but i still get a segmentation fault and im not sure why – user3451026 Oct 27 '14 at 01:26
  • You're almost done. Revert back the `char *` in the fields and in the local variables to `char [50]` so that they can hold memory (the input from the user). And change the get functions so that they return a `char *` instead of a `char`. Also `return pet->name;` – kyflare Oct 27 '14 at 01:31
  • And pet in the main loop needs to be a `Pet`, not a pointer. As it used to be. Then you can use `&pet` to get a pointer to the pet. – kyflare Oct 27 '14 at 01:34
  • Sorry about this. I think I made those changes. Still same error though. – user3451026 Oct 27 '14 at 01:39
  • As i told you, the input from the user has to be holded somewhere. `char *` is only a pointer and it can not hold the data. So `char *name, *type, *owner;` should be `char name[50]`, etc, and same thing for the struct fields. Oh and if you want to copy a string (char[]) to another one, you have to use the function `memcpy(output, input, size)` from `string.h`. You can't assign strings to other strings with `=` operator. – kyflare Oct 27 '14 at 01:42
  • Actually you should use `strcpy(output, input)` from string.h since it has been made exactly for copying strings. – kyflare Oct 27 '14 at 01:52
  • These changes have been made but instead of crashing it hanges in the same spot – user3451026 Oct 27 '14 at 01:52
  • `pet->name=name;` should be `strcpy(pet->name, name)` – kyflare Oct 27 '14 at 01:53
  • Didn't I change that? It seems to need two inputs before it will stop hanging – user3451026 Oct 27 '14 at 01:59
  • Your code works fine now. You can remove the `\n` in your scanf so that it doesn't require the user to press enter twice. Consider printing new lines adding `\n` at the end of your printf. – kyflare Oct 27 '14 at 02:04