この小さなプログラムで何が間違っていますか。
私は c++ を学び始めたばかりですが、これは意味のない質問として受け入れることができます。Prata c++ 入門書を読んでいると、char 配列を取り、for ループで strcmp() を使用するコード例が得られました。これは、'?' で始まる ASCII コードを順次反復します。テスト char 変数 ==sa 別の char から値を設定するまで。
私は本を凌駕できると考えて、char配列を取り、forループを使用してテストchar配列を取り、2つの変数が等しくなるまで配列の各値を反復処理する同様のプログラムを作成しようとしました。
プログラムが単純に for ループをスキップして終了するように見える問題が発生していたため、for ループ内の各配列の最初の部分のみを取得するようにプログラムを単純化しました。
以下は、最初に prata コード スニペットであり、その後に私のコードが続きます。どんなフィードバックでも (虐待 >_< であっても) 役に立ちます。
#include <iostream>
#include <cstring>
int main() {
using namespace std;
char word[5] = "?ate";
for (char ch = ‘a’; strcmp(word, "mate"); ch++) {
cout << word << endl;
word[0] = ch;
}
cout << "After loop ends, word is " << word << endl;
return 0;
}
私のコード(不十分かもしれませんが、それを受け入れることができます)
#include <iostream>
#include <cstring>
int main() {
using namespace std;
char word[5] = "word";
char test[5] = "????";
int j = 0;
int i = 0;
cout << "word is " << word << "\nTest is " << test << endl;
cout << word[0] << " " << test[0] << endl;
for (char temp = '?'; word[0] == test[0] || temp == 'z'; temp++) {
if ((word[i]) == (test[j])) {
test[j] = temp;
j++;
temp = '?';
}
test[j] = temp++;
cout << test << endl; //Added to see if the for loop runs through once,
//which is does not
}
return 0;
}