在编程方面我相对较新且缺乏经验,所以如果我的代码让任何人眼睛流血,我深表歉意。
问题 #1:当我尝试使用 cin.getline() 时,我的代码无法正常工作,并且不断收到错误消息“找不到匹配 std::istream::getline(char[30], int, const char[ 2])”,但是当我尝试使用 getline(cin, varName) 时,它工作得很好。我在使用相同的参数之前使用过 cin.getline() 但我以前从未遇到过这个问题。
问题2:只是好奇。哪一个更好?就我而言,使用 cin.getline(...) 还是 getline(cin, ___) 更好?或者有什么比任何一个更好的东西吗?我尝试阅读差异:cin.getline() 和 getline(cin, st)但我不确定这是否是一个好的答案。
我将包括工作和不工作代码的两个副本。请不要介意这些名字,这些只是我被要求完成的更大任务的一部分。
不工作的代码:
#include<time.h>
#include<iostream>
#include<string>
using namespace std;
class ElectricityBill
{
private:
char accountName[30];
char address[50];
tm dueDate;
tm periodStartDate;
tm periodEndDate;
public:
ElectricityBill();
void print();
};
ElectricityBill::ElectricityBill()
{
int dd, mm, yyyy;
cout << "Input electricity bill data." << endl;
cout << "Account name: ";
cin.getline(accountName, 30, "\n");
cout << "Address: ";
cin.getline(address, 50, "\n");
cout << "Start date: ";
cin >> dd >> mm >> yyyy;
periodStartDate.tm_mday = dd;
periodStartDate.tm_mon = mm;
periodStartDate.tm_year = yyyy;
cout << "End date: ";
cin >> dd >> mm >> yyyy;
periodEndDate.tm_mday = dd;
periodEndDate.tm_mon = mm;
periodEndDate.tm_year = yyyy;
cout << "Due date: ";
cin >> dd >> mm >> yyyy;
dueDate.tm_mday = dd;
dueDate.tm_mon = mm;
dueDate.tm_year = yyyy;
//next = NULL;
}
void ElectricityBill::print()
{
cout << "Account Name: " << accountName << endl;
cout << "Address : " << address << endl;
cout << "Start Date : " << periodStartDate.tm_mday << "/" << periodStartDate.tm_mon << "/" << periodStartDate.tm_year << endl;
cout << "End Date : " << periodEndDate.tm_mday << "/" << periodEndDate.tm_mon << "/" << periodEndDate.tm_year << endl;
}
int main()
{
ElectricityBill a;
a.print();
return 0;
}
这是我使用 getline(cin, varName).. 时工作代码的副本。
#include<time.h>
#include<iostream>
#include<string>
using namespace std;
class ElectricityBill
{
private:
string accountName;
string address;
tm dueDate;
tm periodStartDate;
tm periodEndDate;
public:
ElectricityBill();
void print();
};
ElectricityBill::ElectricityBill()
{
int dd, mm, yyyy;
cout << "Input electricity bill data." << endl;
cout << "Account name: ";
getline(cin, accountName);
cout << "Address: ";
getline(cin, address);
cout << "Start date: ";
cin >> dd >> mm >> yyyy;
periodStartDate.tm_mday = dd;
periodStartDate.tm_mon = mm;
periodStartDate.tm_year = yyyy;
cout << "End date: ";
cin >> dd >> mm >> yyyy;
periodEndDate.tm_mday = dd;
periodEndDate.tm_mon = mm;
periodEndDate.tm_year = yyyy;
cout << "Due date: ";
cin >> dd >> mm >> yyyy;
dueDate.tm_mday = dd;
dueDate.tm_mon = mm;
dueDate.tm_year = yyyy;
//next = NULL;
}
void ElectricityBill::print()
{
cout << "Account Name: " << accountName << endl;
cout << "Address : " << address << endl;
cout << "Start Date : " << periodStartDate.tm_mday << "/" << periodStartDate.tm_mon << "/" << periodStartDate.tm_year << endl;
cout << "End Date : " << periodEndDate.tm_mday << "/" << periodEndDate.tm_mon << "/" << periodEndDate.tm_year << endl;
}
int main()
{
ElectricityBill a;
a.print();
return 0;
}
提前致谢!