为什么打破;当我按下esc时,dosn工作?

时间:2015-11-13 21:26:27

标签: c++ visual-studio-2013 break do-while

一切都运行正常但是在这段代码的最后我写了if if语句退出程序,当用户按下esc键但是当用户按下那个键时没有任何事情发生!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!什么是我的错误???我在visual studio 2013中写了这段代码。

/***************************************************\
* This program imagery dice rolling                 *
* Write by : saeid asaadian                         *
* Create date : 11 - 14 - 2015                      *
* Version : 1.0                                     *
\***************************************************/
#include <iostream>
#include "conio.h"
#include <ctime>
#include <cstdlib>
#define random(x)(rand()%x)
#define randomize()(srand(time(0)))
using namespace std;
int main()
{
    cout << "Please press ENTER to roll the dice and press ESC for exit .";
    do
    {
        char ch = getchar();
        if (ch = 13)
        {
            system("cls");
            randomize();
            switch (random(6) + 1)
            {
            case 1:
                cout << "The dice is 1";
                break;
            case 2:
                cout << "The dice is 2";
                break;
            case 3:
                cout <<"The dice is 3";
                break;
            case 4:
                cout << "The dice is 4";
                break;
            case 5:
                cout << "The dice is 5";
                break;
            default:
                cout << "The dice is 6";
            } //end of switch
        }  //end of if
        else 
        if (ch = 27)
            break;
    } while (1);   //end of do.while
    return 0;
}

1 个答案:

答案 0 :(得分:2)

=是C ++中的赋值,它不检查相等性。

将您的最后一句if语句更改为if (ch == 27)。 编辑:将所有这些更改为使用==