【问题标题】:Why is my program crashing when it reads a hyphen from a string? [closed]为什么我的程序从字符串中读取连字符时会崩溃? [关闭]
【发布时间】:2015-02-05 15:47:59
【问题描述】:

我必须为我正在上课的课程编写一个程序。我必须以数字形式(mm-dd-yyyy)获取出生日期并将其转换为不同的格式(月日,年)。我觉得我的代码是正确的,但是每当我的 do while 循环到达连字符时,它就会崩溃。它编译正确,并且 Visual Studio 在运行之前不会弹出任何错误,所以我真的不知道出了什么问题。我已经包含了整个代码。此外,此分配需要使用 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;

}

【问题讨论】:

  • 什么是崩溃错误?你有例外吗?
  • 您是否调试单步执行您的代码?如果是这样,您在哪一行遇到问题?
  • 尝试取出你的try catch(或捕获异常)看看抛出了什么异常。
  • 使用内置方式是不是更方便:stackoverflow.com/questions/19482378/…
  • 我得到的错误是“调试断言失败”。错误来自文件 c:\program files\microsoft visual studio 10.0\vc\include\xstring 的第 1441 行。表达式:字符串下标超出范围。 Abort and Ignore 关闭程序。重试会弹出相同的错误。我在代码中设置了断点,但我从中得到的最大收获是,如果即将到来的字符是连字符,它会在遇到第一个 while 语句时弹出。 @Rvdk 我从未听说过 sscanf。那肯定会更容易,但我想知道为什么这也不起作用。谢谢大家。

标签: c++ loops


【解决方案1】:

您的输入字符串中没有换行符,因此 x 不断增加并导致 entry[x] 的缓冲区溢出。您需要寻找字符串的结尾:

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

【讨论】:

  • 感谢您的输入,但这不是目前导致问题的循环。这是检查程序崩溃月份的第一个循环。我也认为 '\n' 是一个换行符,我让它在一年的 while 循环中检查它。 '\n' 换行符我错了吗?
  • @ZeverMX 关于'\n' 是换行符,您是正确的,但是您的输入不以换行符结尾,而是以\0 结尾。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-27
  • 2014-06-15
  • 2012-04-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-17
相关资源
最近更新 更多