C + + Primer The Nineth chapter of post-curriculum programming

Source: Internet
Author: User

1.

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvz3v1z2xlmjaxma==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">
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;}
Execution Result:

2, change the program listing 9.8: replace the character array with a string object. This way, the program will no longer need to checkwhether the input string is too long, the same time the input string can be compared to the string "", than the inference isNo 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";}
Execution Result:

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvz3v1z2xlmjaxma==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">

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;}
Execution Result:

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvz3v1z2xlmjaxma==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">


Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

C + + Primer The Nineth chapter of post-curriculum programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.