+ 3

How to take input from user in a specific format in c++?

for example if we want the user to give input for the date in the following format : 01/01/2018 and we want to read the day, month, and year separetly .

14th May 2018, 5:02 AM
Aman Kumar Jain
Aman Kumar Jain - avatar
6 Answers
+ 3
A noob way: - Get the input into string variable named e.g. <input>. - Create a string array with 3 elements e.g. <arr>, use a for loop to iterate <input>, copy each numeric character to <arr> filter numeric characters by using std::isdigit() function. http://en.cppreference.com/w/cpp/string/byte/isdigit - Skip/ignore non numeric characters, except when you find a '/', increment the index of <arr> element used, so next copied character will be stored in the next element. - Use the string conversion function to convert the <arr> elements to number. http://en.cppreference.com/w/cpp/string/basic_string/stol http://en.cppreference.com/w/cpp/string/basic_string/stof - If all 3 elements are correctly populated then you have your data. Good luck : )
14th May 2018, 5:54 AM
Ipang
+ 2
Thanks for the help😊
14th May 2018, 6:04 AM
Aman Kumar Jain
Aman Kumar Jain - avatar
14th May 2018, 5:33 AM
Cain Eviatar
Cain Eviatar - avatar
+ 1
can some write a simple code on the same??
27th May 2018, 6:29 PM
Kelvin
0
you will need to parse the string.
14th May 2018, 5:26 AM
Cain Eviatar
Cain Eviatar - avatar
0
I dont know about parsing of string?
14th May 2018, 5:30 AM
Aman Kumar Jain
Aman Kumar Jain - avatar