1. 程式人生 > >C++知識點備忘錄之多檔案程式編寫(六)

C++知識點備忘錄之多檔案程式編寫(六)

使用標頭檔案來定義使用者型別,為操作使用者型別的函式提供函式原型;並將函式定義放在一個獨立的原始碼檔案中。標頭檔案和原始碼檔案一起定義和實現了使用者定義的型別以及使用方式。最後,將main()和其他使用這些函式的函式放在第三個檔案中。

#include<iostream>
//const int ArSize=10;
//void strcount(const char * str);
#include<string>
using namespace std;
void strcount(const string & str);

int main()
{
    /*char input[ArSize];
    char next;*/

    string input;
    cout<<"Enter a line: \n";
    /*cin.get(input,ArSize);
    while(cin)
    {
        cin.get(next);
        while(next !='\n')
            cin.get(next);
        strcount(input);
        cout<<"Enter next line(empty line to quit): \n";
        cin.get(input,ArSize);
    }*/
    getline(cin,input);
    while (input!= "")
    {
        strcount(input);
        cout << "Enter next line:";
        getline(cin, input);
    }
    cout<<"Bye\n";
    return 0;
}

void strcount(const string &str)
{
    cout << "\"" << str << "\": ";
    int count = str.length();
    cout << count << "  characters\n";
}

/*
void strcount(const char *str)
{
    using namespace std;
    static int total=0;
    int count=0;

    cout<<"\""<<str<<"\"contains ";
    while(*str++)
        count++;
    total+=count;
    cout<<count<<" characters\n";
    cout<<total<<" characters total \n";
}*/