![]() |
#2
fxbszj2012-12-20 20:21
|

#include <iostream>
#include <string>
void strcount(const string test);
int main(void)
{
using namespace std;
string str1;
cout << "enter a string (empty line to terminate input):" << endl;
getline(cin, str1)
while (str1 != "")
{
strcount(str1);
cout << "enter next line:" << endl;
getline(cin, str1);
}
cout << "done!";
return 0;
}
void strcount(const string test)
{
int count = 0;
static int total = 0;
for (int i = 0; i < test.size(); ++i)
{
++count;
}
cout << "current char is " << count << endl;
total += count;
cout << "total char is " << total << endl;
}
#include <string>
void strcount(const string test);
int main(void)
{
using namespace std;
string str1;
cout << "enter a string (empty line to terminate input):" << endl;
getline(cin, str1)
while (str1 != "")
{
strcount(str1);
cout << "enter next line:" << endl;
getline(cin, str1);
}
cout << "done!";
return 0;
}
void strcount(const string test)
{
int count = 0;
static int total = 0;
for (int i = 0; i < test.size(); ++i)
{
++count;
}
cout << "current char is " << count << endl;
total += count;
cout << "total char is " << total << endl;
}
我的意图是这样,输入一行,不管多少个字符,统计输入的字符个数,然后继续输入,统计第二次输入行的字符个数,并且统计前两次输入的总字符数。依次继续,直到输入一个空行,输入结束。只用string类完成,不用数组之类的!代码附上。麻烦来个人指教一下,谢谢了!初学C++感觉有点不适