本帖最後由 鄭繼威 於 2022-6-18 11:52 編輯
類函式的建立與執行 (四)
只是四是可以讀整行的(getline),這個單純就讀一個單字- cin>>str; //(讀到空格就斷了) //ex:只能讀單字
- getline(cin,str); //(讀整行) //ex:可以讀單字、句子
複製代碼- #include<iostream>
- #include<cstdlib>
- using namespace std;
- void count(int n,string s)
- {
- for(int i=1;i<=n;i++){
- cout<<"第"<<i<<"次的"<<s<<endl;
- }
- }
- void print(string s){
- int n;
- cout<<"你要幾次?";
- cin>>n;
- count(n,s);
- }
- int main()
- {
- string str;
- cout<<"你要印什麼?";
- cin>>str;
- print(str);
- system("pause");
- return 0;
- }
複製代碼 |