#include <iostream>
#include <cstring>
using namespace std;
class WeiwenzhaoTime
{
public:
void setDate(string);
string getDate();
void setTime(string);
string getTime();
private:
string date;
string time;
};
void WeiwenzhaoTime::setDate(string inDate)
{
date = inDate;
}
string WeiwenzhaoTime::getDate()
{
return date;
}
void WeiwenzhaoTime::setTime(string inTime)
{
time = inTime;
}
string WeiwenzhaoTime::getTime()
{
return time;
}
int main()
{
string time;
string date;
WeiwenzhaoTime nowTime;
cout<<"Please set date(YYYY-MM-DD):";
cin>>date;
nowTime.setDate(date);
cout<<"Please set time(hh:mm:ss): ";
cin<<time;
nowTime.setTime(time);
cout<<"Now the time is : "<<nowTime.getDate()
<<" "<<nowTime.getTime();
return 0;
}
为什么在DEV里可以运行 而在VC++6.0里不能运行呢 要想在VC++6.0里运行要怎么改呀
帮你改过来了
看看可不可以
错误的地方已经注释了
#include <iostream>
#include <string>//用string库
using namespace std;
class WeiwenzhaoTime
{
public:
void setDate(string);
string getDate();
void setTime(string);
string getTime();
private:
string date;
string time;
};
void WeiwenzhaoTime::setDate(string inDate)
{
date = inDate;
}
string WeiwenzhaoTime::getDate()
{
return date;
}
void WeiwenzhaoTime::setTime(string inTime)
{
time = inTime;
}
string WeiwenzhaoTime::getTime()
{
return time;
}
int main()
{
string time;
string date;
WeiwenzhaoTime nowTime;
cout<<"Please set date(YYYY-MM-DD):";
cin>>date;
nowTime.setDate(date);
cout<<"Please set time(hh:mm:ss): ";
cin>>time;//这的符号错了
nowTime.setTime(time);
cout<<"Now the time is : "<<nowTime.getDate()
<<" "<<nowTime.getTime();
return 0;
}
cin>>date; 换成 getline(cin,date);
cin<<time; 换成 getline(cin,time);
主要的问题在于CString是m$在标准C++出来前自家搞出来的一个东东,他只能和MFC里自家的CArchive配对,和cin是不可调和的. 所以cin>>date 就不行.
将头文件#include <cstring> 换成#include <string> 就没问题.
另外一个小问题就是
cout<<"Please set time(hh:mm:ss): ";
cin>>time; //输入格式错误. >>