誕生日管理プログラム(データ保存まで出来るように)+改

kerotan0820
記事: 91
登録日時: 14年前

誕生日管理プログラム(データ保存まで出来るように)+改

投稿記事 by kerotan0820 » 13年前

ありがたいことに、チャットにてアドバイスを頂き、頂いたヒントからあってるのかわかりませんが修正してみました。

CODE:

#include 
#include 
#include 
#include 

#define NAME_SIZE 30

    /*************************************************************** 
	          ▲ 名前、生年月日の構造体▲
	***************************************************************/
struct data1 {

	char  name[NAME_SIZE];  /*   名前   */
	int   year;      /*    年    */
	int   manth;     /*    月    */
	int   day;       /*    日    */

};


    /***************************************************************
	              ■新規データの入力、データ編集用関数■
    ***************************************************************/
	void inputdata( struct data1 birth[] ,int *quantity)
	{
		char name1[NAME_SIZE];

		printf("名前:"); scanf("%s",name1); //名前を入力
		strcpy(birth[ *(quantity) ].name , name1);
		
		//birth構造体の quantity(データ数)に応じた場所へ入力
		printf("西暦:"); scanf("%d",&birth[ *(quantity) ].year);
		printf(" 月:"); scanf("%d",&birth[ *(quantity) ].manth);
		printf(" 日:"); scanf("%d",&birth[ *(quantity) ].day);
		
		(*quantity)+=1;
	}

    /***************************************************************
	                ■保存データ 一覧表示関数■
    ***************************************************************/
	void indicate_data(struct data1 *birth,int *quantity)
	{
		int i;

		if(*(quantity) != 0 )
		{
			for(i=0; i ");
		scanf_s( "%d", &menu ); //
		printf("\n");
		
		switch (menu){

        case 1: // データの新規登録
			inputdata( birth , &quantity); //■構造体への名前,生年月日入力関数■
			write_data( birth , &quantity); //■テキストデータのデータ書き込み関数■
			printf("\n登録が完了しました\n");
		    while(kbhit()!=1);
			break;

		case 2: // データの編集
			break;

		case 3: // 登録データ一覧表示
			indicate_data( birth, &quantity); //■データ一覧表示関数■
		    while(kbhit()!=1);
	        break;

		case 4: // 終了
			exit(1);

		default: // 入力エラー
		    printf("1~3以外の値が入力されました\n");
			break;
			system("cls");
		} //switch

	} //while
	
	return 0;

}//main

どうでしょうか・・・。こういうことでしょうか。

アバター
softya(ソフト屋)
副管理人
記事: 11677
登録日時: 14年前

Re: 誕生日管理プログラム(データ保存まで出来るように)+改

投稿記事 by softya(ソフト屋) » 13年前

strcpyじゃなくてstrncpyなどでサイズオーバーをガードしたほうが良いと思います。その場合、30文字ぎりぎりの時の文字列終端子の処理も忘れないで下さいね。
あと入力された西暦、月、日の範囲外値のエラー処理もあると良くなると思います。

kerotan0820
記事: 91
登録日時: 14年前

Re: 誕生日管理プログラム(データ保存まで出来るように)+改

投稿記事 by kerotan0820 » 13年前

softyaさん>>
コメント有難うございます。
strncpy、是非使ってみます。
範囲外値のエラー処理、是非取り入れてみます^^
ありがとうございました。