2017-08-31 42 views
-1

我想用字符数组中的字符(但我使用字符串构建它)来遍历并打印字符,如果字符==迭代字符串。但是,第一个打印的字符与所需的字符串不同。最后它有时会增加一个意想不到的字符。迭代通过char字符数组出错

预期输出:

我是史蒂夫

输出:

〜我Stevef

我试图让控制台打印输出与迭代的字符串,我的意思是这样的:

A 

第一,控制台打印A,然后我做回车,你可以在网上看到14和转向B,直到输出所需字符串的第一个字符。 如果输出的字符与字符相同,则将其放入数组cmem。并继续迭代第二个角色,依此类推。

下面的代码:

#include <iostream> 
using namespace std; 
//starts here! 
int main(){ 
    char hlw[] = "I am Steve."; 
    char j; //to be iterated. 
    int hlwstrlen = strlen(hlw); 
    char cmem[hlwstrlen]; //memorize the correct char. 
    char convertedChar; //converted char 
    //iterating begin 
    for (int ch = 0; ch <= hlwstrlen; ch++){ 
     for (int aschr = 32; aschr <= 126; aschr++){ 
      convertedChar = static_cast<char>(aschr); //this converts to an ascii from an int. 
      cout << convertedChar << "\r"; 
      if(convertedChar == hlw[ch]){ 
       cout << convertedChar << "\r"; 
       cmem[ch] = convertedChar; 

       for(int i = 0; i <= ch; i++){ 
        cout << cmem[i]; 
       } 
       continue; 

      } 
     } 
    } 
cout << endl; 
return 0; 
} 

注:对不起,如果我不能完全格式化代码。我用我的手机打字。

回答

1
#include <string> 
#include <iostream> 
#include <algorithm> // remove_copy_if 
#include <cctype> // isprint, isalpha, isalnum, ispunct 

// you check for these characters. 
// I suspect you may want std::isprint, instead. 
bool custom_exclude_filter(char c) { 
    return c < 32 || c > 126; 
} 

int main() { 
    // since you are trying to filter out bad characters, 
    // let's put a bad character in the actual string 
    char hlw[] = "I am \x010Steve."; 
    std::string s(hlw, sizeof(hlw)); 

    // print only the printable characters 
    for (auto c : s) { 
     if (std::isprint(c)) 
      std::cout << c; 
    } 
    std::cout << std::endl; 

    // works the same on hlw. The compiler knows its size already. 
    for (auto c : hlw) { 
     if (std::isprint(c)) 
      std::cout << c; 
    } 
    std::cout << std::endl; 

    // same thing using your custom filter 
    for (auto c : s) { 
     if (!custom_exclude_filter(c)) 
      std::cout << c; 
    } 
    std::cout << std::endl; 

    // Make a new string using only printable characters, 
    // using std algorithm, and print the string 
    { 
     std::string temp_string; 
     std::remove_copy_if(s.begin(), s.end(), std::back_inserter(temp_string), [](char c) {return 0 == std::isprint(c); }); 
     std::cout << temp_string << std::endl; 
    } 

    // you can iterate a string literal, using std::begin and std::end 
    // see https://stackoverflow.com/a/13207440/1766544 
    { 
     std::string temp_string; 
     std::remove_copy_if(std::begin(hlw), std::end(hlw), std::back_inserter(temp_string), [](char c) {return 0 == std::isprint(c); }); 
     std::cout << temp_string << std::endl; 
    } 

    // same thing using your custom filter 
    { 
     std::string temp_string; 
     std::remove_copy_if(s.begin(), s.end(), std::back_inserter(temp_string), custom_exclude_filter); 
     std::cout << temp_string << std::endl; 
    } 
} 
+0

忽略最后三种情况,现在我看到您对注释做出响应,说您要对输出进行流式处理,所以不需要使用您使用的临时字符串。 –

0

我用过某人的代码之前在这里评论过,我忘记了谁,也不知道为什么所有的评论都被删除了。它非常完美!感谢以前帮助我的所有人!

#include <iostream> 

using namespace std; 

int main(){ 
    const char hlw[] = "I am Steve"; //Declare collection of char 
    const int hlwstrlen = strlen(hlw); 
    char *cmem = new char[hlwstrlen]; 
    char convertedChar; 
    int len = 0; 

    for (int ch = 0; ch < hlwstrlen; ch++){ 

     for (int aschr = 32; aschr <= 126; aschr++){ 

      const char convertedChar = static_cast<char>(aschr); 

      cout << convertedChar << "\r"; 

      if (convertedChar == hlw[ch]){ 

       cmem[len++] = convertedChar; 

       continue; 

      }  
     } 
    } 
cout << cmem << '\n'; 

delete[] cmem; 
cout << endl; 

return 0; 
}