If Statement Doesn't Work Correctly - C++ Forum

Có thể bạn quan tâm

cplusplus.com
  • TUTORIALS
  • REFERENCE
  • ARTICLES
  • FORUM

C++

  • Tutorials
  • Reference
  • Articles
  • Forum

Forum

  • Beginners
  • Windows Programming
  • UNIX/Linux Programming
  • General C++ Programming
  • Lounge
  • Jobs
  • Forum
  • Beginners
  • if statement doesn't work correctly

if statement doesn't work correctly

Tima11 (14) 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.
1234567891011121314151617181920212223242526272829303132333435 #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! Ganado (6865) = 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 jlb (4973) Do you know the difference between the assignment operator= and the comparison operator==? Tima11 (14) Thanks really much! It worked Tima11 (14) My Visual Studio getting an error: C6385. And I had to write assignment operator in if statement. But it worked. Ganado (6865)
C6385.
It helps more if you copy the actual, full error message and pertinent code. Last edited on Tima11 (14) 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 Ganado (6865) 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. Tima11 (14) 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:
12345678910111213141516 #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 Ganado (6865) 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
1234 if (i <= 20) { j = a[i]; }
do
1234 if (i < 20) { j = a[i]; }
Tima11 (14) Thank you at all! I will know. Topic archived. No new replies allowed. Home page | Privacy policy© cplusplus.com, 2000-2025 - All rights reserved - v3.3.3Spotted an error? contact us

Từ khóa » C C6385