c++ - Why can`t I put a zero in front of an integer in an "if" statement -
why little cheat code not work when put 0 in front of it. when take out , put little cheat code works fine, when put in , put cheat code in skip right over.
#include <iostream> #include <random> int main() { bool correct = false; std::cout << "put number 1 100" << std::endl; int guess = 0; std::cin >> guess; std::random_device rd; std::mt19937 mt(rd()); std::uniform_int_distribution<int> rnd(0, 101); int answer = rnd(mt); //random number while (correct == false) { if (guess == answer) { std::cout<<"you got it!!!"<<std::endl; correct = true; } else if (guess == 01010101)#<--------------------problem #it works when 1010101 { std::cout<< answer<< std::endl; std::cout << "put number 1 100" << std::endl; std::cin >> guess; } else if (guess > answer) { std::cout<<"your guess high guess again"<< std::endl; std::cout << "put number 1 100" << std::endl; std::cin >> guess; } else if (guess < answer) { std::cout<<"your guess low guess again"<< std::endl; std::cout << "put number 1 100" << std::endl; std::cin >> guess; } else { std::cout << "put number 1 100" << std::endl; std::cin >> guess; } } }
a leading 0 on numeric literal indicates literal expressed in octal (base 8) notation1, (obviously) has different magnitude same digits interpreted decimal (base 10).
the octal number 01010101 equal decimal number 266305.
1 unless leading 0 followed x
, it's interpreted hexadecimal (base 16).
Comments
Post a Comment