gpt4 book ai didi

c++ - 为 C++ 编写不递增的味觉测试程序

转载 作者:行者123 更新时间:2023-11-28 06:27:37 25 4
gpt4 key购买 nike

我正在为一个类编写一个口味测试程序,它会在我的 if 语句之前运行良好。但是,一旦我进入我的 if 语句,它就不会递增 i,所以它永远不会离开 while 循环。

   #include <iostream>
using namespace std;

void main()
{
int i = 0;
int q = 0;
int p = 0;
int c = 0;
char preference;
int x = 0;
cout << "How many taste tests would you like to do?" << endl;
cin >> x;
while (i<x)
{
cout << "Do you prefer Coke, Pepsi, or are they the same? Use c for coke, p for pepsi, and q for the same\n";
cin >> preference;
if (preference == 'q' || preference == 'Q')
{
q = q + 1;
i++;
}
if (preference == 'p' || preference == 'P')
{
p = p + 1;
i++;
}
if (preference == 'c' || preference == 'C')
{
c = c + 1;
i++;
}

}
if (p>q)
{
cout << "Pepsi wins" << endl;
if (c>p)
cout << "Coke wins" << endl;
if (c == p)
cout << "Tie" << endl;
}
}

最佳答案

我认为您正在尝试检查确定的字符是否被 cin >> preference 单击,但您正在测试 int 变量,所以如果我按 'q '

//if(preference==q || preference==Q) // 'q'==0 || 'p'==0
if(preference=='q' || preference=='Q') // 'q'=='q' || 'q'=='Q'

所以添加''

希望对你有帮助

关于c++ - 为 C++ 编写不递增的味觉测试程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28242407/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com