実現したいこと
C言語により、給料をそれぞれの金種に分けるというコードを書きました。
そして、実行すると以下のようなSegmentation fault (core dumped)というエラーが起きてしまいます。
問題の部分はInput_data関数の部分だと予想しています。
発生している問題・エラーメッセージ
給料金種別計算機 入力する人数を指定してください(最大10人):1 1人目名前入力[半角100文字(全角50文字)以内、空白入力禁止]:山田 山田さんの給料入力[0以上100,000,000以下]:200000 Segmentation fault (core dumped)
該当のソースコード
C言語
1#include <stdio.h> 2 3int MONEY[] = { 4 10000, 5 5000, 6 1000, 7 500, 8 100, 9 50, 10 10, 11 5, 12 1 13}; 14 15static char *Money_Ord[] = { 16 "一万円", 17 "五千円", 18 "千円", 19 "五百円", 20 "百円", 21 "五十円", 22 "十円", 23 "五円", 24 "一円" 25}; 26 27typedef struct { 28 int A10000; 29 int B5000; 30 int C1000; 31 int D500; 32 int E100; 33 int F50; 34 int G10; 35 int H5; 36 int I1; 37}M_class; 38 39typedef struct { 40 char name[101]; 41 int salary_total; 42 M_class salary_class; 43}MEMBERS; 44 45void Input_data(MEMBERS *target , int *count){ 46 printf("\n入力する人数を指定してください(最大10人):"); 47 scanf("%d",count); 48 for(int i = 0 ; i < *count ; i++){ 49 printf("\n%d人目名前入力[半角100文字(全角50文字)以内、空白入力禁止]:",i+1); 50 scanf("%s",target[i].name); 51 printf("\n%sさんの給料入力[0以上100,000,000以下]:",target[i].name); 52 scanf("%d",target[i].salary_total); 53 } 54} 55 56void Calc_data(MEMBERS *target , int count){ 57 int salary,number,*denomiCount; 58 59 for(int i = 0 ; i < count ; i++){ 60 salary = target[i].salary_total; 61 denomiCount = &target[i].salary_class.A10000; 62 for(int j = 0 ; j < 8 ; j++){ 63 number = salary / MONEY[j]; 64 salary -= number * MONEY[j]; 65 *denomiCount = number; 66 denomiCount++; 67 } 68 *denomiCount = salary; 69 } 70} 71 72void Export_txtdata(MEMBERS *target , int count){ 73 74} 75 76void Export_dispdata(MEMBERS *target , int count){ 77 int SUM[9] = {0},*denomiCount; 78 printf("給料金種内訳明細書"); 79 printf("-----------------------\n"); 80 for(int i = 0 ; i < count ; i++ ){ 81 denomiCount = &target[i].salary_class.A10000; 82 printf("%d人目:%sさんの給料%d円の金種内訳\n" , i+1 , target[i].name , target[i].salary_total); 83 for(int j = 0 ; j < 9 ; j++){ 84 printf("%sは、 %d枚\n" , Money_Ord[j] , *denomiCount); 85 SUM[j] += *denomiCount; 86 denomiCount++; 87 } 88 } 89 printf("-----------------------\n"); 90 printf("-金種別累計枚数-\n"); 91 printf("-----------------------\n"); 92 for(int i = 0 ; i < 9 ; i++){ 93 printf("%sは、 %d枚\n" , Money_Ord[i] , SUM[i]); 94 } 95 printf("-----------------------\n"); 96} 97 98int main() 99{ 100 int decision = 1; //終了判定 101 int count; 102 MEMBERS target[10]; 103 printf("給料金種別計算機"); 104 do{ 105 Input_data(target , &count); 106 Calc_data(target , count); //データの計算 107 //Export_txtdata(target , count); //計算結果をテキストに出力 108 Export_dispdata(target , count); //計算結果を画面に出力 109 printf("別のデータを入力しますか?\n"); 110 printf("Yes(1),No(0):"); 111 scanf("%d",&decision); 112 }while(decision); 113}
試した事
mainの中のInput_data関数とCalc_data関数との間に文字を出力するコードを入れたのですが、出力されなかったので問題はInput_data関数の中だと思われます。
補足情報(FW/ツールのバージョンなど)
実行環境:「Online C Compiler」https://www.tutorialspoint.com/compile_c_online.php
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/07/21 07:47