I'm trying to figure out how to input a number and make sure it ouputs as five digits. For example, I might ask the user to input an account number and say they input 234, I would want the output to show 00234.
You can do this very easily, without any conversions or anything, with just one of the cout functions and one thing under #include<iomanip>. Look up the cout.fill function and setw. If you still can't get it, I can just post a little 5 line code I wrote demonstrating it. It's very simple, please don't go in to converting to string or anything for this one thing, although you could do that and make it work.
I got it with this in my output command.
setw (5) << right << setfill ('0') << id
Easy enough. For some reason I was having trouble seeing my way through it. I had thought about strings but am still having a little difficulty manipulating them.
I didn't post the whole ouptut statement and I have a << left before this so I used << right to correct that.
This solution worked for the initial question, but now I notice when I input 6 digits (123456),
the output is six digits and I want five. I know setw specifies the minimum number of spaces but how do I set the max?
Are you still taking the input as an integer? Remember that all integers have valid char representations so they can all be input and\or output as strings. I'd say take the users input just like you would any other string and use the functions built in to do what ever you want with them.
Your second question has an undefined variable, do you want the first five or the last five digits? This is actually a different question from your first one so it would not have the same solution.
I've changed my code to input as a string and have managed to deal with the second question when six numbers are input, reducing it to five (using the first five digits). Now I'm not sure how to fix the initial problem when less than five digits are entered (input 23, output 00023).
This is what I have:
I would fill the string with 0's initially then user push_back: http://www.cplusplus.com/reference/string/string/push_back/ to fill it in based on user input. But depending on your style the member functions under the See Also could be useful as well.