私は学生で、2 つの異なる形式で日付を入力するコードを作成し、オーバーロードされたコンストラクターを使用してそれらを区別しようとしていますが、ヘッダー ファイルで宣言した変数が、コンストラクターの外で宣言されたままにならないため、スタックしています。
main(lab6)の前に私が経験したことのない問題
#include <cstdlib>
#include <iostream>
#include "date.h"
#include "test.h"
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
int month;
int year;
int day;
std::string monthstr;
int i=0;
while(i != 3)
{
std::cout << "Enter 1 for format: MM/DD/YYYY\nEnter 2 for format: Month DD, YYYY\nEnter 3 for exit\nChoice:";
std::cin >> i;
switch(i)
{
case 1:
{
std::cout << "enter month(1-12):";
std::cin >> month;
std::cout << "enter day of month:";
std::cin >> day;
std::cout << "enter year:";
std::cin >> year;
test test( month, day, year ) ;
test(month, day, year);
month = test.testdate();
break ;}
case 2:
{
std::cout << "enter month name:";
std::cin >> monthstr;
std::cout << "enter day of month:";
std::cin >> day;
std::cout << "enter year:";
std::cin >> year;
**here is the problem **
test test( monthstr, day, year ) ;
month = test.testdate();
break;}
}
// month = test.testdate();
date date(month, day, year);
date.datedis();
}
system("PAUSE");
return EXIT_SUCCESS;
}
test.ccptests id date is real*ここに問題があります* _ _ _ _ _ _ __ _ _ __ _ _ _ _ __ _ _ ___
#include <iostream>
#include <iomanip>
#include "test.h"
#include "date.h"
#include <string>
#include <stdexcept>
#include <locale>
using namespace std;
test::test(int m, int d, int y)
{
mon = m;
da = d;
ye = y;}
test::test(std::string m, int d, int y)
{
da = d;
ye = y ;
std::string months[] = { "january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december" };
int i = 0;
while (i != 12 || i !=13)
{i++;
std::locale loc;
if (months[i-1] == std::tolower(m,loc ) )
{mon = i;
i = 13;}
if (i!=13)
{throw invalid_argument("month is worng");}}}
int testdate()
{
if (mon < 0 && mon >= 12)
{throw invalid_argument( "month must be betwen 1-12");}
if (ye < 0 )
{throw invalid_argument( "year must be real");}
if (mon == 2 && da == 29 && (ye % 400 != 0 || (ye % 4 != 0 && ye % 100 == 0)))
{throw invalid_argument("not a leap year");}
int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if(da < 0 || da > days[mon-1])
{throw invalid_argument("day not correct");}
return mon;
// date date(mon, da, ye);}
test.hヘッダーファイル_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ ____ _
class test
#include <string>
//#include "date.h"
{
public:
test(int, int, int );
test(std::string, int, int );
int testdate();
private:
int mon;
int da;
int ye;
std::string mons;
};
date.ccp はここに日付*の問題を表示します* _ _ _ _ _ _ _ _ __ _ _ __ _ _ _ _ __ _ _ _
#include <iostream>
#include <iomanip>
#include "test.h"
#include "date.h"
#include <string>
#include <stdexcept>
using namespace std;
date::date(int m, int d, int y)
{ month = m;
daay = d;
year = y;}
**problem here**
void datedis()
{std::string months[] = { "january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december" };
std::cout << "MM/DD/YYYY: "<< month << "/" << daay << "/" << year <<"\nmonth DD,YYYY: "<< months[month-1] << " " << daay <<","<< year;
}
date_ _ _ __ _ _ __ _ _ __ _ _ __ _ _ __ _ __のdate.hヘッダー_ _
#include <string>
class date
{
public:
date(int, int, int );
void datedis();
private:
int month;
int daay;
int year;
};