How to take input for month ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to take input for month ?

If someone enters a date in dd-mm-yyyy format ('-' sign included). Then how to take mm as number input and save in some variable. Also how to convert two elements of char array into a number.Take below input, if i am saving this in a char array then if i only need month then how to convert it into number arr[3] and arr[4] into a variable x. sample input: 27-06-2019 30-12-2019

21st Jun 2019, 12:49 PM
Aditya Kumar
Aditya Kumar - avatar
4 Answers
+ 1
It depends on your preferance, if you want to use array of char as an input, then it will be different, but if you want to use integer, then that one is fine. Also, always put return 0 in the end if your main function return an integer
21st Jun 2019, 2:15 PM
Agent_I
Agent_I - avatar
+ 1
Thanks
21st Jun 2019, 2:23 PM
Aditya Kumar
Aditya Kumar - avatar
0
You can use atoi function in cstdlib to convert string into integer
21st Jun 2019, 2:03 PM
Agent_I
Agent_I - avatar
0
I find a solution online is this possible to use char array (my approach) or below solution is better Also its uses int main but its not returning zero i mean code just run fine without any error #include<iostream> using namespace std; int main() { int d; int m; int y; cin >> d; // read the day if (cin.get() != '/' ) // make sure there is a slash between DD and MM { cout << "expected /\n"; return 1; } cin >> m; // read the month if (cin.get() != '/' ) // make sure there is a slash between MM and YYYY { cout << "expected /\n"; return 1; } cin >> y; // read the year cout << "input date: " << d << "/" << m << "/" << y << "\n"; }
21st Jun 2019, 2:08 PM
Aditya Kumar
Aditya Kumar - avatar