-5

I am having to write a program for a class I'm taking. I have to take a birth date in numerical form (mm-dd-yyyy) and put it into a different format (Month day, year). I feel like my code is correct, but whenever my do while loop gets to the hyphen, it crashes. It compiles correctly, and Visual Studio isn't popping up any error until it runs, so I really don't know what's wrong. I've included the entire code. Also, use of the try/catch block is required for this assignment. Can anyone tell me why this loop doesn't like to check for hyphens? Thanks in advance.

#include <iostream>
#include <string>

using namespace std;

int get_digit(char c) {return c - '0';}

int main() {

string entry = "";
short month = 0;
int day = 0;
int year = 0;
bool error = false;
int temp = 0;

try {

    cout << "Enter your birthdate in the form M-D-YYYY: ";
    cin >> entry;

    int x = 0;
    do {
        month *= 10;
        temp = get_digit(entry[x]);
        month += temp;
        x += 1;
    } while (entry[x] != '-');
    if (month > 12) {
        throw month;
    }
    x += 1;

    do {
        day *= 10;
        temp = get_digit(entry[x]);
        day += temp;
        x += 1;
    } while (entry[x] != '-'); 
    if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
        if (day > 31) {
            throw day;
        }
    } else if (month == 4 || month == 6 || month == 9 || month == 11) {
        if (day > 30) {
            throw day;
        }
    } else {
        if (day > 29) {
            throw day;
        }
    }
    x += 1;

    do {
        year *= 10;
        temp = get_digit(entry[x]);
        year += temp;
        x += 1;
    } while (entry[x] != '\n');
    if ((year % 4) != 0) {
        if (month == 2 && day > 28) {
            throw day;
        }
    }

    switch (month) {
    case 1:
        cout << "January ";
    case 2:
        cout << "February ";
    case 3:
        cout << "March ";
    case 4:
        cout << "April ";
    case 5:
        cout << "May ";
    case 6:
        cout << "June ";
    case 7:
        cout << "July ";
    case 8:
        cout << "August ";
    case 9:
        cout << "September ";
    case 10:
        cout << "October ";
    case 11:
        cout << "November ";
    case 12:
        cout << "December ";
    }

    cout << day << ", " << year << endl;

} catch (short) {

    cout << "Invalid Month!" << endl;

} catch (int) {

    cout << "Invalid Day!" << endl;

}

system("pause");
return 0;

}
ZeverMX
  • 23
  • 1
  • 2
  • 6
  • 2
    And what is the crash error? Do you get an exception? – RvdK Feb 05 '15 at 15:49
  • 3
    Did you **debug** and **step through** your code? If so, on which line do you encounter the problem? – Vinz Feb 05 '15 at 15:49
  • Try taking out your try catch (or catching the exception) to see what exception is thrown. – mcraenich Feb 05 '15 at 15:50
  • Isn't it easier to use a buildin way: http://stackoverflow.com/questions/19482378/how-to-parse-and-validate-a-date-in-stdstring-in-c – RvdK Feb 05 '15 at 15:50
  • The error I get is "Debug Assertion Failed". The error is from line 1441 of file c:\program files\microsoft visual studio 10.0\vc\include\xstring. Expression: string subscript out of range. Abort and Ignore shutdown the program. Retry pops the same error up. I put breakpoints in to step through the code, but the most I got from that is that it pops up when it hits the first while statement if the upcoming character is a hyphen. @Rvdk I had never heard of sscanf. That would definitely by easier, but I'd like to know why this isn't working also. Thanks guys. – ZeverMX Feb 05 '15 at 15:59

1 Answers1

1

There is no newline character in your entry string, so x keeps getting incremented and causes buffer overrun for entry[x]. You need to look for the end of the string instead:

 do {
        year *= 10;
        temp = get_digit(entry[x]);
        year += temp;
        x += 1;
    } while (entry[x] != '\0');
OldProgrammer
  • 12,050
  • 4
  • 24
  • 45
  • Thanks for the input, but that's not the loop causing the problem right now. It is the first loop that is checking the month that is crashing the program. Also i though '\n' was a newline character, and I have it checking for that in the year while loop. Am I wrong about the '\n' newline character? – ZeverMX Feb 05 '15 at 16:05
  • 1
    @ZeverMX You are correct about `'\n'` being newline, but your input doesn't end with a newline, it ends with `\0` instead. – Bart van Nierop Feb 05 '15 at 16:07