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

求大神说明该如何修改这个错误~~~~~

Hee麦 发布于 2012-12-23 13:17, 669 次点击
我写的这个string Upper(string & s);函数是为了:将string对象的内容装换为大写,但是不知道该如何一个个地读取string字符串里的字符
这个函数要求接受一个指向string对象的应用做为参数;


#include <iostream>
#include <string>
#include <cctype>
using namespace std;

string Upper(string & s);

int main()
{
    cout<<"Enter a string (q to quit): ";
    string str;
    getline(cin,str);
    Upper(str);
    while(str!="q")
    {
        cout<<"Next string (q to quit): ";
        getline(cin,str);
        Upper(str);
    }
    cout<<"Bye.\n";
    return 0;
}

string Upper(string & s)
{
    while(*s)
    {
        toupper(*s);
        s++;
    }
    return s;
}
12 回复
#2
yuccn2012-12-23 16:36
string Upper(string &s)
{
    char *pStr = (char *)s.c_str();
    while(*pStr)
    {
       *pStr = toupper(*pStr);
        pStr++;
    }
    return s;
}

这样就行了,注意红色的部分
#3
yuccn2012-12-23 16:40
s 是string对象来的 不是char  所以就s.c_str();获取其char buff指针。
获取出来的是const类型的,也就是不可以改变其内容。强行转换(就好比 不让拆?强行拆迁~~~),就可以改写他的内容了
#4
Hee麦2012-12-24 10:18
谢谢!
#5
Hee麦2012-12-24 10:48
回复 3楼 yuccn
你好,感谢你的解答,但是我还没解决这个问题,这个代码是我做C++ Primer Plus(第五版)第八章编程题第3题的答案,不过我按照你的修改后没能按题目要求正确运行。

原题目:编写一个函数,他接受一个指向string对象的引用作为参数,并将该string对象的内容转换为大写,为此可使用函数toupper()。然后编写一个程序,它通过使用一个循环让你能够用不同的输入来测试这个函数,改程序的运行情况如下:
Enter a string (q to quit): go away
GO AWAY
Next string (q to quit): good grief!
GOOD GRIEF!
Next string (q to quit):q
Bye.

而我现在写的代码是:
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
string Upper(string & s);
int main()
{
    cout<<"Enter a string (q to quit): ";
    string str;
    getline(cin,str);
    Upper(str);
    while(str!="q")
    {
        cout<<"Next string (q to quit): ";
        getline(cin,str);
        Upper(str);
    }
    cout<<"Bye.\n";
    return 0;
}

string Upper(string &s)
{
    char *pStr = (char *)s.c_str();
    while(*pStr)
    {
       *pStr = toupper(*pStr);
        pStr++;
    }
    return s;
}

结果运行后,功能没实现,我不知道错在哪里了。如果你有空的话,请帮我看一下 指出错误的地方或该怎么修改,谢谢!
#6
mmmmmmmmmmmm2012-12-24 13:04
帮你修改了一下 供参考
程序代码:
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
string Upper(string & s);
int main()
{
    string str;
    cout<<"Enter a string (q to quit): ";
    while (1)
    {
        getline(cin,str);
        if (str == "q")
        {
            break;
        }
        cout<<Upper(str)<<endl;
        cout<<"Next string (q to quit): ";
    }
    cout<<"Bye.\n";
    return 0;
}

string Upper(string &s)
{
    string::size_type i =0;
    for (; i<s.size(); ++i)
    {
        s[i] = toupper(s[i]);
    }

    return s;
}
#7
mmmmmmmmmmmm2012-12-24 13:16
楼主 你的程序少了输出 所以看不见 可以加上cout<<Upper(str)<<endl;

但是你的while循环有问题,q是退出不了的  你可以参考下上面的

以下是引用Hee麦在2012-12-24 10:48:21的发言:

你好,感谢你的解答,但是我还没解决这个问题,这个代码是我做C++ Primer Plus(第五版)第八章编程题第3题的答案,不过我按照你的修改后没能按题目要求正确运行。

原题目:编写一个函数,他接受一个指向string对象的引用作为参数,并将该string对象的内容转换为大写,为此可使用函数toupper()。然后编写一个程序,它通过使用一个循环让你能够用不同的输入来测试这个函数,改程序的运行情况如下:
Enter a string (q to quit): go away
GO AWAY
Next string (q to quit): good grief!
GOOD GRIEF!
Next string (q to quit):q
Bye.

而我现在写的代码是:
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
string Upper(string & s);
int main()
{
    cout<<"Enter a string (q to quit): ";
    string str;
    getline(cin,str);
    Upper(str);
    while(str!="q")
    {
        cout<<"Next string (q to quit): ";
        getline(cin,str);
        Upper(str);
    }
    cout<<"Bye.\n";
    return 0;
}

string Upper(string &s)
{
    char *pStr = (char *)s.c_str();
    while(*pStr)
    {
       *pStr = toupper(*pStr);
        pStr++;
    }
    return s;
}

结果运行后,功能没实现,我不知道错在哪里了。如果你有空的话,请帮我看一下 指出错误的地方或该怎么修改,谢谢!
#8
Hee麦2012-12-24 18:42
回复 7楼 mmmmmmmmmmmm
感谢,我把你给我的代码运行之后,出现了新问题是:输入字符串后要按两次才显示转换后的大写,我认为是getline()函数保留回车符,下一次读取会先读取回车符,我就在循环里加了一句cin.get();不过还是没能解决。

while (1)
    {
        getline(cin,str);
        cin.get();
        if (str == "q")
        {
            break;
        }
        cout<<Upper(str)<<endl;
        cout<<"Next string (q to quit): ";
    }
    cout<<"Bye.\n";
    return 0;
}

只有本站会员才能查看附件,请 登录
#9
mmmmmmmmmmmm2012-12-26 09:09
这个是VC6 的BUG  在后面的版本已经修复了
#10
Hee麦2012-12-26 14:09
好的,谢谢
#11
低调的哥额2012-12-27 14:30
噢噢噢
#12
guoyao3332012-12-31 01:27
作为一个小渣渣。。你看这样行不。。
程序代码:

#include <iostream>
#include <string>
#include <cctype>
using namespace std;
string Upper(string & s);
int main()
{
    cout<<"Enter a string (q to quit): ";
    string str;
    getline(cin,str);   
    Upper(str);            //只改了这里到
    while(str!="Q")               
    {
        cout<<str<<endl;
        cout<<"Next string (q to quit): ";
        getline(cin,str);
        Upper(str);
    }                    //这里。目测可行。
    cout<<"Bye.\n";
    return 0;
}

string Upper(string &s)
{
    char *pStr = (char *)s.c_str();
    while(*pStr)
    {
       *pStr = toupper(*pStr);
        pStr++;
    }
    return s;
}
#13
yangmingcout2013-01-02 00:39
回复 2楼 yuccn
好手法
1