2017-08-31 8 views
-1

charの配列でcharを使って繰り返しますが(文字列で構築しました)、文字==反復文字列の場合は文字を出力します。しかし、最初に印刷される文字は、目的の文字列と同じではありません。そして時には予期しない文字が最後に追加されることがあります。charが間違っている配列を繰り返し返す

予想される出力:

私はスティーブだ

出力:

〜Stevefだ

私は、出力コンソールのプリントを作成しようとしている

反復文字列では、私はこのように意味する:

A 

まず、コンソール版画Aあなたがライン14上で見ることができるように、私は、キャリッジリターンを行うと、出力が所望の文字列の最初の文字になるまで、Bになります。 出力された文字が文字と同じ場合は、配列cmemに入れます。そして、それは第2のキャラクターを繰り返し続けます。

#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; 
} 
関連する問題