9.1
9_1.cpp文件:
#include "golf.h"
int main()
{
golf golfs[3];
for(int i=0; i<3; ++i) {
if (setgolf(golfs[i]))
showgolf(golfs[i]);
else
break;
}
}
golf.h:
const int Len = 40;
struct golf {
char fullname[Len];
int handicap;
};
void setgolf (golf& g, const char * name, int hc);
int setgolf (golf& g);
void handicap(golf& g, int hc);
void showgolf(const golf &g);
golf.cpp:
#include <iostream>
#include <string>
#include <cstring>
#include "golf.h"
extern const int Len;
using std::cin;
using std::cout;
int setgolf (golf& g) {
cout << "Please enter the name of golf:\n";
char input[Len];
char next;
if (cin.get(input, Len)) {
cin.get(next);
while (next != '\n')
cin.get(next);
if (strlen(input) > 0)
strcpy(g.fullname, input);
else
return 0;
} else {
return 0;
}
int hc = 0, count=0;
cout << "Please enter the handicap of golf: \n";
cin >> hc;
while (hc < 0 && count<3) {
cout << "\nInvalid handicap number(>0)!\nTry again:";
cin >> hc;
count++;
}
cin.get(next);
if (hc > 0) {
g.handicap &#