How can I cut off the leading digits of a number in order to only display the last two digits, without using the library. For example:
1923 to 23
2001 to 01
1234 to 34
123 to 23
with only
#include <iomanip>
#include <iostream>
Thanks!
How can I cut off the leading digits of a number in order to only display the last two digits, without using the library. For example:
1923 to 23
2001 to 01
1234 to 34
123 to 23
with only
#include <iomanip>
#include <iostream>
Thanks!
If your numbers are in int form (rather than string form), you should think about using the modulo operator.
If the numbers are in char[] form, there is an easy solution that involves indexing into the string, like:
char *myString = "ABCDE";
int lengthOfMyString = 5;
cout << myString[lengthOfMyString - 3]
<< myString[lengthOfMyString - 5]
<< myString[lengthOfMyString - 4];
//outputs the word CAB
If you're just working with integers I'd suggest just doing mod %100 for simplicity:
int num =2341;
cout << num%100;
Would display 41.
And if you need a leading zero just do:
std::cout << std::setw(2) << std::setfill('0') << num%100 << std::endl;