输入无效时间单位时输入无限循环

时间:2018-05-01 18:00:43

标签: c

在此代码中,用户输入并相应地计算放射性输出。

代码完全正常,直到我要求用户输入时间单位,无论他输入正确的字符(h,d,y)还是错误的字符,代码打印出错误代码并再次询问时间单位,它永远不会结束! 从最后一个if语句开始

if(timeUnit!='h'|| timeUnit!='H'|| timeUnit!='d'|| timeUnit!='D'|| timeUnit!='y'|| timeUnit!='Y “)

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define year 365.25;            //Defining year
#define day 24.0                //Defining day


int main(int argc, const char * argv[])
{
double maxA0 = 1e6;
double A0;                  //initial activity
double t;                   //time elapsed since the initial activity measured
double oldt;                //will set the value of t = oldt before conversion.
char timeUnit;              //Type of time (hours, day or year)
double T;                   //half-life
int isotope;                //Type of isotope
double radioActive;         //Radio Active final result

printf("\nAvailable isotopes:\n");
printf("[1] Actinium 227    (Ac-227)\n");
printf("[2] Barium 140      (Ba-140)\n");
printf("Cesium 134          (Cs-134)\n");
printf("Iodine 132          (I-132)\n");

printf("\nEnter the isotope index (1-4)\n");            //Promoting user to enter index for isotope type
scanf("%d", &isotope);


if (isotope > 4 || isotope < 1) {
    do {
        printf("ERROR: That is not a valid index.\n");
        printf("\nEnter the isotope index (1-4)\n");
        scanf("%d", &isotope);
    }
    while (isotope > 4 || isotope < 1);
}


    switch(isotope)
{                                       //Switch to assign T value according to isotope type
    case 1:
        T = 21.77 * year;
        break;
    case 2:
        T = 12.8;
        break;
    case 3:
        T = 2.06 * year;
        break;
    case 4:
        T = 2.26 / day;
        break;

}

printf("\nEnter the initial activity A0 in microCi."); //Promoting user to input initial activity value
scanf("%lf", &A0);


if (A0 < 0 || A0 > maxA0) {
    do {
        printf("ERROR: That is not a valid initial activity level. \n");
        printf("\nEnter the initial activity A0 in microCi.");
        scanf("%lf", &A0);
    }
    while (A0 < 0 || A0 > maxA0);
}


printf("\nEnter the decay time t:\n");                  //promoting user to enter time
scanf("%lf", &t);


if (t < 0) {
    do {
        printf("ERROR: Negative or zero time is not valid.\n");
        printf("\nEnter the decay time t:\n");
        scanf("%lf", &t);
    }
    while (A0 < 0 || A0 > maxA0);
}

oldt = t;                                               //origina t input from user to appear in final equation (t timeUnit)


printf("\nEnter the time unit (h/d/y)\n");              // Promoting user to input type of time
scanf("  %c", &timeUnit );

//checking if time type entered is either hours, days or years.

if (timeUnit !='h' || timeUnit !='H' || timeUnit !='d' || timeUnit !='D' || timeUnit !='y' || timeUnit !='Y') {

    do {
        printf("ERROR: That is not a valid time unit.\n");
        printf("\nEnter the time unit (h/d/y)\n");
        scanf("  %c", &timeUnit );
    }
    while (timeUnit !='h' || timeUnit !='H' || timeUnit !='d' || timeUnit !='D' || timeUnit !='y' || timeUnit !='Y');
}

else if (timeUnit =='h' || timeUnit =='H' || timeUnit =='d' || timeUnit =='D' || timeUnit =='y' || timeUnit =='Y')
{
    if (timeUnit == 'h' || timeUnit == 'H')             //converting hours to days
        t = t / day;
    else if (timeUnit == 'y' || timeUnit == 'Y')        //converting years to days
        t = t * year;
}

radioActive = (double) (A0 * exp(-0.693*t/T));                     //radio active equation

printf("\nA(%.1lf %c) %.4e uCi\n", oldt,timeUnit,radioActive);      //desired output

return 0;

}

1 个答案:

答案 0 :(得分:0)

使用'或'运算符意味着如果不满足任何条件,将输入if语句。由于不可能同时满足所有这些条件,因此每次都会输入。使用'和'运算符。

if (timeUnit !='h' && timeUnit !='H' && timeUnit !='d' && timeUnit !='D' && timeUnit !='y' && timeUnit !='Y')