1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
// golf.h -- for pe-9.cpp
const int Len = 40;
struct golf
{
char fullname[Len];
int handicap;
};
//non-interactive version:
// function sets golf structure to provided name, handicap
// using vlues passed as arguments to the function
void setgolf(golf & g, const char * name, int hc);
// interactive version:
// function 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);
// chapEx9-1
#include <iostream>
#include "golf.h"
using namespace std;
int main()
{
golf ann;
setgolf(ann, "Ann Birdfree", 24);
showgolf(ann);
cout << "Enter how many golfers name and handicap do you want to enter: ";
static int ngolfers;
cin >> ngolfers;
while (cin.get() != '\n')
continue;
golf * ptr_golf = new golf[ngolfers];
//int entered = getinfo
int rint = 1;
while (rint == 1)
rint = setgolf(ann);
showgolf(ann);
}
// golf.cpp load and show golf struc
#include<iostream>
#include "golf.h"
using namespace std;
void setgolf(golf & g, const char * name, int hc)
{
strcpy_s(g.fullname, name);
g.handicap = hc;
// strlen(name)
}
int setgolf(golf & g)
{
static int count = 0;
static int pos = 0;
char fname[40];
cout << "Enter golfers name: ";
cin.getline(fname, 40, '\n');
int ihc = 0;
cout << "Enter golfers handicap: ";
cin >> ihc;
strcpy(g[count].fullname, fname);
g[count].handicap = ihc;
++count;
return 0;
}
void showgolf(const golf & g)
{
cout << "\nFull name: " << g.fullname << "\n";
cout << "Handicap: " << g.handicap << "\n";
}
|