Reputation: 341
In c++ will their be any error if we input an integer containing leading zereos.
for eg:
int a;
cin>>a;
we give an input 00 or 01.
or inputing with the help of string for this is a better idea.
Upvotes: 0
Views: 3992
Reputation: 33116
In c++ will their be any error if we input an integer containing leading zereos.
You might not get what you expect, depending on the settings of the input stream's format flags. The default is to expect user input to always be in decimal. Leading zeros have no effect. What if we turn that off by calling std::cin.unsetf()
?
int main () {
int i;
std::cin.unsetf (std::ios::dec);
while (std::cin >> ii) {
std::cout << i << "\n";
}
}
The output will be 25 if you enter 25, but if you enter 025 the output is 21. That's because C++ now interprets a leading zero on input to mean the number that follows is in octal (or in hexadecimal in the case of a leading 0x or leading 0X).
Upvotes: 3
Reputation: 409176
Integers (or floats for that matter) do not have leading zeroes. If you want to keep the leading zeroes then you have to read the input as a string instead, and convert it to number when needed. Or you can use formatting to add leading zeroes when printing results.
Upvotes: 4