使用“ while”

时间:2018-10-10 21:47:02

标签: c while-loop

我是Stackoverflow的新手,并且对编码非常陌生。只是弄乱C。这就是我要在这里做的(不要将此程序科学地准确地对待),这是一个为长度,质量和时间的相对论方程计算的程序。我实际上有3个问题:

当我尝试在y / n问题中输入其他字符时,一切正常,但是例如,如果我输入“ sfkl”,则警告会出现4次,因为我输入了4个字符。如果我放空格,它甚至不会发出警告,直到我放另一个字符然后输入。无论我在一行(包括空格)中输入多少个字符,我都可以发出1条警告吗?

我的另一个问题是,我避免输入y / n以外的任何东西,但对于双值输入(质量,长度和时间),我无法弄清楚类似的系统(在和上寻求双值)。再次)。你能给我建议一个解决方案吗?

我的第三个问题是,当执行“ scanf_s(”%c“,&answer);”时,如果我在“%c”之前不加空格,则它不能正常工作。它注册一个输入,并要求我仅输入y / n。为什么在此之前需要一个空格?

代码如下:

#include <stdio.h>
#include <math.h>
#define LIGHT 299792458

int input();

int main()
{
    printf("\n\n\tThis program calculates how length, mass and time changes with respect to your speed.\n\n\tThe values you enter are the quantites which are observed by a stationary observer and the output values are the quantites observed by the person in a vehicle which is moving at the speed that you enter.");

    input();

    return 0;
}

int input()
    {
        double length, mass, utime, speed;
        char answer;

        do
        {
            printf("\n\n     **************************************************");

            printf("\n\n\tPlease enter a quantity of length: ");
            scanf_s("%lf", &length);

            printf("\n\tPlease enter a quantity of mass: ");
            scanf_s("%lf", &mass);

            printf("\n\tPlease enter a quantity of time: ");
            scanf_s("%lf", &utime);

            printf("\n\tNow enter the speed of the vehicle (m/s): ");
            scanf_s("%lf", &speed);

            while (speed > LIGHT)
            {
                printf("\n\n\tNothing can surpass the speed of light in the universe. Enter a smaller value: ");
                scanf_s("%lf", &speed);
            }

            double newlength = length * (sqrt(1 - pow(speed, 2) / pow(LIGHT, 2)));
            double newmass = mass / (sqrt(1 - pow(speed, 2) / pow(LIGHT, 2)));
            double newutime = utime / (sqrt(1 - pow(speed, 2) / pow(LIGHT, 2)));

            if (speed == LIGHT)
            {               
                printf("\n\n     **************************************************");

                printf("\n\n\n\tIt's technically impossible to reach the speed of light if you have mass but here are the mathematical limit results:\n\n\t*The new length quantity is 0\n\n\t*The new mass quantity is infinity\n\n\t*The new time quantity is infinity\n\n\n\t- Time successfully dilated -\n\n");

                printf("\n\tDo you want to start over? (y/n): ");
                scanf_s(" %c", &answer);

                if (answer == 'n')
                {
                    return 0;
                }
                else if (answer == 'y')
                {
                    continue;
                }
                else
                {
                    while (answer != 'y' && answer != 'n')
                    {
                        printf("\n\tPlease only enter 'y' or 'n': ");
                        scanf_s(" %c", &answer);
                    }
                }
            }


            if (speed < LIGHT)
            {
                printf("\n\n     **************************************************");

                printf("\n\n\n\t*The new length quantity is %.20lf\n\n\t*The new mass quantity is %.20lf\n\n\t*The new time quantity is %.20lf\n\n\n\t- Time successfully dilated -\n\n", newlength, newmass, newutime);

                printf("\n\tDo you want to start over? (y/n): ");
                scanf_s(" %c", &answer);

                if (answer == 'n')
                {
                    return 0;
                }
                else if (answer == 'y')
                {
                    continue;
                }
                else
                {
                    while (answer != 'y' && answer != 'n')
                    {
                        printf("\n\tPlease only enter 'y' or 'n': ");
                        scanf_s(" %c", &answer);
                    }
                }
            }
        }
        while (answer == 'y');

        return 0;
    }

谢谢,祝你有美好的一天

1 个答案:

答案 0 :(得分:0)

scanf的返回值是成功解析的元素数;您可以使用它重复直到成功读取某些内容:

double nr=0;
while (!feof(stdin) && scanf("%lf",&nr)!=1) {
  printf("not a number; try again.");
  while ( (c = getchar()) != '\n' && c != EOF ) { }
}

请注意,您必须从缓冲区中取出“无效”输入。否则,scanf将一次又一次地失败。