1.
Code: Header File Golf.h code:
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 s Howgolf (const golf & G);
Golf.cpp Code
#include <iostream> #include <cstring> #include "golf.h" using namespace Std;void Setgolf (Golf & G, const char * name, int hc) {strcpy (g.fullname, name); G.handicap = HC;} int Setgolf (Golf & G) {cout<< "Please enter full name:"; Cin.getline (G.fullname, Len); if (g.fullname[0] = = ' + ') return 0; cout << "Enter Handicap value:"; while (!) ( CIN >> G.handicap)) {cin.clear (); cout << "Please enter an integer:"; } while (Cin.get ()! = ' \ n ') continue; return 1;} void Handicap (Golf & G, int hc) {g.handicap = HC;} void Showgolf (const golf & G) {cout << "golfer:" << g.fullname << "\ n"; cout << "Handicap:" << g.handicap << "\ n";} const INT Mems = 5;int Main () {golf Team[mems]; cout << "Input" << Mems << "team member: \ n"; int i; for (i=0; i<mems; i++) if (Setgolf (team[i]) = = 0) break; for (int j=0; j<i; j + +) Showgolf (Team[j]); Setgolf (Team[0], "Fred Norman", 5); Showgolf (Team[0]); Handicap (Team[0], 4); Showgolf (Team[0]); return 0;}
Operation Result:
2. Modify the program listing 9.8: replace the character array with a string object. This way, the program will no longer need to checkthe string entered is too long, and the input string can be compared with the string "", rather than theNo blank lineCode:
#include <iostream> #include <string>using namespace std;void strcount (const string &str); int main () { string input; cout << "Enter a line:\n"; Getline (cin, input); while (""! = input) { strcount (input); cout << "Enter next line:"; Getline (cin, input); } cout << "bey!\n";} void Strcount (const string &str) { cout << "\" "<< str <<" \ ":"; int count = Str.length (); cout << Count << " characters\n";}
Operation Result:
3.
Code:
#include <iostream> #include <cstring>struct chaff{ char dross[20]; int slag;}; int main () { using Std::cout; Using Std::endl; Chaff *p1; int i; P1 = new Chaff[2]; std::strcpy (P1[0].dross, "Tiffany Zhao"); p1[0].slag=13; std::strcpy (P1[1].dross, "Guugle"); p1[1].slag=-23; for (i=0; i<2; i++) cout << p1[i].dross << ":" << p1[i].slag <<endl; delete [] p1; return 0;}
Operation Result:
C + + Primer nineth post-lesson programming questions