-2

everything's work fine but at the end of this code i write else if statement to exit the program when user press esc key but when user press that key nothing's happen !!!! what's my mistake ??? i write this code in 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;
}
ssaeidd
  • 1
  • 4
  • Don't call `srand` inside a loop, especially when you use `time` to get the seed. Call it *once* only, at the beginning of the program. – Some programmer dude Nov 13 '15 at 21:29
  • Why aren't you stepping through your code to find out? – Anon Mail Nov 13 '15 at 21:29
  • This is for PHP, but completely applicable for your specific problem: http://stackoverflow.com/questions/2063480/the-3-different-equals – Marc B Nov 13 '15 at 21:29
  • As for your problem, are you sure that the terminal/console will send the escape "character" to your program to read with `getchar`? Also, don't use the ["magic number"](https://en.wikipedia.org/wiki/Magic_number_%28programming%29) `13` for a carriage return, use the proper `'\r'` character. By the way, not all platforms send a carriage return for for newlines, not to mention that some functions *translate* the newline sequence so you only get the newline character `'\n'`. – Some programmer dude Nov 13 '15 at 21:31
  • Finally, if you're programming C++, why are you using the C-function `getchar`? – Some programmer dude Nov 13 '15 at 21:34
  • im new in c++ and i write this for practis – ssaeidd Nov 13 '15 at 21:43
  • There's also a fair amount of redundancy in using a `switch` in your code like that. You can just use `cout << "The dice is " << random(6) + 1;` intstead of the whole `switch (random(6) + 1) { //... }` thing you're doing. – Bryn McKerracher Nov 14 '15 at 02:11

1 Answers1

2

= is assignment in C++, it does not check equality.

Change your last if statement to if (ch == 27). EDIT: Change all of them to use ==

Luke Joshua Park
  • 9,527
  • 5
  • 27
  • 44
  • 1
    Your question was asking why the last if statement doesn't work, and I told you why. This isn't CodeReview. Poor programming practices weren't part of the question. – Luke Joshua Park Nov 13 '15 at 21:40
  • when i change = to == when i press enter the cursor just go to next line – ssaeidd Nov 13 '15 at 21:42
  • 1
    Did you change `char = getchar();` as well? Because if you did, that's wrong. Please just learn the difference between `=` and `==`. – Luke Joshua Park Nov 13 '15 at 21:50