C编程标识符值错误
好的,我正在做一个相当基本的程序,从account.txt读取。除了金额和余额的值显示为0.00外,输出结果与我期望的结果类似 下面是account.txt文件:C编程标识符值错误,c,C,好的,我正在做一个相当基本的程序,从account.txt读取。除了金额和余额的值显示为0.00外,输出结果与我期望的结果类似 下面是account.txt文件: I 478.83 D 127.45 D 619.84 C 945.12 C 4.76 D 32.81 C 1.06 D 184.32 C 495.18 C 141.81 C 255.10 D 250.00 D 123.88 D 245.05 D 873.25 C 981.12 D 317.84 C 812.73 D 606.
I 478.83
D 127.45
D 619.84
C 945.12
C 4.76
D 32.81
C 1.06
D 184.32
C 495.18
C 141.81
C 255.10
D 250.00
D 123.88
D 245.05
D 873.25
C 981.12
D 317.84
C 812.73
D 606.12
这是我节目的相关部分
int process_info(void){
char code;
double amount, service, balance;
double amtCheck, amtDeposit, openBalance, closeBalance;
int numCheck, numDeposit, numOverdraft;
amount = 0.0;
service = 0.0;
balance = 0.0;
amtCheck = 0.0;
amtDeposit = 0.0;
openBalance = 0.0;
closeBalance = 0.0;
numCheck = 0;
numDeposit = 0;
fp = fopen("account.txt", "r");
printf("Transaction\tDeposit\tCheck\tBalance\n");
printf("-----------\t-------\t-----\t-------\n");
while ((fscanf(fp, "%c %f", &code, &amount)) != EOF){
if (code == 'I'){
openBalance = amount;
balance += amount;
printf("Initial Balance\t\t\t%.2f\n", amount);
}
else if (code == 'D'){
numDeposit++;
balance += amount;
printf("Deposit\t\t%.2f\t\t%.2f\n", amount, balance);
}
else if (code == 'C'){
numCheck++;
balance -= amount;
printf("Check\t\t\t%.2f\t%.2f\n", amount, balance);
}
}
getch();
return 0;
}
fscanf
返回成功转换的次数-检查并查看调用是否按预期方式工作(提示:不是这样)
打开一些警告将是良好的第一步。在fscanf()
调用中使用的%f
格式与正在分配的double*
参数不匹配。使用%lf
。发件人:
我不确定你在第一段中的确切意思,程序就停在account.txt的末尾。我将%f更改为%lf,现在所有值都被正确输出。非常感谢。我可能还有一些错误需要处理;)。
example.c:29:36: warning: format specifies type 'float *' but the argument has
type 'double *' [-Wformat]
while ((fscanf(fp, "%c %f", &code, &amount)) != EOF){
~~ ^~~~~~~
%lf