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

本文提供了C++ Primer Plus第六版第九章的课后习题解答,包括9.1至9.4部分。详细涵盖了9_1.cpp、golf.h、golf.cpp等文件的编程内容,并附带了编译指令。
1万+

被折叠的 条评论
为什么被折叠?



