Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 无限循环,我不知道为什么_C++ - Fatal编程技术网

C++ 无限循环,我不知道为什么

C++ 无限循环,我不知道为什么,c++,C++,我在一个学校实验室工作,我似乎不明白为什么代码一直在无限循环。我知道while语句有问题,但我看不出是什么 #include <iostream> using namespace std; int main() { int c = 0, p = 0, prof; cout << "Do you like Coke or Pepsi? " << endl; cin >> prof; //put in data va

我在一个学校实验室工作,我似乎不明白为什么代码一直在无限循环。我知道while语句有问题,但我看不出是什么

#include <iostream>
using namespace std;
int main()
{
    int c = 0, p = 0, prof;

    cout << "Do you like Coke or Pepsi? " << endl;
    cin >> prof;

    //put in data validation


    while (prof != 'q' && prof != 'Q')
    {
        if (prof == 'p')
            p++;
        else if (prof == 'c')
            c++;

        cout << "Do you like Coke or Pepsi? " << endl;
        cin >> prof;
    }


    if (p > c)
        cout << "Pepsi Wins";
    else if (p < c)
        cout << "Coke Wins";

    else
        cout << " It's a tie";

    system("Pause");

}
#包括
使用名称空间std;
int main()
{
int c=0,p=0,prof;
库特教授;
//输入数据验证
而(prof!='q'&&prof!='q')
{
如果(prof=='p')
p++;
else if(prof='c')
C++;
库特教授;
}
如果(p>c)

cout只是变量
prof
应该是
char
而不是
int

char prof;

如果您正在检查字符,为什么
prof
int
是一个
int
变量,但您试图在其中存储
char
?明智的做法是在使用输入操作的结果之前验证输入操作是否成功。请参阅。一旦输入操作失败,则所有后续操作都可能会失败,并且ut除非清除错误状态,否则将等待使用输入。尝试将字母作为
int
将不起作用,但将单个字母解释为
char
将起作用。尝试将99作为输入输入这是一个很好的示例,说明在哪里使用调试器可以回答问题-正如您所看到的,prof并不是您所期望的那样不使用命名空间STD;int({ int=0),p=0;char PROF;cOutProf;(Prof=‘q’& & of!’=’q’){if(PROF=‘p’& & PROF=‘P’’)p++;否则,(Prof=‘C’& & PRO=='C’)C++;CUT PROF;}(P>C)CUT,因为它发现<代码> P==C<代码>,U输入什么?