0
私はまだCを新しくしています。コンボとアラートのためのトータルトランザクションを見つける必要があります。したがって、私は各行を読み込み、そしてwhileループの中で合計コンボミールと合計アラートカートゥーンを得るための関数を作成しました。しかし、私はすべての値をプリントアウトしていません。 daily_transactions機能からテキストファイルから読み取った値を加算する - Cプログラミング
出力:
0:1:7.98
1:1:20.97
2:1:35.96
2:2:44.95
2:2:44.95
3:2:55.94
編集:
Total combo meal transaction : 0
Total ala-carte transaction : 0
Total sales : RM 0.00
私は
tfptr = fopen("trans.txt", "r");
//loops until end of file to read last line
while(fgets(str, sizeof(str), tfptr)!=NULL){ //fgets() will return NULL when the file is over
fscanf(tfptr, "%u:%u:%f\n", &combo_trans, &ala_trans, &total);
c_trans += combo_trans;
a_trans += ala_trans;
grand_total += total;
}
daily_transactions(c_trans, a_trans, grand_total);
fclose(tfptr);
テキストファイル)(関数fscanfを使用してコード化された取引をプリントアウトする機能を
void daily_transactions(int combo_trans, int ala_trans, float grand_total){
printf("--------------------------------\n");
printf(" Daily Transactions\n");
printf("--------------------------------\n");
printf("Total combo meal transaction : %d\n", combo_trans);
printf("Total ala-carte transaction : %d\n", ala_trans);
printf("Total sales : RM %.2f\n", grand_total);
puts("------------------------------------");
}
どこから印刷していますか?上記のコードにはありません。 –
'fscanf'の代わりに' sscanf'を使用しますか? – BLUEPIXY
私は 'sscanf'を使いましたが、最後の行の値だけを読み込みます。これは私が望むものではありません。私はすべてを追加したい。 –