202006XX-静态存储持续性\无连接性
//static.cpp-using a static local variable
#include <iostream>
using namespace std;
//constants 常量
const int ArSize=10;
//function prototype 函数原型
void strcount(const char * str);
int main()
{
char input[ArSize];
char next1;
cout<<"enter a line:\n";
cin.get(input,ArSize);
//cin.get和cin.getline的区别是:cin.get最后一位取的是换行符,所以只能取9位,但在dev中好像都一样,input是取的结果 ,而cin的值是16进制
while(cin)
{
cin.get(next1);
cout<<next1<<endl; //读取行输入后的字符
while(next1!='\n')
cin.get(next1);
strcount(input);
cout<<"Enter next line(empty lint to quit):\n";
cin.get(input,ArSize);
}
cout<<"bye\n";
return 0;
}
void strcount(const char * str)
{
static int total=0; //static local variable
int count=0; //automatic local variable
cout<<"\""<<str<<"\"contains"<<"\n";
while(*str++) //go to end of string
count++;
total+=count;
cout<<count<<"characters\n";
cout<<total<<"total characters\n";
}