Problem with strcmp

Hi, I'm new to C++ programming and am trying to create a simple AI conversation program which asks a number of questions and replies.

I have the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/* this is a sample program which should ask the user's
name and say hello if it works */

#include <iostream>
#include <cstdlib>
#include <string.h>
using namespace std;

int main()
{

char yourname [80];
char colour [20];
char mycolour [] = "blue";
int age;


cout << "Hello, what is your name";
cout << "\n";
cin >> yourname;

cout << "Hello ";
cout << yourname << '\n';

cout << "How old are you?" << '\n';
cin >> age;

if(age>27){
cout << "You are older than me. I am only 27" << '\n'; }

else if(age < 27){
cout << "You are younger than me. I am 27" << '\n'; }

else if(age = 27){
cout << "We are the same age!" << '\n'; }

cout << "What is your favourite colour?" << '\n';
cin >> colour;

if(strcmp (colour,mycolour) != 1){
cout << "Wow, mine too! What a coincidence" << '\n'; }

else if(strcmp (colour,mycolour) != 0){
cout << "Wow, mine too! What a coincidence" << '\n'; }

cout << "It's been very nice talking to you " << yourname << " but I have to go now. Goodbye";

return 0;

}


For some reason (probably very obvious to people in the know) the program won't work in the strcmp line. It never recognises the colour entered as being different from the one assigned to the 'mycolour' variable and so returns "Wow, what a coincidence" every time.

Any ideas?

Thanks

Your code outputs "Wow, mine too! What a coincidence" in the case where they are the same, and the case where they are different (i.e. line 41 is the same as line 44).

I'm guessing that you meant this test - if(strcmp (colour,mycolour) != 1) - to be the test for "not the same". You've actually got that test wrong.

What happens in your code when the output of strcmp is not zero or one? strcmp returns zero if they're the same, but could return any integer at all if they're different. The returned value carries more meaning that just "same/not-same". If they're different, and the returned value is 2, for example, the first test passes even though that's meant to be your test that passes when they are the same.
Last edited on
Yes, sorry they were the same because of a copy and paste I did to attempt to rectify the problem. I have changed them back and still have the same problem of the else if statement never been returned no matter what colour is entered

1
2
3
4
5
6
7
8
cout << "What is your favourite colour?" << '\n';
cin >> colour;

if(strcmp (colour,mycolour) != 0){
cout << "Wow, mine too! What a coincidence" << '\n'; }

else if(strcmp (colour,mycolour) != 1){
cout << colour << " is ok but I prefer blue" << '\n'; }


Could you please explain what you mean by the strcmp not being zero or one? I was under the impression it had to be one of these but seemingly not by your reply.

Thanks for the reply
The return of strcmp will be zero if the two are the same. A value greater than zero indicates that the first character that does not match has a greater value in str1 than in str2, and a value less than zero indicates the opposite.


http://www.cplusplus.com/reference/clibrary/cstring/strcmp/

Accordingly, your test should be
1
2
3
4
5
if(strcmp (colour,mycolour) == 0){
cout << "Wow, mine too! What a coincidence" << '\n'; }

else if(strcmp (colour,mycolour) != 0){
cout << colour << " is ok but I prefer blue" << '\n'; }


As an aside, this

1
2
#include <cstdlib>
#include <string.h> 


is not right. string.h is a C library. You can use it if you like, but it's better to use the C++ replacement. The C++ replacement is called cstring, which you've already included. You're including the C and C++ headers of the same thing.

It would be better to use <string>; you would then be able to use proper C++ std::string rather than arrays of char. The std::string comes with a nice == operator you can use to compare two std::string objects too, so you wouldn't have to bother with strcmp.

Last edited on
Thank you very much for taking the time to help out a beginner.

I will take your advice on the <string> header.

Thanks again
This is the correct form:
1
2
3
4
5
6
7
8
9
10
cout << "What is your favourite colour?" << '\n';
	cin >> colour;
	//char mycolour[] = "blue";
	if(strcmp (colour, mycolour) == 0){
		cout << "Wow, mine too! What a coincidence" << '\n'; 
		}

	else /*if(strcmp (colour,mycolour) != 1)*/{
		cout << colour << " is ok but I prefer blue" << '\n'; 
		}
Topic archived. No new replies allowed.