Thread: Not equal to operator NOT working

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    81

    Not equal to operator NOT working

    I have a question about the relational != operator.

    First here is part of my code:
    Code:
    void RuleThree(string word, string target)
    {
    	string temp;
    	int len = word.length()-1;   // Index of last character
    
    	cout << target << endl;
    
    	if (target!="TH" || target!="th")  // here is the problem!!
    
    	{
    		temp = word.substr(1,len);
    		word = temp + target + "AY";
    		cout << word << endl;
    	}
    
    } // End function RuleThree
    This is simply not working. When my word starts with TH or th, it still accepts the word and goes through the code. When I do this though:

    if (target!="TH" || target=="th")

    where only one of them is set to not equal to, it works.

    The problem is that I have to add more than just the TH and th to the if statement, but when I do, it treats it as an equal to.

    I just don't get it.

    Any help would be appreciated

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    >> When my word starts with TH or th, it still accepts the word and goes through the code.

    You're testing the whole string, not just the start!

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    81
    target = word.substr(0,2);

    is included in the code in order to test for the first 2 characters. Thanks for your reply!

  4. #4
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    So does it work?

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    81
    I'm sorry, no it doesn't work... I've tried to put the substr inside the function as well and it still won't accept the NOT operator. I've put it right before the call to the function (don't know if that's a good thing or not) but at this point I'm trying anything and everything.

    I just don't get it, I've never had this problem before.

  6. #6
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    I'm not sure if this is what you want to use the function for, but anyway:
    Code:
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    void RuleThree(string word, string target)
    {
    	string temp1, temp2;
    	
    	int len = word.length()-1;   // Index of last character
    
    	cout << target << endl;
    
    	temp2 = word.substr(0,2);
    	
    	if (temp2 == "TH" || temp2 == "th")
    		return;
    	else
    	{
    		temp1 = word.substr(1,len);
    		word = temp1 + target + "AY";
    		cout << word << endl;
    	}
    
    } // End function RuleThree
    
    int main()
    {
    	string one = "THAT'S the joint!";
    	RuleThree("THAT'S", one);
    
    	return 0;
    }

  7. #7
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Code:
    if (target!="TH" || target!="th")  // here is the problem!!
    
    	{
    		temp = word.substr(1,len);
    		word = temp + target + "AY";
    		cout << word << endl;
    	}
    Lets asume that target is "th".

    After the evaluation of target != "TH" it evalutes true and therefore it doesn´t need to evaluate anymore bacause regardless of the outcome of the second evaluation the condintion is true!

    What you inteended to do is that if target doesn´t start with "TH" or "th" then the compound statement should be executed.

    Try change it to
    Code:
    if (!(target == "TH" || target == "th"))
    //statement or compound statement
    Hope that it solves your problem
    Last edited by ripper079; 09-29-2002 at 02:25 PM.

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    81

    THANK YOU SO MUCH!!

    THE DOG Thanks for your answer, you actually gave me an idea to make my code better and it worked! I never thought of making more than one target (duh), I think that's where my problem lied.

    RIPPER079 Well that answered my question about the not operator, no wonder it didn't work! I will definitely add that one to my notes!! Thank you very much, you not only made me understand it, but it's one I will not ever forget!

    Thanks to you both!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Not working Right
    By raven420smoke in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2005, 03:21 AM
  2. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  3. x on upper right corner not working
    By caduardo21 in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2005, 08:35 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM