0
我在這個程序中遇到了一些嚴重的麻煩。 我需要從一個文件中讀取數據,這個文件有一組名和姓,一個cookie類型,購買金額,成本以及是否支付。從文件讀取變量並將它們存儲在結構中 - C
我相當有信心,我的功能是正確的。
當我運行該程序時,它運行正常,但在某些數字之間看起來會有額外的數字。我的意思是,如果輸入的文件是: 約翰綠薄荷1 1.00是 簡藍籌2 2.00ñ 輸出文件將是這樣的: 約翰·格林12 20000薄荷455 1等等
有任何想法嗎?我猜我讀了文件/將它們存儲到結構中。
#include <stdio.h>
#include <ctype.h>
/*structure definition*/
struct customer{
char first[12], last[12], type[10], paid;
int boxes;
float cost;
};
/*prototypes*/
void readCustomerData(FILE*, struct customer[]);
void printCustomerData(FILE*, int, struct customer[]);
float moneyReceived(int, struct customer[]);
int custWhoOwe(int, struct customer[]);
float moneyOutstanding(int, struct customer[]);
int moneyReceivedPercent(int, struct customer[]);
int main(){
int numCustomers;
float money, money2;
int cust, perc;
FILE*input;
FILE*output;
input = fopen("custInfo.txt", "r");
output = fopen("output.txt", "w");
fscanf(input, "%d", &numCustomers);
struct customer customerData[numCustomers];
readCustomerData(input, customerData);
printCustomerData(output, numCustomers, customerData);
fprintf(output, "Summary:\n\n");
money = moneyReceived(numCustomers, customerData);
fprintf(output, "Total Cash Received: $%.2f\n\n", money);
cust = custWhoOwe(numCustomers, customerData);
fprintf(output, "Num customers who have not paid: %d\n\n", cust);
money2 = moneyOutstanding(numCustomers, customerData);
fprintf(output, "Total Amount Outstanding: $%.2f\n\n", money2);
perc = moneyReceivedPercent(numCustomers, customerData);
fprintf(output, "Percentage of Sales outstanding: %d%%\n\n", perc);
fclose(input);
fclose(output);
return 0;
}
void readCustomerData(FILE*file, struct customer cdata[]){
int numCustomers, i;
file = fopen("custInfo.txt", "r");
fscanf(file, "%d", &numCustomers);
for (i=0; i<numCustomers; i++){
fscanf(file, "%s%s%s%d%f%c", &cdata[i].first, &cdata[i].last, &cdata[i].type, &cdata[i].boxes, &cdata[i].cost, &cdata[i].paid);
}
fclose(file);
}
void printCustomerData(FILE*file, int num, struct customer cdata[]){
int i;
fprintf(file, "Customers:\n\n");
fprintf(file, "Name\t\tType of Cookie\t\tNum of Boxes\tCost of Box\tPaid\n");
fprintf(file, "--------------------------------------------------------------------------------\n\n");
for (i=0; i<num; i++){
fprintf(file, "%s %s\t", cdata[i].first, cdata[i].last);
fprintf(file, "%s\t\t", cdata[i].type);
fprintf(file, "%d\t\t", cdata[i].boxes);
fprintf(file, "$%.2f\t\t", cdata[i].cost);
fprintf(file, "%c", cdata[i].paid);
fprintf(file, "\n\n");
}
fprintf(file, "\n\n\n\n\n");
}
float moneyReceived(int num, struct customer cdata[]){
int i, sum=0;
for (i=0; i<num; i++){
if(cdata[i].paid=='Y'){
sum= sum+(cdata[i].cost*cdata[i].boxes);}
}
return sum;
}
int custWhoOwe(int num, struct customer cdata[]){
int count=0, i;
for (i=0; i<num; i++){
if (cdata[i].paid=='N'){
count++;}
}
return count;
}
float moneyOutstanding(int num, struct customer cdata[]){
int i, sum=0;
for (i=0; i<num; i++){
if (cdata[i].paid=='N'){
sum= sum+(cdata[i].cost*cdata[i].boxes);}
}
return sum;
}
int moneyReceivedPercent(int num, struct customer cdata[]){
int answ;
float money, moneyOut;
money = moneyReceived(num, cdata);
moneyOut = moneyOutstanding(num, cdata);
answ=(moneyOut/money)*100;
return answ;
}
'fscanf(file,「%s%s%s%d%f%c」,' - >'fscanf(file,「%s%s%s%d%f%c」, '在%c之前添加空格 – chux
上帝的甜媽你真是太好了謝謝你!! – AwesomeSauce
這是爲什麼呢?btw – AwesomeSauce