new struct 求大神

q7822819 2012-10-18 10:15:28
#include "stdafx.h"
#include <iostream>
struct test
{
char name[20];
double num;
};

int _tmain(int argc, _TCHAR* argv[])
{ using namespace std;
test *ps=new test[2];
cin.get(ps[0].name,20);
cin.get();
cin>>ps[0].num;
cin.get();
cin.get(ps[1].name,20);
cin.get();
cin>>ps[1].num;
cin.get();
cout << ps[0].name<<endl;
cout << ps[0].num <<endl;
cout << ps[1].name <<endl;
cout<<ps[1].num <<endl;
delete []ps;
return 0;
}
delete []ps是正确的吧
为什么我用cin.get(ps.name,20);不能用->符还是用.看书看糊涂了
...全文
87 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
q7822819 2012-10-18
  • 打赏
  • 举报
回复
编译的时候 (ps+1)->name是错误的 (ps+1).name是正确的
fujialin2011 2012-10-18
  • 打赏
  • 举报
回复
上面ps[0].name可以改为(ps+1)->name错了,应该是ps[1].name可以改为(ps+1)->name
fujialin2011 2012-10-18
  • 打赏
  • 举报
回复
delete []ps;是正确的;对于cin.get(ps.name,20);不能用->是由于cin不是指针类型
ps[0].name可以改为(ps+0)->name
ps[0].name可以改为(ps+1)->name

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧