Hey
How do i ensure a user only enters 4 values into an integer.
entered values will range from 0000-9999
Hey
How do i ensure a user only enters 4 values into an integer.
entered values will range from 0000-9999
thats works
thanks
Read the user input as a string and then parse using strtol(). Search the forum for strtol(). Cast to an integer to convert from long to int. Use printf() formatting to add leading zeros when you report the value.
Todd
Mainframe assembler programmer by trade. C coder when I can.
Remember that kind of loop, please.
It's used a great deal in all kinds of programs.
You're welcome.
on second thoughts what stops the user from entering 1,2 or 3 values insted of the required 4?
Are you saying the number has to consist of exactly four digits? Or that at most it consists of four digits? If you want the former, you just have to change the condition at the bottom of the do...while loop posted above.
yes sorry i should have mentioned that the value entered by the user will be 4 values.
its a uni project part of the criteria is that i have to make sure that the ammount of digits entered is 4 and nothing else.
Then your first loop condition will be to ensure the user enters 4 characters. Therefore, you have to accept it as a string, not as a number.
Mainframe assembler programmer by trade. C coder when I can.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"