2016-10-17 9 views
-2

私は、入金(D)または払い戻し(C)のどちらの入力をユーザに求める銀行口座の割り当てを割り当てられました。彼らが選択した後、彼らの銀行口座の残高を表示すると仮定します。fscanfを正しく使用していますか?とswitch文?

私が問題を抱えているのは、私のfopenとfscanfを扱うビルドエラーが発生し続けていることです。私のコードの問題は何ですか?

重大度コード説明プロジェクトファイルの行 エラーC4996 'fscanf':この関数または変数は安全でない可能性があります。代わりにfscanf_sを使用することを検討してください。廃止予定を無効にするには、_CRT_SECURE_NO_WARNINGSを使用してください。詳細については、オンラインヘルプを参照してください。 checkingAccountのC:ドキュメントは、Visual Studio 2015 \プロジェクト\ \ \ユーザー\ CSIS \ 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; 
} 
+2

"私はfopenとfscanfを処理するビルドエラーが発生し続けています。" - >エラーを投稿してください。 – chux

+1

'='は数式を設定せず、現在の値を使用して計算を実行します –

+0

浮動小数点値をお金に使うときは注意してください: 'double'に正確に' 0.01'の値を格納することはできません。そのため、100倍を追加すると、1ドルの全体が得られません。 – usr2564301

答えて

1

ここにあなたの問題です。 mainforループでは:

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

単一引用符は文字定数に使用されています。文字列の場合、二重引用符を使用する必要があります。

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

... 'fscanf'の返り値を確認することをお勧めします。 –

+0

良いアイデアはより多くの警告(例えば' gcc -Wall')でコンパイルし、コンパイラで診断された問題を修正することです。 – chqrlie

関連する問題