在if语句C++中使用char
伙计们,我试图使用一个带有char变量的if语句,但它似乎没有注意到何时满足yes条件。我不知道在没有阵列的情况下是否有办法做到这一点。下面是我的代码。非常感谢您的帮助在if语句C++中使用char,c++,if-statement,char,C++,If Statement,Char,伙计们,我试图使用一个带有char变量的if语句,但它似乎没有注意到何时满足yes条件。我不知道在没有阵列的情况下是否有办法做到这一点。下面是我的代码。非常感谢您的帮助 // Running times calculator # include <iostream> # include <math.h> using namespace std; int main () { float cTime; float gTime; float cDi
// 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;
}
处理字符数组的方式有一些问题 字符响应[1];在此处创建一个由一个字符组成的字符数组,但在以下几行中将其视为字符串: 扫描%s,&响应; 如果响应==是
还请注意,您不能简单地将char数组和字符串文本与==进行比较,您所要做的只是比较地址。您要么必须使用strcmp,要么最好使用std::string。没有检查所有代码,但您使用了
operator ==
这不适用于char[],它是用于字符串的
请改用strcmp。此外,如果响应是字符数组,scanf%s和response将调用UB。谢谢您的建议。我现在开始学习字符串的工作原理。未知标识符printf。你忘了包括或@博格尔:为什么这不是C++?是否有任何语句不在C++语言中?@ ToasasMatthWS PrimTf,Sncf,char数组。这是C代码,因此应该标记为C@ @ BoGueLead:我可以在C++下编译它。C++标签是否意味着问题必须使用C语言中的一些C++语言元素?