Limiting Integer Input from User

closed account (yh7EwA7f)
Hello all, I have been given an assignment to accept an 8 digit id in the form of an integer. I was wondering if there is a way to limit the user's input, or to take in the first 8 numbers imputed by the user.

Something like if the user entered 123456789, I would only use 12345678 and disregard anything after 8.
Thanks:)

*I am new to C++ and my code is quite simple*
1
2
cout << "Enter an ID: ";
cin >> x;
If that is the case, it is simple.

1
2
3
4
5
6
7
string buffer;
cout << "Enter an ID: "; cin >> buffer;
buffer = buffer.substr(0, 8); // I would only use 12345678 and disregard anything after 8.

stringstream ss;
ss << buffer;
ss >> x; // I will input the variable x 
closed account (yh7EwA7f)
Thank you so much this worked and was very helpful!!
Topic archived. No new replies allowed.