Howdie all.
Up front I'd like to thank your for taking the time to read this.
Here is my problem , I wrote a program that takes the date entered as MMDDYYYY, and it converts it to for example
01012000 to January 1, 2000..
Here is my problem when I type in the about input 01012000 I get January 20 2000...I know its a math error, but for the life of me I cannot find it...any help would be appreciated.
Oh and there is one more thing , If at all you could give suggestions on how to shorten the code, i would be very grateful, thanks again. Heres the code
Code:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
void input();
bool checkdigit();
bool checklength();
bool checkday();
bool checkmonth();
int contoint();
int contointd();
int contointy();
void printdate();
int n,a,b,c;
string date, stop, day, year, month;
bool ok;
int main()
{
input();
while (date != "stop")
{
ok = checklength();
if (ok == true)
ok = checkdigit();
if (ok == true)
{
month = date.substr (0,2);
day = date.substr (2,2);
year = date.substr (4,4);
a = contoint();
b = contointd();
c = contointy();
ok = checkmonth();
if (ok = true)
{
ok = checkday();
if (ok == true)
printdate();
}
}
input();
}
return 0;
}
void input()
{
cout << endl<< "Enter Stop to quit" << endl << "Enter the date in the form of MMDDYYYY or MDDYYY :";
cin >> date;
}
bool checklength()
{
if (date.length() == 7)
date = "0" + date;
if (date.length() != 8)
{
cout << "Your Lenght is wrong" << endl;
return false;
}
return true;
}
bool checkdigit()
{
n = 0;
while (n <=7)
{
if (date[n] >= 48 && date[n] <= 57)
n++;
else n = 50;
if(n == 50 || n == 0)
{
cout << "Use all numbers"<< endl;
return false;
}
}
return true;
}
int contoint()
{
int a = 0, x = 0;
int len = month.length();
while (x < len)
{
a = a * 10 + int (month[x]) - 48;
x++;
}
return a;
}
int contointd()
{
int b = 0, x=0;
int len = day.length();
while (x < len)
{ b=b * 10 + int (year[x]) - 48;
x++;
}
return b;
}
int contointy()
{
int c = 0, x = 0;
int len = year.length();
while (x < len)
{
c = c * 10 + int ( year [x]) - 48;
x++ ;
}
return c;
}
bool checkmonth()
{
if (a == 0 || a > 12)
{
cout << "Month is Bad."<< endl;
return false;
}
return true;
}
bool checkday()
{
if (b == 0 || b > 31)
{
cout<<" Day is Bad." << endl;
return false;
}
if (a == 2 && b == 29)
{
if (c % 400 == 0)
return true;
else if (c % 100 == 0)
{
cout << " Day is not correct." << endl;
return false;
}
else if ( c % 4 == 0 )
return true;
else
{
cout << "The day is not correct." << endl;
return false;
}
}
if (a == 4 || a == 6 || a == 9 || a == 11)
{
if (b > 30)
{
cout << " Day is bad." << endl;
return false;
}
}
return true;
}
void printdate()
{
if (a == 1)
cout <<" January";
else if (a == 2)
cout << "February";
else if (a == 3)
cout << "March";
else if (a == 4)
cout << "April";
else if (a == 5)
cout << "May";
else if (a == 6)
cout << "June";
else if (a == 7)
cout << "July";
else if (a == 8)
cout << "August";
else if (a == 9)
cout << "September";
else if (a == 10)
cout << "October";
else if (a == 11)
cout << "November";
else if (a == 12)
cout << "December";
cout << b<<" , " << c<< endl;
}