objects

This is a discussion on objects within the C++ Programming forums, part of the General Programming Boards category; I have created an object which has a char str[]. Why cant I do any of the following Code: if ...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    4

    objects

    I have created an object which has a char str[]. Why cant I do any of the following
    Code:
    if (obj.str =="something")
    
    if (obj.str != "something")
    
    while (obj.str != '\n')
    am I doing something wrong???? In order to do a comparison, must they both be objects???


    cheers

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    When using == you compare the pointers not the actual string. Use strcmp() to compare strings, it returns 0 if both strings match.

    In the while-loop you make a comparasion between a char and a char*, which is not valid. You probably want to pick out a single character from the string: while (obj.str[index] != '\n')
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. most efficient way to write filestream objects?
    By darsunt in forum C++ Programming
    Replies: 3
    Last Post: 01-26-2009, 05:17 PM
  2. Replies: 60
    Last Post: 12-20-2005, 11:36 PM
  3. Question about cout an stack object?
    By joenching in forum C++ Programming
    Replies: 8
    Last Post: 05-08-2005, 11:10 PM
  4. chain of objects within pop framework help needed
    By Davey in forum C++ Programming
    Replies: 0
    Last Post: 04-15-2004, 11:01 AM
  5. array of objects?
    By *~*~*~* in forum C++ Programming
    Replies: 4
    Last Post: 05-31-2003, 06:57 PM

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