»
 

Go Back   ResellerRatings Store Ratings > ResellerRatings Forums > Tech Support

Reply
 
LinkBack Thread Tools Display Modes
Old 07-13-2003, 12:07 AM   #1 (permalink)
Registered User
 
Join Date: Oct 2001
Location: UIUC
Posts: 1,144
Chiguy is on a distinguished road
Abnormal Program Termination in C++

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

Chiguy is offline   Reply With Quote
Old 07-31-2003, 01:24 PM   #2 (permalink)
Registered User
 
Join Date: Jul 2003
Posts: 2
raghuraman is on a distinguished road
PHP Code:
//-----------------------------------------------------------------------------------------
void GetDateComponents (string DateintMointDayintYr)
//sepearates the necessary information from the user input
{
string M_StrD_StrY_Str;

M_Str Date.substr(02);
D_Str Date.substr(22);//<<---- here is where it is failing
Y_Str Date.substr(44);//<<--- here is where it is failing

Mo StringToNum(M_Str);
Day StringToNum(D_Str);
Yr StringToNum(Y_Str);

when you have a string length less then 3 it fails at D_Str as it cant retreive in the string 2,2 and less 4 it fails at Y_Str as it cannot retreive 4,4..So you have to do error check to not allow less 4 according to your program.
Hope this helps
raghuraman is offline   Reply With Quote
Old 07-31-2003, 11:40 PM   #3 (permalink)
Registered User
 
Join Date: Oct 2001
Location: UIUC
Posts: 1,144
Chiguy is on a distinguished road
Someone pointed this out to me already but thanks alot anyways!
Chiguy is offline   Reply With Quote
Reply




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Most Active Discussions

Recent Discussions

All times are GMT -6. The time now is 05:10 AM.