#include <cstdlib> #include <iostream> #include <windows.h> using namespace std; int hp=100; char start[20]; void draw() { system("color 7"); } void enter_start(){ cout <<"Добро пожаловать в CPP Wars!" <<endl; cout <<"Введите 'Start' для начала игры" <<endl; cin >>start; cout <<"Начинаем игру!" <<endl; system("cls"); } void game(){ cout <<"У вас " <<hp <<" делаем ход" <<endl; if (rand()% 2 == 0) { system("color a"); cout <<"Вас ударили -1 hp!" <<endl; hp-=1; Sleep(2000); system("cls"); } else { system("color c"); cout <<"Вы восстановили силы +1 hp" <<endl; hp+=1; Sleep(2000); system("cls"); } } void check(){ if (start=="Start") { game(); } else { cout <<"Команда введена не верно! Повторите попытку " <<start <<endl; } } int main() { setlocale(LC_ALL, "Russian"); while(hp > 0){ draw(); enter_start(); check(); } system ("color 4"); cout <<"Игра окончена" <<endl; cout <<"Вы умерли..." <<endl; return 0; } 

The check() function for some reason does not work, when you enter "Start" it still displays a message stating that "Команда введена не верно!" I can not understand where I was wrong, I ask for your help!

  • And what line do you enter? The register here is important - free_ze
  • @free_ze, I understand that the register is important. Therefore, I enter what I check, I have a char variable. Maybe this is the problem? - Malyuga
  • Wow How can we help you if you have not provided all the necessary code? We are not telepaths. Read this . Show the entire program code (but first discard everything that is not related to the problem) so that we can compile it and check it. - HolyBlackCat
  • @HolyBlackCat updated, pasted all the code! - Malyuga

2 answers 2

Classic cant with string comparison using pointers.

You == do not compare the contents of strings, but their addresses. Once these are two separate lines, the addresses are always different, and false always returned.


In C ++, it is not customary to use character arrays to store strings (without much need). For this there is a std::string .

Replace char start[20]; on std::string start; and it will work. Remember to add #include <string> .

(Since you have a using namespace std; then string start; also suitable. But using namespace std; - the thing is not very good , I would advise you to refuse it.)

In addition, it solves the problem of entering more characters than there are places in the array. std::string itself would stretch to the desired size, and going beyond the boundaries char name[20]; will cause undefined behavior, and most likely a crash.


If you insist on char name[20] , then to compare it with another string, use std::strcmp . This is done in the barbaric lands of C, where there is no such luxury as std::string .

Either use a loop and compare strings character by character, but this makes little sense, because strcmp already doing the same thing.

  • Thank you, your answer helped me! Everything is clear and clear, good luck to you! - Malyuga

See, comparing type variables

 char str[]; 

or

 char * str; 

with a string literal (or among themselves - it does not matter)

 str == "String" 

compares two pointers - str and a pointer to a string literal, but not the contents of strings. Obviously, these are two different pointers, and such a comparison is always false.

C-style strings are compared using the strcmp function. Using the == operator, you can compare the contents of strings of the string type (but this is not your case, unless you do the conversion to string ...).