How would i format the follwing code without using string so user can enter dash
ID: 3803689 • Letter: H
Question
How would i format the follwing code without using string so user can enter dash (-) or slash(/) with the date.
My program wroks but when i use dash or slash, i get wrong answer.
{
int day, month, year;
cout << "Please enter Date in M-D-Y (12-28-2016) ";
cin >> month >> day >> year;
while (month <= 12 && day <= 31){
//noOfDays Funcion call
noOfDays(day, month, year);
month += 12;
}
cout << "Invalid Month or Date" << endl;
system("pause");
return 0;
}
Explanation / Answer
Ans:- There are various ways to take '-' or '/' as input. Ihave give two ways of doing that. As you have no provided the full program
I cannot recheck with your program.But this works fine with my program. If you have any problem, the please repost your whole code and I will provide the solution.
Method 1:-You can use a char variable to read '-' or'/' .
#include <iostream>
using namespace std;
int main()
{
int day, month, year;
char slash_dummy;
cout << "Please enter Date in M-D-Y (12-28-2016) ";
cin >> month>> slash_dummy >> day>>slash_dummy >> year;
// while (month <= 12 && day <= 31){
// // //noOfDays Funcion call
// // noOfDays(day, month, year);
// // month += 12;
// }
cout << "Invalid Month or Date" << endl;
// system("pause");
return 0;
}
Method 2:- You can use get() function if you are sure the format will be correct.
example-
#include <iostream>
using namespace std;
int main()
{
int day, month, year;
char slash_dummy;
cout << "Please enter Date in M-D-Y (12-28-2016) ";
cin >> month;
cin.get();
cin>>day;
cin.get();
cin>> year;
// while (month <= 12 && day <= 31){
// // //noOfDays Funcion call
// // noOfDays(day, month, year);
// // month += 12;
// }
cout << "Invalid Month or Date" << endl;
// system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.