// 编程练习 04.01_display message_2.cpp
#include <iostream> // a preprocessor directive
#include <string> // for enter the name
using namespace std;
struct message
{
char *FirstName;
char *LastName;
char Grade;
int age;
};
int main()
{
string temp;
cout << "What is your first name? ";
cin >> temp;
message.FirstName = new char [strlen(temp) + 1];
strcpy(message.FirstName, temp);
cout << "What is your last name? ";
cin >> temp;
message.LastName = new char [strlen(temp) + 1];
strcpy(message.LastName, temp);
cout << "What letter grade do you deserve? ";
cin.get();
cin.get(message.Grade);
cout << "What is your age? ";
cin >> message.age;
cout << "Name: " << message.*LastName << ", " << message.*FirstName;
cout << endl << "Grade: " << ((char)(message.Grade + 1)) << endl;
cin.get();
return 0;
}
// 现在请各位大侠帮帮忙,不知道应该怎么改才好。
VC++ 结构体中字符指针在main中使用new的赋值问题。
答案:1 悬赏:30 手机版
解决时间 2021-04-04 11:20
- 提问者网友:活着好累
- 2021-04-03 15:09
最佳答案
- 五星知识达人网友:青灯有味
- 2021-04-03 16:02
int main()
{
string temp;
cout << "What is your first name? ";
cin >> temp;
message msg;
msg.FirstName = new char [strlen(temp.c_str()) + 1];
strcpy(msg.FirstName, temp.c_str());
cout << "What is your last name? ";
cin >> temp;
msg.LastName = new char [strlen(temp.c_str()) + 1];
strcpy(msg.LastName, temp.c_str());
cout << "What letter grade do you deserve? ";
cin.get();
cin.get(msg.Grade);
cout << "What is your age? ";
cin >> msg.age;
cout << "Name: " << msg.LastName << ", " << msg.FirstName;
cout << endl << "Grade: " << ((char)(msg.Grade + 1)) << endl;
cin.get();
return 0;
}
{
string temp;
cout << "What is your first name? ";
cin >> temp;
message msg;
msg.FirstName = new char [strlen(temp.c_str()) + 1];
strcpy(msg.FirstName, temp.c_str());
cout << "What is your last name? ";
cin >> temp;
msg.LastName = new char [strlen(temp.c_str()) + 1];
strcpy(msg.LastName, temp.c_str());
cout << "What letter grade do you deserve? ";
cin.get();
cin.get(msg.Grade);
cout << "What is your age? ";
cin >> msg.age;
cout << "Name: " << msg.LastName << ", " << msg.FirstName;
cout << endl << "Grade: " << ((char)(msg.Grade + 1)) << endl;
cin.get();
return 0;
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯