分別用char數組和string類型編寫代碼,使他們輸出相同。
#include<iostream>
#include<string>
int main()
{
using namespace std;
string name;
string dessert;
cout << "Enter your name:\n";
getline(cin,name);
cout << "Enter your favorite desseert:\n";
getline(cin,dessert);
cout << "I have some delicious " << dessert;
cout << " for you," << name << ".\n";
return 0;
}
#include<iostream>
#include<string>
int main()
{
using namespace std;
const int AirSize = 20;
char name[AirSize];
char dessert[AirSize];
cout << "Enter your name:\n";
cin.getline(name,AirSize);
cout << "Enter your favorite desseert:\n";
cin.getline(dessert,AirSize);
cout << "I have some delicious " << dessert;
cout << " for you," << name << ".\n";
return 0;
}
哪裡不對,麻煩指出.謝謝
#include<iostream>
#include<string>
int main()
{
using namespace std;
string name;
name.resize(20);
string dessert;
dessert.resize(20);
cout << "Enter your name:\n";
cin.getline((char *)name.c_str(), 20);
cout << "Enter your favorite desseert:\n";
cin.getline((char *)dessert.c_str(), 20);
cout << "I have some delicious " << dessert;
cout << " for you," << name << ".\n";
return 0;
}