-4

皆さん、char 変数で「if」ステートメントを使用しようとしていますが、「yes」条件が満たされたときに気付かないようです。配列なしでこれを行う方法があるかどうかはわかりません。以下は私のコードです。どんな助けでも大歓迎です。

// 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];ここでは 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 に答える