-
Split int variable
Im VERY new to C++ and programming in general.. so if this is all very simple... sorry.
Im having trouble trying to divide a number into 2 parts, basically, you have to enter a 7 digit number, and then split it into 2 other int variables, for example, if you entered 1234567, then you would split it so that p1 = 1234 and p2 = 567.
I cant figure out how to do this...
this is what the code looks like leading up to that part...
int main()
{
int id, a, b, p1, p2;
cout<<"Please enter your 7-digit ID: ";
cin>>id;
Please help im really stuck....
-
Try this:
Code:
p2 = id % 1000;
p1 = id / 1000;
-
3-digit part in p1, 4-digit part in p2.
Code:
p1 = (id / 10000); // Basically a shift of the decimal point, which ints don't carry past.
p2 = (id - (p1 * 10000));
-
-
Would it be possible to use the cin.get function to do this as well ?