-4

伙计们,我正在尝试使用带有 char 变量的“if”语句,但它似乎没有注意到何时满足“是”条件。我不知道是否有办法在没有数组的情况下做到这一点。下面是我的代码。任何帮助深表感谢。

// Running times calculator

# include <iostream>
# include <math.h>

using namespace std;

int main ()
{
    float cTime;
    float gTime;
    float cDist;
    float gDist;

    float min;
    float sec;

    float cMin;
    float cSec;
    float p1000;

    char response[1];

    int blank;

    printf ("Welcome to the running times calculator.\n\nEnter your completed race distance in metres: \n");
    scanf ("%f", &cDist);
    printf("Enter your completed race time. Type minutes, hit enter. Type seconds, hit enter\n");
    scanf ("%f" "%f", &cMin, &cSec);

    cTime = cSec+(60*cMin);
    p1000 = pow(1000/cDist,1.1)*cTime;

    printf ("Would you like to enter another race time to improve prediction accuracy? \n");
    scanf ("%s", &response);

    if(response == "yes")
    {
       printf ("Enter your completed race distance in metres: \n");
       scanf ("%f", &cDist);          
       printf("Enter your completed race time. Type minutes, hit enter. Type seconds, hit enter\n");
       scanf ("%f" "%f", &cMin, &cSec);

       cTime = cSec+(60*cMin);
      p1000 = ((pow(1000/cDist,1.1)*cTime)+p1000)/2;

    }

    printf ("What is your goal race distance in metres? \n");
    scanf ("%f", &gDist);

    gTime = pow(gDist/1000, 1.1)*p1000;
    min = gTime/60;
    sec = remainder(gTime,60);

    if (sec < 0)
    {
    sec = sec + 60;
    min = min - 1;    
    }
    printf ("Your predicted time for a race of %.0f metres is %.0f minutes and %.0f seconds", gDist, min, sec);
    scanf("%f", &blank);

    return 0;
}
4

2 回答 2

3

您处理 char 数组的方式存在一些问题。

char response[1];您在此处创建一个由一个字符组成的 char 数组,但随后将其视为以下行中的字符串:

scanf ("%s", &response); if(response == "yes")

另请注意,您不能简单地将 char 数组和字符串文字与==您要做的所有事情进行比较地址。您要么必须使用 strcmp(),要么更好地使用 std::string。

于 2013-07-14T20:06:20.570 回答
1

没有检查所有的代码,但你使用

operator ==

这不适用于char [],而是用于字符串使用strcmp。

于 2013-07-14T20:06:35.760 回答