首页 > 解决方案 > 为什么我的 C 程序会跳过 if 语句?

问题描述

我是一个新的编码员,不知道我在做什么,请帮忙!

代码正在读取并接受输入,直到它到达 scanf(" %c", &i); 然后它跳到 Amount 打印似乎忽略了我的 if 语句。我使用 scanF 有什么问题吗?

这是代码:

#include <stdio.h>
int main(){
printf("BANK ACCOUNT PROGRAM!\n--------------------\n");
char W,F,A,i,D; 
int t=1;
double b,d;

while (t=1){
    printf("Enter the old balance:\n");
    scanf(" %lf", &b);
    printf(" %lf", b);
    if(b>=0)
        {
    printf("Enter the transactions now!\n Enter an F for the transaction type when you are finished.\n");

    printf("Transaction Type (D=deposit, W=withdrawal, F=finished):\n");
    scanf(" %c", &i);
    if(i=F){
        printf("Your ending balance is");
        printf(" %lf", b);
        printf("\n Program is ending!");

        return 0;
        }
    if(i=W){
        printf("Amount:");
        scanf(" %f", &d);
        b= b-d;
        printf(" %f",b);}
    }
    if(b<0);
        {
            printf("The balance must be maintained above zero!\n");

    }


    }
    return 0;
}

标签: c

解决方案


因为你的比较是错误的

if(i=F){  // this is assignment, not comparison

应该

if(i=='F'){  // note also it's comparison to character 'F'

推荐阅读