C++ Primer Plus 第九章的一道题 不明白

ufdxkm 2009-02-12 12:11:02
// golf.h -- for pe9-1.cpp
#ifndef HEADER_H_
#define HEADER_H_
const int Len = 40;
struct golf
{
char fullname[Len];
int handicap;
};

// non-interactive version;
// function sets golf structure to provided name,handicap
// using values passed as arguments to the function
void setgolf (golf & g, const char * name, int hc);

// interactive version;
// function solicits name and handicap from user
// and sets the members of g to the values entered
// returns 1 if name is entered, 0 if name is empty string
int setgolf( golf &g);

// function resets handicap to new value
void handicap (golf & g,int hc);

// function displays contents of golf structure
void showgolf (const golf & g);

#endif

#include <iostream>
#include <cstring>
#include "golf.h"
//函数定义
void setgolf(golf &g,const char* name,int hc)
{
strcpy(g.fullname,name);
g.handicap=hc;
}
int setgolf(golf &g)
{
using namespace std;
cout<<"enter fullname: ";
cin.getline(g.fullname,len);
if(g.fullname[0]=='\0')
return 0;
cout<<"enter handicap: ";
while(!(cin>>g.handicap))
{
cin.clear();
while(cin.get()!='\n')
continue;
cout<<"enter handicap: ";
}
while(cin.get()!='\n') //我不明白cin,get()是什么意思。也没有参数。看了书上海市不明白。。
continue;
return 1;
}
void handicap(golf &g,int hc)
{
g.handicap=hc;
}
void showgolf(const golf &g)
{
std::cout<<g.fullname<<std::endl;
std::cout<<g.handicap<<std::endl;
}

/*============使用函数.............*/
#include <iostream>
#include "golf.h"

int main()
{
using namespace std;
cout<<"Enter the num of golfer: ";
int size;
cin>>size;
golf *user=new golf[size]; //创建指针

int i=0;
while(setgolf(user[i]) && i<size) //调用setgolf()
{
i++;
}
cout<<"cout golfer:\n";
for(i=0;i<size;i++)
showgolf(user[i]);
delete []user; //删除
return 0;
}







...全文
185 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
ufdxkm 2009-02-12
  • 打赏
  • 举报
回复
上面的
#ifndef HEADER_H_
#define HEADER_H_
#endif
这里错了
改为 GOLF_H_
然后编译运行
输出是错误的
我试着调试一下
好像这个调用
while(setgolf(user[i]) && i<size) //调用setgolf()
{
i++;
}
里的setgolf() 没有执行
ufdxkm 2009-02-12
  • 打赏
  • 举报
回复

// golf.h -- for pe9-1.cpp
#ifndef HEADER_H_
#define HEADER_H_
const int Len = 40;
struct golf
{
char fullname[Len];
int handicap;
};

// non-interactive version;
// function sets golf structure to provided name,handicap
// using values passed as arguments to the function
void setgolf (golf & g, const char * name, int hc);

// interactive version;
// function solicits name and handicap from user
// and sets the members of g to the values entered
// returns 1 if name is entered, 0 if name is empty string
int setgolf( golf &g);

// function resets handicap to new value
void handicap (golf & g,int hc);

// function displays contents of golf structure
void showgolf (const golf & g);

#endif
//------------------------------------------------------------
#include <iostream>
#include <cstring>
#include "golf.h"
//函数定义
void setgolf(golf &g,const char* name,int hc)
{
strcpy(g.fullname,name); //字符串复制到g.fullname
g.handicap=hc;
}
int setgolf(golf &g)
{
using namespace std;
cout<<"enter fullname: ";
cin.getline(g.fullname,len);
if(g.fullname[0]=='\0')
return 0;
cout<<"enter handicap: ";
while(!(cin>>g.handicap))
{
cin.clear();
while(cin.get()!='\n')
continue;
cout<<"enter handicap: ";
}
while(cin.get()!='\n') //我不明白cin,get()是什么意思。也没有参数。看了书上海市不明白。。
continue;
return 1;
}
void handicap(golf &g,int hc)
{
g.handicap=hc;
}
void showgolf(const golf &g)
{
std::cout<<g.fullname<<std::endl;
std::cout<<g.handicap<<std::endl;
}

/*-------------------------使用函数------------------------*/
#include <iostream>
#include "golf.h"

int main()
{
using namespace std;
cout<<"Enter the num of golfer: ";
int size;
cin>>size;
golf *user=new golf[size]; //创建指针

int i=0;
while(setgolf(user[i]) && i<size) //调用setgolf() 似乎这个调用有问题
{
i++;
}
cout<<"cout golfer:\n";
for(i=0;i<size;i++)
showgolf(user[i]);
delete []user; //删除
return 0;
}

waizqfor 2009-02-12
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 eidolondedidi 的回复:]
具体的是那个字符?cin.get() != '\n' 这条语句作用是什么?
[/Quote]
给你个get的用法 主要是判断输入的字符是不是'\n'

int get();
istream &get( char &ch );
istream &get( char *buffer, streamsize num );
istream &get( char *buffer, streamsize num, char delim );
istream &get( streambuf &buffer );
istream &get( streambuf &buffer, char delim );



get()函数被用于输入流,和以下这些:

读入一个字符并返回它的值,
读入一个字符并把它存储在ch,
读取字符到buffer直到num - 1个字符被读入, 或者碰到EOF或换行标志,
读取字符到buffer直到已读入num - 1 个字符,或者碰到EOF或delim(delim直到下一次不会被读取),
读取字符到buffer中,直到碰到换行或EOF,
或是读取字符到buffer中,直到碰到换行,EOF或delim。(相反, delim直到下一个get()不会被读取 ).
例如,下面的代码一个字符一个字符的显示文件temp.txt中的内容:

char ch;
ifstream fin( "temp.txt" );
while( fin.get(ch) )
cout << ch;
fin.close();
wzg112 2009-02-12
  • 打赏
  • 举报
回复
cin将 '\n' ' '等当作空格符略过了,不会放入你的变量里,它自动将输入转换成相应格式放入你的变量中
而cin.get()没有这些规则,只是从输入流中依次取出字符。 所以判断输入流字符中'\n',一般都用cin.get()
ufdxkm 2009-02-12
  • 打赏
  • 举报
回复
具体的是那个字符?cin.get() != '\n' 这条语句作用是什么?
lspol 2009-02-12
  • 打赏
  • 举报
回复
cin.get()是输入流的方法,就是从输入流获得一个字符
ufdxkm 2009-02-12
  • 打赏
  • 举报
回复
谢谢各位
自己解决了
golf *user=new golf[size-1]; // 指针创建
int i=0;
while(i<size)
{
setgolf(user[i]);
i++;
}
cout<<"cout golfer:\n";
for(i=0;i<size;i++)
showgolf(user[i]);

这里创建指针的时候 我错误的用成 char数组了 所以减了个1 ..
正确的应该这样
golf *user=new golf[size-1]; // 指针创建
int i=0;
while(i<size)
{
setgolf(user[i]);
i++;
}
cout<<"cout golfer:\n";
for(i=0;i<size;i++)
showgolf(user[i]);

ufdxkm 2009-02-12
  • 打赏
  • 举报
回复
麻烦帮我编译下这整个代码,还有一处错误我搞不清楚。就是最后的输出!
总共三个文件. 各位大哥帮我编译一下就知道是那里了,我找半天也不知道那里错了。 谢谢!


//user.cpp
#include <iostream>
#include "golf.h"

int main()
{
using namespace std;
cout<<"Enter the num of golfer: "; //输入要创建的结构数
int size;
while(!(cin>>size))
{
cin.clear();
while(cin.get() != '\n')
continue;
cout<<"enter erro enter again!";
}
cin.get();

golf *user=new golf[size-1]; // 指针创建
int i=0;
while(i<size)
{
setgolf(user[i]);
i++;
}
cout<<"cout golfer:\n";
for(i=0;i<size;i++)
showgolf(user[i]);

delete [] user; //删除
return 0;
}

————————————————————————————————————
//函数定义
//golf.h
#include <iostream>
#include <cstring>
#include "golf.h"

int setgolf(golf &g)
{
using namespace std;
cout<<"enter fullname: ";
cin.getline(g.fullname,len);
if(g.fullname[0]=='\0')
return 0;
cout<<"enter handicap: ";
while(!(cin>>g.handicap))
{
cin.clear();
while(cin.get()!='\n')
continue;
cout<<"Please enter handicap: ";
}
while(cin.get()!='\n')
continue;
return 1;
}
void showgolf(const golf &g)
{
std::cout<<"name: "<<g.fullname<<std::endl;
std::cout<<"handicap: "<<g.handicap<<std::endl;
}



__________________________________________________________________________

//golf.h 头文件

#ifndef GOLF_H_
#define GOLF_H_
const int len=40;
struct golf
{
char fullname[len];
int handicap;
};
int setgolf(golf &g);
void showgolf(const golf &g);
#endif




谢谢各位拉!
yuzl32 2009-02-12
  • 打赏
  • 举报
回复

while(!(cin>>g.handicap))
{ //输入发生错误,进入此代码块
cin.clear(); //清除错误标识
while(cin.get()!='\n') //清空流缓冲区
continue;
cout<<"enter handicap: ";//继续输入
}
while(cin.get()!='\n') //清空流缓冲区
continue;
bfhtian 2009-02-12
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 eidolondedidi 的回复:]
具体的是那个字符?cin.get() != '\n' 这条语句作用是什么?
[/Quote]
忽略输入的换号
ufdxkm 2009-02-12
  • 打赏
  • 举报
回复
顶。。
fangfeifensi 2009-02-12
  • 打赏
  • 举报
回复
清空输入流
while(cin.get()!='\n')
continue;

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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