printf不会在其他地方打印

时间:2017-03-03 21:17:38

标签: c if-statement

我的代码有问题,if(flag!= -1)printf在输入除123和124之外的任何其他数字后没有打印错误消息。如果输入123和124一切正常,但提交我需要显示该错误消息。 这是我的代码:

#include <stdio.h>
#define distRate1 0.69
#define distRate2 0.49

struct Rental{
        int id;
        double baseDay;
        double earnings;
};

int main(){
        const int noVehicles = 2;
        int option;
        int flag;
        int tempID;
        int j;
        int i = 0;
        struct Rental vRent[] = {{123, 9.95, 0},{124, 19.95, 0}};
        printf("***** Rental Vehicle Management App *****\n");
        while(1){
                printf("\n1.) Rental Status\n");
                printf("2.) Apply Charges\n");
                printf("0.) Log out\n\n");
                printf("Please enter an option to continue: ");
                scanf("%d", &option);
                if(option == 0){
                        return 0;
                }
                if(option == 1){
                        printf("-- Rental Vehicle Status --\n\n");
                        printf("ID#      Earnings\n");
                        printf("-------- ----------\n");
                        for(j = 0; j < noVehicles; j++){
                                printf("%8d%10.2lf", vRent[j].id, vRent[j].earnings);
                        }
                }
                if(option == 2){
                        flag = -1;
                        printf("\n-- Rental Charges --\n\n");
                        printf("Enter vehicle ID: ");
                        scanf("%d", &tempID);
                        for(i = 0; i < noVehicles && flag == -1; i++){
                                if(tempID == vRent[i].id){
                                        flag = i;
                                        if(flag != -1){
                                                        int days, kms;
                                                        double BaseP, kmsC, total;
                                                        printf("Enter Rental Period (in Days): ");
                                                        scanf("%d", &days);
                                                        BaseP = (vRent[i].baseDay * days);
                                                        printf("Enter kilometers driven: ");
                                                        scanf("%d", &kms);
                                                        if(kms > 100){
                                                                kmsC = (100 * distRate1)+((kms - 100) * distRate2);
                                                        }
                                                        if(kms < 100){
                                                                kmsC = (kms * distRate1);
                                                        }
                                                        total = (kmsC + BaseP);
                                                        printf("\nBase   kmCost Total\n");
                                                        printf("====== ====== ======\n");
                                                        printf("%6.2lf %6.2lf %6.2lf\n", BaseP, kmsC, total);
                                                        vRent[i].earnings = total;
                                        } else {
                                                printf("ERROR: Vehicle ID does not exist.\n");
                                        }
                                }
                        }
                }
        }
return 0;
}

1 个答案:

答案 0 :(得分:0)

你已经放置了:

if(flag != -1){...} else{ printf("ERROR: Vehicle ID does not exist.\n"); }

条件声明:

if(tempID == vRent[i].id){ ...

哪个 - 如果是真的 - 总是这样:

flag = i;

意味着第一次检查将始终为真,因为flag始终首先被循环计数器i覆盖,因此永远不会为负。我认为您的意图是else条件与if语句相关,检查tempID是否等于vRent[i].id。将else一个大括号放低可以解决问题。