With this code, I get a Abnormal Program Ternination Error when the input is less than 4 characters and I can't figure out why.
/*Program File : Assignment 6.cpp
Program #6 CSC140-001 07/07/2003
This program takes a user inputted date in the form of MDDYYYY or MMDDYYYY and outputs
this in the form of month-name blank day-of-month comma blank year. Error checking and
reporting is implemented also.*/
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
//-----------------------------------------------------------------------------------------
string ConvertToUpper(string Date)
//converts user input to uppercase
{
string TempStr;
char TempChar;
int n = 0;
while (n < Date.length())
{
TempChar = toupper(Date[n]);
TempStr = TempStr + TempChar;
n++;
}
return TempStr;
}
//-----------------------------------------------------------------------------------------
void GetDate (string &Date)
//prompts and gets input from the user
//if the input is a length of 7, a leading zero is added to assist in later tests
{
cout << "Enter the Date in MDDYYYY or MMDDYYYY form (STOP to exit): ";
cin >> Date;
if (Date.length() == 7)
Date = "0" + Date;
Date = ConvertToUpper(Date);
}
//-----------------------------------------------------------------------------------------
bool CheckLength (string Date)
//checks the length of user input for a length of 8
{
if (Date.length() == 8)
return true;
else
return false;
}
//-----------------------------------------------------------------------------------------
bool CheckDigits (string Date)
//checks the user input for all digits
{
int n = 0;
bool test = true;
while (test && n < Date.length())
{
if (isdigit(Date[n]) != 0)
test = true;
else
test = false;
n++;
}
return test;
}
//-----------------------------------------------------------------------------------------
int StringToNum (string Temp)
//converts a string to a number
{
int Num = 0, n = 0;
while (n < Temp.length())
{
Num = Num * 10 + Temp[n] - 48;
n++;
}
return Num;
}
//-----------------------------------------------------------------------------------------
void GetDateComponents (string Date, int& Mo, int& Day, int& Yr)
//sepearates the necessary information from the user input
{
string M_Str, D_Str, Y_Str;
M_Str = Date.substr(0, 2);
D_Str = Date.substr(2, 2);
Y_Str = Date.substr(4, 4);
Mo = StringToNum(M_Str);
Day = StringToNum(D_Str);
Yr = StringToNum(Y_Str);
}
//-----------------------------------------------------------------------------------------
bool CheckMonth (int Mo)
//checks the user inputted month
{
if (Mo < 1 || Mo > 12)
return false;
else
return true;
}
//-----------------------------------------------------------------------------------------
bool LeapYearCheck (int year)
//checks for a leap year
{
if (year % 400 == 0)
return true;
else if (year % 100 == 0)
return false;
else if (year % 4 == 0)
return true;
else
return false;
}
//-----------------------------------------------------------------------------------------
bool CheckDay (int Mo, int Day, int Yr)
//checks the user inputted day
{
int DayLimit;
if (Mo == 4 || Mo ==6 || Mo == 9 || Mo == 11)
DayLimit = 30;
else if (Mo == 2 && LeapYearCheck(Yr))
DayLimit = 29;
else if (Mo == 2)
DayLimit = 28;
else
DayLimit = 31;
if (Day < 1 || Day > DayLimit)
return false;
else
return true;
}
//-----------------------------------------------------------------------------------------
void CheckDate (string Date, int Mo, int Day, int Yr, int& Error_Code)
//checks the user inputted date
{
if (!CheckLength(Date))
Error_Code = 1;
else if (!CheckDigits(Date))
Error_Code = 2;
else if (!CheckMonth(Mo))
Error_Code = 3;
else if (!CheckDay(Mo, Day, Yr))
Error_Code = 4;
else
Error_Code = 0;
}
//-----------------------------------------------------------------------------------------
void PrintDate (int Mo, int Day, int Yr, int Error_Code)
/*outputs the date in the form of month-name blank day-of-month comma blank year based on
the user's input or an error message if appropriate*/
{
switch (Error_Code)
{
case 0 : switch (Mo)
{
case 1 : cout << "January";
break;
case 2 : cout << "February";
break;
case 3 : cout << "March";
break;
case 4 : cout << "April";
break;
case 5 : cout << "May";
break;
case 6 : cout << "June";
break;
case 7 : cout << "July";
break;
case 8 : cout << "August";
break;
case 9 : cout << "September";
break;
case 10 : cout << "October";
break;
case 11 : cout << "November";
break;
case 12 : cout << "December";
break;
}
cout << " " << Day << ", ";
cout << Yr << "\n\n";
break;
case 1 : cout << "Incorrect length entered.\n\n";
break;
case 2 : cout << "Not all digits entered.\n\n";
break;
case 3 : cout << "Month is incorrect.\n\n";
break;
case 4 : cout << "Day is incorrect.\n\n";
break;
}
}
//-----------------------------------------------------------------------------------------
int main ()
{
string Date;
int Month, Day, Year, ErrorCode;
GetDate(Date);
while (Date != "STOP")
{
GetDateComponents(Date, Month, Day, Year);
CheckDate(Date, Month, Day, Year, ErrorCode);
PrintDate(Month, Day, Year, ErrorCode);
GetDate(Date);
}
return 0;
}
Thanks