Search code examples
c++stringcomparisonstring-comparisonstrcmp

issue with strcmp() not comparing strings properly


Here's the actual code, since it seems to be specific to something here.

#include <iostream>
#include <string.h>

using namespace std;

int main()

cout << "  Just say \"Ready\" when you want to start.";
char tempReady[20];
cin >> tempReady;
length = strlen(tempReady);
char* ready = new char[length+1];
strcpy(ready, tempReady);
while((strcmp(ready, "Ready")||strcmp(ready, "ready"))!=0)
   {
   cout << "Try again.";
   cin >> tempReady;
   length = strlen(tempReady);
   delete[] ready;
   ready = new char[length+1];
   strcpy(ready, tempReady);
   }
cout << "Success";

Anyone see anything wrong?


Solution

  • while((strcmp(ready, "Ready")||strcmp(ready, "ready"))!=0)
    

    should be

    while(strcmp(ready, "Ready") != 0 && strcmp(ready, "ready") != 0)
    

    The version you wrote will always be true.