65,211
社区成员
发帖
与我相关
我的任务
分享// 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;
}
// 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;
}
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();
//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
while(!(cin>>g.handicap))
{ //输入发生错误,进入此代码块
cin.clear(); //清除错误标识
while(cin.get()!='\n') //清空流缓冲区
continue;
cout<<"enter handicap: ";//继续输入
}
while(cin.get()!='\n') //清空流缓冲区
continue;