if statement

This is a discussion on if statement within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream.h> char foo[10]; int main() { cin >> foo; if (foo == "hello") { cout << "HI!!!!!"; } ...

  1. #1
    Unregistered
    Guest

    if statement

    Code:
    #include <iostream.h>
    
    char foo[10];
    
    int main()
    {
    	cin >> foo;
    	if (foo == "hello")
    	{
    		cout << "HI!!!!!";
    	}
    
    	return 0;
    }
    I would have thought that if I entered the word hello it would output HI!!!!! bit it does'nt. What's wrong with it?

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Use strcmp to compare 2 strings.

  3. #3
    Unregistered
    Guest
    Thanks for the reply but I'm still not sure what's wrong with the code posted above.

  4. #4
    TK
    Guest
    The name of the character array is the same as a pointer to the first element of the array, so here you are comparing:

    if(h == "HELLO")

    To which it is saying, false (1).

  5. #5
    TK
    Guest
    Or maybe even it's an address such as:

    if(343433 == "hello")

  6. #6
    Unregistered
    Guest
    The name of the character array is the same as a pointer to the first element of the array, so here you are comparing:

    if(h == "HELLO")

    To which it is saying, false (1).
    I'm not sure that is the case bacuase if i tell it to output the foo variable, it will output what i entered rather than just one byte as you are suggesting.

  7. #7
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680

    Re: if statement

    Try this:
    Code:
    if (strcmp(foo, "hello") == 0)
    {
      cout << "HI!!!!!";
    }

  8. #8
    Unregistered
    Guest
    Yes, but is'nt there a way to do it without using a function?

  9. #9
    ..
    Guest
    The name of the array is a pointer to the first element:

    char name[222];

    name is the same as &name[i]

    Check out your textbook, the chapter on arrays.

  10. #10
    ..
    Guest
    The only way to get around it is to create a new type. In C++ they have a string type, that allows you to make the comparison that you want to make, because it overloads the = operator.

  11. #11
    ..
    Guest
    When you are outputting the array, you are using printf which takes a pointer to char arguement, or in other words the name of the array (which is a pointer to the first element).

  12. #12
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Originally posted by Unregistered
    Yes, but is'nt there a way to do it without using a function?
    Code:
    int main() 
    { 
      int i = 0;
      char foo[10];
      char s[] = "Hello";
    
      cin >> foo;
    
      while(s[i] == foo[i] && s[i]) i++;
    
      if(!s[i] && !foo[i])
      {
        cout << "HI!!!!!";
      }
    
      return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 05:19 AM
  2. Meaning of this statement?
    By @nthony in forum C Programming
    Replies: 7
    Last Post: 07-16-2006, 02:57 AM
  3. If Else statement problem
    By doofusboy in forum C Programming
    Replies: 2
    Last Post: 11-09-2005, 06:18 AM
  4. if/break statement
    By Apropos in forum C++ Programming
    Replies: 7
    Last Post: 02-22-2005, 01:33 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 03:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21