0

I have recently started learning C++ and wanted to try something just for fun. But I ran into a problem. Here is the code:

#include <iostream>

int num;

void enter_code(int passcode) {
  std::string secret_knowledge = "E=mc2";

  if (passcode == 0310) {
    
    std::cout << secret_knowledge << "\n";
    
  } else {
    
    std::cout << "Sorry, incorrect!\n";
    
  }
}

int main() {
  
  std::cout <<"Enter pass\n";
  std::cin >>num;

 if(num==0310)
 {
    enter_code(0310);
 }
 else{
    enter_code(num);
 }


}

Even if I enter the correct passcode, the output is "Sorry, incorrect!. Where am I going wrong?

rit25
  • 11
  • 1
  • 2
    Integers starting with `0` in C++ source code are treated as octal numbers, not decimal. Similar to `0x` or `0b`. Try giving 200 as the input (0*8*8*8 + 3*8*8 + 1*8 + 0). – yeputons Jun 25 '23 at 11:09
  • 1
    Also, note: there's no way to store a number with leading zeros. When you have `int i; std::cin >> i;` and enter `00013`, `i` will simply store `13`. If you need leading zeros, read the input as string and compare it as string. – Yksisarvinen Jun 25 '23 at 11:27

0 Answers0