这是用户可以输入段落(1000、500 或 300 个字符)的解决方案。
代码:
#include <iostream>
using namespace std;
int main()
{
char ch;
int count = 0;
int maxCharacters=0;
char words[1024]={' '};
cout<<"Enter maxCharacters 300,500,1000 >";
cin>>maxCharacters;
cout << "\nProceed to write chars, # to quit: \n";
cin.get(ch);
while( (ch != '#') )
{
cin.get(ch); // read next char on line
++count; // increment count
words[count]=ch;
cout <<words[count]; // print input
if (count>= maxCharacters) break;
}
cout << "\n\n---------------------------------\n";
cout << endl << count << " characters read\n";
cout << "\n---------------------------------\n";
for(int i=0;i<count;i++) cout <<words[i];
cout << "\n"<< count << " characters \n";
cout<<" \nPress any key to continue\n";
cin.ignore();
cin.get();
return 0;
}
输出:
Enter maxCharacters 300,500,1000 >10
Proceed to write chars, # to quit:
The pearl is in the river
The pearl
---------------------------------
10 characters read
---------------------------------
The pearl
10 characters
Press any key to continue