streq
私の教科書には、文字列が等しいかどうかを確認する関数が呼び出されています。このコードをコピーし、cout << *s1 << endl;
false を返す前に出力内容を確認するために追加することにしました。ただし、両方の入力文字列が等しく、追加した追加のコードがある場合、false が返されることに気付きました。
#include <iostream>
using namespace std;
bool streq(const char* s1, const char* s2);
int main()
{
const char A [] = "Hello";
const char B [] = "Hello";
cout << streq(A,B) << endl;
}
bool streq(const char* s1, const char* s2)
{
int count = 0;
while (*s1 != 0 && *s2 != 0)
if (*s1++ != *s2++)
cout << *s1 << endl;
return false;
count++;
return (*s1 == *s2);
}
ただし、追加したコードをコメントアウトすると、関数は正常に動作し、true (1) を返します。ここで何が問題なのか知っている人はいますか?