構造体

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら

構造体

#1

投稿記事 by » 17年前

gstudent構造体を定義し,ユーザからの入力を3人分(fusayoshi,taro, hanako)受付けて,入力後に全員のデータを表示するプログラムを作成せよ.また構造体への値の代入は,struct gstudent set_gstudent();という関数の内部で行い,返り値として値の入った構造体を受け取るようにすること.



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

struct gstudent{
char name[20];
int height;
float weight;
int math;
long schols;
};

void show_profile(struct gstudent user_prof) {

printf("名前:%s, 身長:%d, 体重:%f, 数学の成績:%d,奨学金:%ld\n", user_prof.name, user_prof.height, user_prof.weight, user_prof.math,user_prof.schols);
}

struct gstudent set_gstudent(char username[/url],int userheight,float userweight,int usermath,long userschols){

struct gstudent temp;
strcpy(temp.name,username);
temp.height=userheight;
temp.weight=userweight;
temp.math=usermath;
temp.schols=userschols;

return temp;
}


int main(void) {

struct gstudent fusayoshi,taro,hanako,user_prof;


printf("fusayoshiのデータを入力してください\n");
printf("Fusayoshi\n");
scanf("%s,%d,%f,%d,%ld",user_prof.name,user_prof.height,user_prof.weight,user_prof.math,user_prof.schols);
printf("taroのデータを入力してください\n");
printf("Taro\n");
scanf("%s,%d,%f,%d,%ld",user_prof.name,user_prof.height,user_prof.weight,user_prof.math,user_prof.schols);
printf("hanakoのデータを入力してください\n");
printf("Hanako\n");
scanf("%s,%d,%f,%d,%ld",user_prof.name,user_prof.height,user_prof.weight,user_prof.math,user_prof.schols);
show_profile(fusayoshi);
show_profile(taro);
show_profile(hanako);
return 0;
}

こんなプログラムを作ったのですが、
実行結果が、
fusayoshiのデータを入力してください
Fusayoshi
172,47.2,56,10000
taroのデータを入力してください
Taro
176,60.0,81,50000
hanakoのデータを入力してください
Hanako
165,48.1,75,30000
名前:n@h 奎須 hi@, 身長:-1073743908, 体重:-1.999742, 数学の成績:134514126,奨学金:1075145064
名前:, 身長:0, 体重:2.139266, 数学の成績:1073826116,奨学金:1073826712
名前:wK@, 身長:1073918700, 体重:2.020582, 数学の成績:0,奨学金:0

こんな事になってしまうんですが、どこが可笑しいか教えてもらえませんでしょうか?お願いします。

通りすがり

Re:構造体

#2

投稿記事 by 通りすがり » 17年前

 
#include <stdio.h>

struct gstudent{
	char name[20];
	int height;
	float weight;
	int math;
	int schols;
};

struct gstudent set_gstudent(char *name)
{
	struct gstudent student;
	char buff[64];
	
	printf("%s のデータを入力してください。\n%s\n", name, name);
	sscanf(name, "%s", student.name);
	fgets(buff, 64, stdin);
	sscanf(buff, "%d,%f,%d,%d", &student.height,&student.weight,
			&student.math, &student.schols);
	return student;
}
 
 void print_gstudent(struct gstudent student)
 {
 	printf("名前:%s, ", student.name);
 	printf("身長:%d, ", student.height);
 	printf("体重:%.2f, ", student.weight);
 	printf("数学の成績:%d, ", student.math);
 	printf("奨学金:%d\n", student.schols);
 	return;
 }
 
int main(void)
{
	char *name[/url] = {"Fusayoshi", "Taro", "Hanako"};
	struct gstudent student[3];
	int i;
	
	for(i = 0; i < 3; i ++) student = set_gstudent(name);
	for(i = 0; i < 3; i ++) print_gstudent(student);
	return 0;
}

 

管理人

Re:構造体

#3

投稿記事 by 管理人 » 17年前

う、すみません、忙しくてお返事遅れました。
解決されました?

Re:構造体

#4

投稿記事 by » 17年前

あっ!!!できました★
でも書いてあるのと私が作ったのは全然違ったです。
新しい勉強ができました。
ありがとうございます。

管理人

Re:構造体

#5

投稿記事 by 管理人 » 17年前

解決なさったようで、よかったです。
がんばってくださいね☆

閉鎖

“C言語何でも質問掲示板” へ戻る