我正确使用fscanf吗?和switch语句?

时间:2016-10-17 20:46:09

标签: c switch-statement double scanf

我被分配了一个银行账户分配,在那里我得到用户输入他们是想要存款(D)还是退出(C)。在他们选择之后,假设显示他们的银行账户余额。

我遇到的麻烦是我一直在处理我的fopen和fscanf的构建错误。我的代码有什么问题?

严重级代码说明项目文件行 错误C4996' fscanf':此函数或变量可能不安全。请考虑使用fscanf_s。要禁用弃用,请使用_CRT_SECURE_NO_WARNINGS。详细信息请参见在线帮助。 checkingAccount c:\ users \ csis \ documents \ visual studio 2015 \ projects \ checkingaccount \ checkingaccount \ checkingaccount.c 21

    #include <stdio.h>

FILE *fpin;
FILE *fpout;

double deposit;
double withdraw;
double balance;


//Function that asks the user for deposit information then prints balance + deposit.
double D(double balance, double deposit) {
printf("and the amount is:");
fscanf(fpout, "%lf", &deposit);
printf("The balance is: %lf", balance);
return 0;
}

//Function that asks user for the amount they want withdrawn, then prints balance - withdrawal amount.
double C(double balance, double withdraw) {
    printf("and the amount is:");
    fscanf(fpout, "%lf", &withdraw);
    printf("The balance is %lf", balance);
    return 0;
}



int main(void) {

    char code;

    deposit = 0;
    withdraw = 0;
    balance = 0;

    //formula for balance.
    balance = balance + deposit;
    balance = balance - withdraw;

//opens documents for inputs.
fpin = fopen("transactions.txt", "r");
fpout = fopen("processed.txt", "w");


printf("Welcome to Chris's Checking Account Tracer Program");
printf("\n------------------------------------------------------\n");

//runs infinite loop.
for (;;) {
    printf("The transaction is a: ");
    //Sends user input of code to transcations text document.
    fscanf(fpin, '%c', &code);


    // If user enters D runs 'D' Function.
    switch (code) {
    case 'D':
        D(balance, deposit);
        break;

        // If user enters C runs 'C' Function.
    case 'C':
        C(balance, withdraw);
        break;
        // If user enters anything else other then D or C prints "Not responding correctly".
    default:
        printf("Not responding Correctly.");
        break;
    }
}
//closes the text documents.
fclose(fpin);
fclose(fpout);
getchar();
return 0;
}

1 个答案:

答案 0 :(得分:1)

这是你的问题。在for中的main循环:

fscanf(fpin, '%c', &code);

单引号用于字符常量。对于字符串,您需要双引号:

fscanf(fpin, "%c", &code);