if statement doesn't work correctly

I'm new to C ++ and in my script to change the keyboard. For example: I press q and the program outputs a random letter. But when I press q for some reason, it outputs the random letter and the random letter that must output only when I press E.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <string>
#include <array>
#include <random>
#include <ctime>
#include<conio.h>
using namespace std;

int main()
{
    char key;
    int max = 25;
    int asciiValue;
    srand((unsigned) time(0));
    int randomnumber = ((unsigned) rand() % max);
    int randomnumber2 = ((unsigned) rand() % max);
    const char* letters[26] = { "q","w","e","r","t","y","u","i","o","p","a","s","d","f","g","h","j","k","l","z","x","c","v","b","n","m"};
    for (int i = 0; i < 100; i++)
    {
        i = 0;
        key = _getch();
        asciiValue = key;

        if (asciiValue == 27)
            break;
        if (asciiValue = 113)
        {
            cout << letters[randomnumber] << endl;
        }
        if (asciiValue = 101)
        {
            cout << letters[randomnumber2] << endl;
        }
    }
}

When I press q the program outputs:
randomletter that must output when I press q;
randomletter2 that must output when I press e;
When I press e the program outputs:
randomletter that must output when I press q;
randomletter2 that must output when I press e;

I don't know if statement doesn't work correctly of checking Ascii value.
Thanks for any ideas!
= is assignment
== checks for equality
Change your = to == in the if statements.

- Also, rand() % 25 can never output 25, so your last letter m is inaccessible.
- You shouldn't need to cast rand() itself to unsigned.
- You keep resetting i to 0 in your loop.
Last edited on
Do you know the difference between the assignment operator= and the comparison operator==?

Thanks really much! It worked
My Visual Studio getting an error: C6385. And I had to write assignment operator in if statement. But it worked.
C6385.
It helps more if you copy the actual, full error message and pertinent code.
Last edited on
warning C6385: invalid data: accessing buffer-name, the readable size is size1 bytes, but size2 bytes may be read: Lines: x, y. Solution of that error was just changing == operator to = in if statement. But now the error no longer appear. The link: https://docs.microsoft.com/en-us/cpp/code-quality/c6385?view=vs-2019
Last edited on
Putting = in your if statements is not the solution.
Please post the real source code that produces the warning, and show which line number the warning points to.
Sorry, but I can't find the code that was doing that error in my project.It no longer appear. But this code also generate that error:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

int main()
{
    unsigned int i;
    f(i);
}
void f(unsigned int i)
{
    char a[20];
    char j;
    if (i <= 20)
    {
        j = a[i];
    }
}

C6385 Reading invalid data from 'a': The readable size is 20 bytes, but 'i' bytes may be read
Thank you. The issue with those code you just posted is that the a array only has a size of 20.

This means that a[0] through a[19] are valid indices.
But a[20] itself is out of bounds of the array.

So instead of
1
2
3
4
    if (i <= 20)
    {
        j = a[i];
    }

do
1
2
3
4
    if (i < 20)
    {
        j = a[i];
    }
Thank you at all! I will know.
Topic archived. No new replies allowed.