Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么我的程序从字符串中读取连字符时会崩溃?_C++_Loops - Fatal编程技术网

C++ 为什么我的程序从字符串中读取连字符时会崩溃?

C++ 为什么我的程序从字符串中读取连字符时会崩溃?,c++,loops,C++,Loops,我得为我要上的课写一个程序。我必须用数字形式mm dd yyyy来表示出生日期,并将其转换为不同的格式月、日、年。我觉得我的代码是正确的,但每当我的do-while循环到达连字符时,它就会崩溃。它编译正确,并且VisualStudio在运行之前不会出现任何错误,所以我真的不知道出了什么问题。我已经包括了全部代码。此外,此分配还需要使用try/catch块。有人能告诉我为什么这个循环不喜欢检查连字符吗?提前谢谢 #include <iostream> #include <stri

我得为我要上的课写一个程序。我必须用数字形式mm dd yyyy来表示出生日期,并将其转换为不同的格式月、日、年。我觉得我的代码是正确的,但每当我的do-while循环到达连字符时,它就会崩溃。它编译正确,并且VisualStudio在运行之前不会出现任何错误,所以我真的不知道出了什么问题。我已经包括了全部代码。此外,此分配还需要使用try/catch块。有人能告诉我为什么这个循环不喜欢检查连字符吗?提前谢谢

#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;

}

您的条目字符串中没有换行符,因此x不断递增,并导致条目[x]的缓冲区溢出。您需要查找字符串的结尾:

 do {
        year *= 10;
        temp = get_digit(entry[x]);
        year += temp;
        x += 1;
    } while (entry[x] != '\0');

什么是崩溃错误?您是否遇到异常?是否调试并单步执行代码?如果是这样,您会在哪一行遇到问题?尝试取出您的Try catch或捕获异常以查看引发了什么异常。使用buildin方式不是更容易吗:我得到的错误是调试断言失败。错误来自文件c:\program files\microsoft visual studio 10.0\vc\include\xstring的第1441行。表达式:字符串下标超出范围。中止并忽略关闭程序。重试弹出相同的错误。我在代码中加入了断点,但我从中得到的最多的是,如果即将出现的字符是连字符,当它遇到第一个while语句时,它会弹出@Rvdk我从未听说过sscanf。这肯定会更容易,但我想知道为什么这也不起作用。谢谢大家。谢谢你们的意见,但这不是造成问题的循环。这是第一个循环,用于检查导致程序崩溃的月份。我还认为“\n”是一个换行符,我在while循环中检查了它。“\n”换行符是否有误?@ZeverMX您认为“\n”是换行符是正确的,但您的输入不是以换行符结尾,而是以\0结尾。