C++风格:
#include
#include
#include
//输入任意行,统计行数和总字符个数
using namespace std;
int main(void){
int count = 0;
int lenth = 0;
string line;
cout << "请输入任意行信息:" << endl;
while(1){
if(getline(cin,line) == NULL)//C++中读取一行,遇到文件结束符,同样是返回0,即NULL。
break;
count++;
lenth += line.length ();
}
cout << "你共输入了:" << count <<"行" << endl;
cout << "共计:" << lenth <<"个字符(包括空白字符)" << endl;
system("pause");
return 0;
}
C语言风格:(在前基础上稍作更改)
```cpp
#include
#include
#include
//输入任意行,统计行数和总字符个数
//using namespace std;
int main(void){
int count = 0;
int lenth = 0;
char line[128] = {0};
printf("请输入任意行信息:\n");
while(1){
if(fgets(line,sizeof(line),stdin) == NULL)//C++中读取一行,遇到文件结束符,同样是返回0,即NULL。//get(line)效果也一样
break;
count++;
lenth += strlen(line);
}
printf("你输入了:%d行\n",count);
printf("共计%d个字符(包括空白字符)\n",lenth);
system("pause");
return 0;
}
总结:
1. c++读取一行用getline(), C读取一行用gets,返回值是什么忘了就查手册。