+ 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 .
6 Réponses
+ 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 : )
+ 2
Thanks for the help😊
+ 1
check out std::strtok
http://en.cppreference.com/w/cpp/string/byte/strtok
+ 1
can some write a simple code on the same??
0
you will need to parse the string.
0
I dont know about parsing of string?