0

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?

14
  • 2
    You're not using std::string. Commented Sep 28, 2013 at 22:13
  • I'm not sure -- your program enters the loop when I try it (under MacOS/X using g++-4.2). My guess would be that perhaps stdin has included a carriage return or newline character at the end of the string -- do a cout << strlen(string) to see if it is actually 5 chars long or not. Commented Sep 28, 2013 at 22:15
  • It worked for me in gcc on Linux. Commented Sep 28, 2013 at 22:15
  • Your code works for me, the problem must be somewhere in the code you didn't post. Try to post complete programs. Commented Sep 28, 2013 at 22:15
  • 2
    To get those questions answered you need to post your actual program. Of course strcmp is not picky with the first cap, it expects all letters to match exactly. Your problems are something else than what you think they are, but without seeing the actual code no-one can help you with them. Commented Sep 28, 2013 at 22:28

3 Answers 3

2
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.

Sign up to request clarification or add additional context in comments.

1 Comment

This was the problem all along. All this frustration over a simple logical error. Very obvious.
2

C-style approach:

char str[256];
if (scanf("%255s", str) == 1 && strcmp(str, "hello") == 0) {
    printf("success");
}

C++ approach:

std::string str;
if (std::cin >> str && str == "hello") {
    std::cout << "success";
}

Now decide whether you want to write code in C or C++, just don't mix it.

4 Comments

And your code doesn't even run into an infinite loop when printing success :)
@RobertJørgensgaardEngdahl: Yeah, although OP wrote "I'll enter "hello" at the prompt, and the program will not enter the loop" ~> which sounds like infinite loop was what he actually aimed for :D
I didn't notice until I tested it that it was an infinite loop. But at least it infinitely couted success which made success pretty obvious.
@user1362548: Yes, it's ok. That's why my answer doesn't say anything about that while. One more thing: try to avoid using using namespace std;, it might make you more troubles. For example you named your array string, which might be ambiguous since it can also refer to std::string etc.
1

Here's how to do some basic debugging, such as checking exactly what you input.

using namespace std; 

char* string = new char[6];
cin >> string;

for(int i=0; i<6; ++i)
{
    printf("[%d]: Hex: 0x%x;  Char: %c\n", i, string[i], string[i]);
}

while(strcmp(string, "hello")==0)
{
   cout << "success!";
}

I suspect that your input is something other than hello, (such as hello\n, or hello\r\n, or maybe even (unicode)hello, which makes the strcmp fail.

But rather than me guessing, you can check for yourself using the simple printf above.

If you can come back with the exact Hex dump of your input, and state that strcmp still doesn't work as expected, then we'll have something worth investigating.

5 Comments

@DanO (Don't I wish. I've been trying to get people to use debuggers since S.O. started... but most won't even use printf "debugging")
I don't know how to use printf, but I'm inputting directly from the linux terminal and hitting enter once after typing the word. I couted strlen to see that it was exactly 5 chars which tells me enough.
WTF do you mean you don't know how to use printf?? I put example code in my answer. Copy-Paste it into your program.
(@DanO: See his comment. I rest my case. :D )
@abelenky If only stackoverflow registration could force new users to go through a tutorial on the basics of c. Granted a week long registration process would probably hamper adoptation.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.