アプリ版:「スタンプのみでお礼する」機能のリリースについて

C言語で以下のようなプログラムを作りました。
「main関数内で下記のデ-タを構造体に格納し、キーボードから入力された名前と該当する学生の身長と年齢を画面に表示するプログラムを作成せよ。」というものです。
このプログラムはコンパイルは通るのですが、2人目以降のデータを表示させようとしても表示してくれません。。。どうもリスト構造のfor文がうまくループしていないみたいなんですが原因が分かりません。どなたか原因の分かる方アドバイスをお願いしますm(_ _)m

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

typedef struct data{
char name[20];
int height;
int age;
struct data *next;
}person;

person *newperson(void);

int main(void){

char namae[20],s[20];
int toshi,shinchou,i;

person *head;
person *list;
person *nlist;
person *LIST;

head = newperson();
nlist = head;

printf("データを入れてください。\n");

for(i=0;i<=4;i++){

scanf("%s",namae);
scanf("%d",&shinchou);
scanf("%d",&toshi);
list = newperson();
strcpy(list ->name,namae);
list -> height = shinchou;
list -> age = toshi;

nlist -> next = list;
nlist = list;

}

printf("知りたい人の名前は?\n");
scanf("%s",s);

for(LIST = head->next;LIST ->next != NULL;LIST = LIST->next){

if(strcoll(s,LIST ->name)==0){
printf("%s\t%d\t%d\n",LIST->name,LIST->height,LIST->age);
break;
}
printf("%s\n",LIST->name);
printf("%s\n",LIST->next->name);

}

return(0);

}

person *newperson(){

person *dummy;

dummy = (person*)malloc(sizeof(person));
dummy -> next = NULL;

return(dummy);

}

A 回答 (3件)

>2人目以降のデータを表示させようとしても表示してくれません


ということですが、とりあえずMacOS X + GCCで動かしたところ、二人目以降も表示してますね。

ただ、最後の人は絶対にヒットしないので修正する必要があります。ヒントは検索ループの継続条件。

あと、問題がもう一つ。
newperson()でmallocしているけど、プログラム中のどこにもfreeする箇所がない。
とりあえずはそれでも動作しますが、確保したメモリーはきちんと解放するようにしましょうね。
    • good
    • 0
この回答へのお礼

ヒントありがとうございます!!
おかげで「LIST != NULL;」ってことに気づけました。あとfreeの注意にも感謝します!!

お礼日時:2005/12/19 21:31

バグがありました。


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

typedef struct _person
{
char *name;
int height;
int age;
struct _person *next;
} person;

char *
getline(void)
{
static char buf[BUFSIZ];
if (fgets(buf, BUFSIZ, stdin) == NULL) {
exit(0);
}
buf[strlen(buf) - 1] = 0;
return (buf);
}

int
main(void)
{

int find;
char *s, *name;
person *head = NULL;
person *list;
person **nlist;

nlist = &head;

while (1) {
printf("知りたい人の名前は?\n");
name = getline();
find = 0;
for (list = head; list != NULL; list = list->next) {
if (strcoll(name, list->name) == 0) {
printf("%s\t%d\t%d\n", list->name,
list->height, list->age);
find++;
break;
}
}
if (find == 0) {
name = strdup(name);
printf("見つかりません\n");
printf("データを入れますか? (y/n) ");
s = getline();
if (s[0] != 'n') {
list = (person *) malloc(sizeof(person));
list->next = NULL;
list->name = name;
printf("身長:");
list->height = atoi(getline());
printf("年齢:");
list->age = atoi(getline());
*nlist = list;
nlist = &list->next;
}
else {
free(name);
}
}
}
return (0);
}
    • good
    • 0

#include<stdio.h>


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

typedef struct data
{
char *name;
int height;
int age;
struct data *next;
} person;

person *
newperson()
{
person *dummy;

dummy = (person *) malloc(sizeof(person));
dummy->next = NULL;

return (dummy);
}

char *
getline(void)
{
static char buf[BUFSIZ];
if (fgets(buf, BUFSIZ, stdin) == NULL) {
exit(0);
}
buf[strlen(buf) - 1] = 0;
return (buf);
}

int
main(void)
{

int find;
char *s, *name;
person *head = NULL;
person *list;
person **nlist;
person *LIST;

nlist = &head;

while (1) {
printf("知りたい人の名前は?\n");
name = getline();
find = 0;
for (LIST = head; head != NULL && LIST != NULL;
LIST = LIST->next) {
if (strcoll(name, LIST->name) == 0) {
printf("%s\t%d\t%d\n", LIST->name,
LIST->height, LIST->age);
find++;
break;
}
}
if (find == 0) {
name = strdup(name);
printf("見つかりません\n");
printf("データを入れますか? (y/n) ");
s = getline();
if (s[0] != 'n') {
list = newperson();
list->name = name;
printf("身長:");
list->height = atoi(getline());
printf("年齢:");
list->age = atoi(getline());
*nlist = list;
nlist = &list->next;
}
else {
free(name);
}
}
}
return (0);
}
    • good
    • 0
この回答へのお礼

ありがとうございます!!
該当者を検索するプログラムの訂正だけじゃなく、データの追加まで組み込んでもらって助かりました。

お礼日時:2005/12/19 21:28

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!