Hey everyone, how my code should work is a console screen in which the user enters a date using format dd/mm/yyyy
Regardless of logical errors (checking days if bigger than 31 or negative values or.. etc) My code should be able to validate the following:
- if the user enters a letter, the program should output an error message
- if the user typed the date as 8/2/2014 the program should convert it to 08/02/2014 without returning an error message
Here's my code so far, I really can't find the bugs in it
as you notice, I tried two different validating formats in the days and months, unfortunately they both failed..
Thanks for helping!
Here's how my program is outputting so far:
Regardless of logical errors (checking days if bigger than 31 or negative values or.. etc) My code should be able to validate the following:
- if the user enters a letter, the program should output an error message
- if the user typed the date as 8/2/2014 the program should convert it to 08/02/2014 without returning an error message
Here's my code so far, I really can't find the bugs in it
Code:
#include<iostream>
using namespace std;
main(){
char d[2], m[2], y[4], br1, br2, dx, mx;
bool end, nobr1, nobr2;
do{
cout << "Enter Date (format: dd/mm/yyyy):" << endl;
d={'n','n'};
m={'n','n'};
y={'n','n','n','n'};
dx = 'n';
mx = 'n';
end = false;
nobr1 = false;
nobr2 = false;
for(int i=0;i<2;i++)
{
cin >> d[i];
}
if(d[0]!='1' && d[0]!='2' && d[0]!='3' && d[0]!='4' && d[0]!='5' && d[0]!='6' && d[0]!='7' && d[0]!='8' && d[0]!='9' && d[0]!='0'){
cout << "Wrong day value, please enter date again" << endl << endl;
end = true;
}
if((d[0]=='1' && d[0]=='2' && d[0]=='3' && d[0]=='4' && d[0]=='5' && d[0]=='6' && d[0]=='7' && d[0]=='8' && d[0]=='9' && d[0]=='0') && (d[1] == '/' ||d[1] == '\\' ||d[1] == '*' ||d[1] == '.' )){
d[1] = d[0];
d[0] = '0';
br1 = '/';
nobr1 = true;
}
if(nobr1 == false) cin >> br1;
for(int i=0;i<2;i++)
{
cin >> m[i];
if(m[i]!='1' && m[i]!='2' && m[i]!='3' && m[i]!='4' && m[i]!='5' && m[i]!='6' && m[i]!='7' && m[i]!='8' && m[i]!='9' && m[i]!='0'){
if (end == true) continue;
cout << "Wrong month value, please enter date again" << endl << endl;
end = true;
}
}
cin >> br2;
for(int i=0;i<4;i++)
{
cin >> y[i];
if(y[i]!='1' && y[i]!='2' && y[i]!='3' && y[i]!='4' && y[i]!='5' && y[i]!='6' && y[i]!='7' && y[i]!='8' && y[i]!='9' && y[i]!='0'){
if (end == true) continue;
cout << "Wrong year value, please enter date again" << endl << endl;
end = true;
}
}
if (end == true) continue;
cout << d[0] << d[1] << "/" << m[0] << m[1] << "/" << y[0] << y[1] << y[2] << y[3] << endl << endl;}while(y[0] != 0);
return 0;
}
Thanks for helping!
Here's how my program is outputting so far:
Comment