2017-08-31 57 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; 
}