1

I've got a problem with giving number to function.

how can I give a number with leading zero to my function?

int main()
{
    int number;
    cout << "enter your number : " << endl;
    cin >> number;
    desplay(number);
    desplay(0647);
}

void desplay(unsigned int number){
    int one = number%10;
    number /= 10;
    int two = number%10;
    number /= 10;
    int three = number%10;
    number /= 10;
    int four = number%10;
    number /= 10;
    std::cout << one << "\t";
    std::cout << two << "\t";
    std::cout << three << "\t";
    std::cout << four << "!\n";
}

output:

enter your number :                                                                                            
0647                                                                                                           
7       4       6       0!                                                                                     
3       2       4       0! 
walnut
  • 21,629
  • 4
  • 23
  • 59
  • 3
    A leading `0` means ["this number is represented in octal"](https://en.cppreference.com/w/cpp/language/integer_literal)! – BoBTFish Nov 25 '19 at 11:15
  • 2
    Possible duplicate of [What is special about numbers starting with zero?](https://stackoverflow.com/questions/26568200/what-is-special-about-numbers-starting-with-zero) or [C++ int with preceding 0 changes entire value](https://stackoverflow.com/questions/29325822/c-int-with-preceding-0-changes-entire-value) – walnut Nov 25 '19 at 11:18
  • You could use a string rather then an integer and parse it. This would also allow you to check for non-numeric values. Edit: Tbh wasn't thinking about the octal issue.. i mean you could just check if the fist character is a 0 as well. https://pastecode.xyz/view/ef4d738f – Jeremy Trifilo Nov 25 '19 at 11:42

0 Answers0