/* 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>
usingnamespace 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'; }
elseif(age < 27){
cout << "You are younger than me. I am 27" << '\n'; }
elseif(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'; }
elseif(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.
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.
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'; }
elseif(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.
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.
if(strcmp (colour,mycolour) == 0){
cout << "Wow, mine too! What a coincidence" << '\n'; }
elseif(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.