注册 登录
编程论坛 C++教室

菜鸟问题,关于string类的一个小程序

迷途的菜鸟 发布于 2012-12-20 16:46, 536 次点击
程序代码:
#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;

}

我的意图是这样,输入一行,不管多少个字符,统计输入的字符个数,然后继续输入,统计第二次输入行的字符个数,并且统计前两次输入的总字符数。依次继续,直到输入一个空行,输入结束。只用string类完成,不用数组之类的!代码附上。麻烦来个人指教一下,谢谢了!初学C++感觉有点不适
5 回复
#2
fxbszj2012-12-20 20:21
程序代码:
#include <iostream>
#include <string>
using namespace std;  //注意位置

void strcount(const string test);
int main(void)
{
   
    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;

}

好像这样就可以了,你试试
#3
w5277050902012-12-20 20:41
楼上正解!

#4
迷途的菜鸟2012-12-20 21:23
以下是引用fxbszj在2012-12-20 20:21:58的发言:

#include <iostream>
#include <string>
using namespace std;  //注意位置

void strcount(const string test);
int main(void)
{
   
    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 <iostream>
#include <string>

void strcount(const string test);
int main(void)
{
   
    string str1;
    using namespace std;
    cout << "enter a string (empty line to terminate input):" << endl;
    getline(cin, str1).get();
    while (str1 != "")
    {
        strcount(str1);
        cout << "enter next line:" << endl;
        getline(cin, str1).get();
        
    }
    cout << "done!";
   
    return 0;
   
}

void strcount(const string test)
{
    using namespace std;
    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;
   
}

你好,我using namespace在main()和被调函数里都用一个,为什么会报错呢?
#5
fxbszj2012-12-21 10:31
回复 4楼 迷途的菜鸟
我看了下,应该是你看的这本书喜欢把using namespace std;这句放在main里面的吧,你把这句放在声明的位置,并养成习惯好了

[ 本帖最后由 fxbszj 于 2012-12-22 11:36 编辑 ]
#6
低调的哥额2012-12-27 15:05
str.size();
1