|
CS256 - Principles of Structured Design
Fall 2021
|
Displaying ./code/cs256su21code/jul12/dogs.c
/*
defining a struct
*/
#include <stdio.h>
#include <string.h>
//defining a struct with an integer and two arrays
struct dogs {
int did;
char name[128];
char breed[128];
};
int main(){
//if we wanted to declare a variable using our struct, here's how we'd do it
struct dogs pet[128];//declaring a variable of type "struct dogs"
char buf[128];
int n, len;
//printf("how many entries? ");
//scanf("%d", &n);
*buf = '\0';
for(int i = 0; i < n; i++){
pet[i].did = i+1;
printf("\nDog #%d\n", i+1);
printf("What is the dogs name? ");
fgets(buf, 128, stdin);
len = strlen(buf) - 1;
buf[len] = '\0';
strcpy(pet[i].name, buf);
*buf = '\0';
printf("what is this dog's breed? ");
fgets(buf, 128, stdin);
len = strlen(buf) - 1;
buf[len] = '\0';
strcpy(pet[i].breed, buf);
*buf = '\0';
}
for(int i = 0; i < 3; i++){
printf("Id: %d, Name: %s, Breed:%s\n", pet[i].did, pet[i].name, pet[i].breed);
}
return 0;
}
|