strcmp doesn't seem to be working

I'm trying to compare 2 strings. I'm using the Dev-C++ compiler in case that matters.
Here's the code i have:
#include <iostream>
#include <cstring>
#include <time.h>
using namespace std;

int main(){
char password[8];
_strdate( password);
char guess[8];
cin >> guess[8];
cin.ignore( 8, '\n' );

if(strcmp (password,guess) != 0){
cout << "The password is www.cpuzzle.webs.com";
}
cin.get();
return 0;
}
But regardless of what i enter in "cout << "The password is www.cpuzzle.webs.com";" still runs.
Any help with this would be awesome.

Thanks in advance, Morgan
Last edited on
Tried:
#include <iostream>
#include <cstring>
#include <time.h>
using namespace std;

int main(){
char password[8];
_strdate( password);
char guess[8];
cin >> guess[8];

if(strcmp (password,guess) == 1){
cout << "The password is www.cpuzzle.webs.com";
}
cin.ignore( 8, '\n' );
cin.get();
return 0;
}
That fixed it displaying no matter what but now even with the correct password (the date) it won't display. Although if i run it in Debug mode it works...
Last edited on
strcmp returns zero when there's a match.

You should probably use larger strings. The documentation for _strdate() says the string should be at least 9 characters long.

And remember, C/C++ arrays start at zero. guess[8] is one beyond the end of char guess[8].

And cin >> guess[8]; reads a character, not a string.
strcmp returns zero when there's a match.

You should probably use larger strings. The documentation for _strdate() says the string should be at least 9 characters long.

And remember, C/C++ arrays start at zero. guess[8] is one beyond the end of char guess[8].

And cin >> guess[8]; reads a character, not a string.
|

Many thanks. I has forgeten about the null char at the end of an array. After changing them to 9 it works perfectly. Oh and is there a way to get a string as opposed to a char?
Last edited on
std::cin.getline(guess,9);
Topic archived. No new replies allowed.