gpt4 book ai didi

c++ - 调试错误,在c++中调用了abort()

转载 作者:行者123 更新时间:2023-11-30 04:11:21 27 4
gpt4 key购买 nike

我正在尝试对日期的初始化值执行错误检查,以便超出可接受范围的一天或一个月将停止程序调试,但即使在可接受的范围内我也会收到调试错误。我不知道错误来自哪里,所以我发布了代码。

// ConsoleApplication56.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include<stdexcept>
#include<array>

using namespace std;

class Date
{
public:
explicit Date(int mo, int da, int ye)
{
setDate( mo, da, ye);
}
void setDate(int &m, int &d, int &y)
{

setMonth(m);
setDay(d);
setYear(y);

}
void setMonth(int &m)
{
if (month>0 && month <13)
{
month = m;
}else
throw invalid_argument("month must be 1-12");
}
unsigned int getMonth()const
{
return month;
}
void setDay(int &d)
{
if(day>0 && day<32)
{
day = d;
}else
throw invalid_argument("day must be 0-31");
}
unsigned int getDay()const
{
return day;
}
void setYear(int &y)
{
year = y;

}
unsigned int getYear()const
{
return year;
}
void print()
{
cout<< month <<'/' << day << '/' << year;
}
void nextDay()
{
int numberOfDaysToAdd = 1;
array <int,12> daysInAMonth = {31,28,31,30,31,30,31,31,30,31,30,31};

cout << "the date before is: " << month << "/" << day << "/" << year << endl;

day += numberOfDaysToAdd;

while (day > daysInAMonth [month - 1 ] )
{
day-= daysInAMonth [month - 1 ];
month++;

if (month > 12){
month = 1;
year++;
}

}

cout << "the day after is: " << month << "/" << day << "/" << year << endl;

}

private:
unsigned int month;
unsigned int day;
unsigned int year;
};


int main()
{
char response = 'y';
Date date(12, 3, 2013 );

cout<<"The date is :";
date.print();
cout<<endl;
cout<<endl;

cout<<"do you wish to check the next date(y/n)? :";
cin >> response;
cout<<endl;

while(response == 'y')
{
date.nextDay();
cout<<endl;
cout<<"do you wish to check the next date(y/n)? : ";
cin >> response;
cout<<endl;
}
return 0;

最佳答案

您应该检查 md 值,而不是 monthday 成员变量。

void setMonth(int &m)
{
if (month>0 && month <13)

应该是

void setMonth(int &m)
{
if (m>0 && m <13)

关于c++ - 调试错误,在c++中调用了abort(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20275867/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com