精英盒子 -> 程序设计 -> 有道题目写不出来~~~~ [打印本页]

编程土豆 2011-10-06 18:24

有道题目写不出来~~~~

程序用来删除输入的string对象中的标点符号,自己写了个:
  1. #include  <iostream>
    #include  <string>
    using  std::cout;
    using  std::endl;
    using  std::cin;
    using  std::string;

    int  main()
    {
        string  word;
        getline(cin,word);
        for(string::size_type  index  =  0;index  <=   word.size();index++)
            if(ispunct(word[index]))
                word[index]  =  "";
        cout  <<  word;
        return  0;
    }




但编译出错:
  1. error: invalid conversion from ‘const char*’ to ‘char’
怎么改呀?

outman 2011-10-06 20:37
  1. word[index]  =  "";

改成
  1. word[index]  = ‘’;



pcliker 2011-10-06 20:42
找我也没用,我不会编程……

jybox 2011-10-06 20:51
  1. #include  <iostream>
    #include  <string>
    using  std::cout;
    using  std::endl;
    using  std::cin;
    using  std::string;
    int  main()
    {
        string  word;
        getline(cin,word);
        for(string::size_type  index  =  0;index  <=   word.size();index++)
            if(ispunct(word[index]))
                word.replace(index,1,string(""));
        cout  <<  word;
        return  0;
    }



jybox 2011-10-06 20:51
标准库很不爽啊,还是qt爽

jybox 2011-10-06 20:52
outman:word  =  "";
改成
word  = ‘’;
 (2011-10-06 20:37) 

依旧错误
main.cpp:13:29: error: empty character constant

编程土豆 2011-10-10 18:15


天哪!!其实只要加一个空格就好了,我想了一个下午,怎么才能做到最简单~~~~浪费那么多脑细胞
  1. #include  <iostream>
    #include  <string>
    using  std::cout;
    using  std::endl;
    using  std::cin;
    using  std::string;

    int  main()
    {
        string  word;
        getline(cin,word);
        for(string::size_type  index  =  0;index  <=   word.size();index++)
            if(ispunct(word[index]))
                word[index]  =  ' ';
        cout  <<  word;
        cout  <<  endl;
        return  0;
    }



abreto 2011-10-10 18:20
删除不是这么写的,
应该从后往前覆盖
而且
string::opeartor[]返回的貌似是const char*

whtsky 2011-11-06 20:08
代码在手机客户端呈现为一大坨…无换行…

jybox 2011-11-06 20:40
whtsky:代码在手机客户端呈现为一大坨…无换行… (2011-11-06 20:08) 

那也不影响你阅读代码嘛

whtsky 2011-11-06 20:50
第9楼jybox于2011-11-06 20:40发表的 回 8楼(whtsky) 的帖子 的帖子
whtsky:代码在手机客户端呈现为一大坨…无换行… (2011-11-06 20:08) [url=http://jybox.n ..
无换行,蛋疼,根本没法读




Powered by phpwind v8.7 Code ©2003-2011 phpwind
Time 0.044475 second(s),query:5 Gzip enabled