7 Replies - 581 Views - Last Post: 12 June 2011 - 02:34 AM Rate Topic: -----

#1 Jozhogg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 11-June 11

If else with strings not working

Posted 11 June 2011 - 05:03 AM

Hi, I'm very (about half an hour) new to C++ but I felt I should give it a look.

I was trying to create this as a bit of a joke to a friend but this specific if else situation isn't working.


 string namestr;
     string gfxstr;
      cout << "Hello there, \n\n Please state your name? \n" << endl;
      getline (cin, namestr);
      cout << "\n Hello " << namestr << "\n\n What is the aftermarket brand of your Graphics Card? \n" << endl;
      getline (cin, gfxstr);
      if (gfxstr="xfx", "XFX", "Xfx"){
      cout << "\n\n XFX are beast!! \n\n" << endl;
      }
      else {
      cout << "\n\n HAHAHAHAH!  " << gfxstr << " are rubbish! \n\n" <<endl;
      }



It will only ever use the "\n\n XFX are beast!!" line, regardless of the input to gfxstr. Any suggestions?

Is This A Good Question/Topic? 0
  • +

Replies To: If else with strings not working

#2 sk1v3r  Icon User is offline

  • D.I.C Addict

Reputation: 231
  • View blog
  • Posts: 668
  • Joined: 06-December 10

Re: If else with strings not working

Posted 11 June 2011 - 05:10 AM

when you are doing boolean statements (trying to get a true or false value) and you want to test equality, then you use == instead of =, as the second performs assignment.
Also, checking equality against multiple things requires separate boolean statements. E.G.
int guess;
cin >> guess;
if ( guess == 7 || guess == 10 )
{
    cout << "correct";
}
else
{
    cout << "nope";
}



|| means or, so if the first statement is false, it ckecks the next statement. In this example, if guess does not equal seven, it checks to see if guess equals ten.
Hope This Helps :)

If you are still a bit unsure, this link should help :)

This post has been edited by sk1v3r: 11 June 2011 - 05:13 AM

Was This Post Helpful? 1
  • +
  • -

#3 Jozhogg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 11-June 11

Re: If else with strings not working

Posted 11 June 2011 - 05:15 AM

Brilliant! That was a great help.

Thanks very much
Was This Post Helpful? 0
  • +
  • -

#4 v0rtex  Icon User is offline

  • Caffeine: db "Never Enough!"
  • member icon

Reputation: 223
  • View blog
  • Posts: 773
  • Joined: 02-June 10

Re: If else with strings not working

Posted 11 June 2011 - 05:23 AM

You should not really compare a string like you would a primitive data type such as a Integer. lucky for you though, std::string has functions that can be used to compare ASCII values and thus compare one string with another string :)

A simple example of a function called compare could do the trick, an example use of it would be:

if (gfxstr.compare("xfx") != 0) { /*ie: true = 1 and false = 0 therefore if gfxstr is equal to "xfx" execute statements below: */



However should you wish to use the equals operator, your logic regarding how you compare the string with the literal constant is kind of wrong. First off, do not use the assignment operator (=) to compare anything as that does something entirely different. Also if you are doing multiple checks on different words, use the OR (||) Operator to continue the condition. A simple example is this:

     if (gfxstr=="xfx" || gfxstr == "XFX" || gfxstr == "Xfx"){
     cout << "\n\n XFX are beast!! \n\n" << endl;
     }



I hope this helps,
v0rtex

Oops, bit late did not see the post above as I was posting :)
Was This Post Helpful? 0
  • +
  • -

#5 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 856
  • View blog
  • Posts: 2,339
  • Joined: 20-August 07

Re: If else with strings not working

Posted 12 June 2011 - 12:09 AM

View Postv0rtex, on 11 June 2011 - 01:23 PM, said:

You should not really compare a string like you would a primitive data type such as a Integer.

lucky for you though, std::string has functions that can be used to compare ASCII values and thus compare one string with another string :)
It's perfectly OK actually. std::string has overloaded operators which allow you to compare one string to another. Usually they are implemented as wrappers for string::compare so it makes no difference (although operator syntax is usually more idiomatic)
Was This Post Helpful? 1
  • +
  • -

#6 v0rtex  Icon User is offline

  • Caffeine: db "Never Enough!"
  • member icon

Reputation: 223
  • View blog
  • Posts: 773
  • Joined: 02-June 10

Re: If else with strings not working

Posted 12 June 2011 - 12:11 AM

Ah ok, I stand corrected then. I know with Java, One generally never compares Strings like you would a Integer. Thanks Bench. Either way the string functions have their uses...

This post has been edited by v0rtex: 12 June 2011 - 12:15 AM

Was This Post Helpful? 1
  • +
  • -

#7 codewizard  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 16
  • Joined: 20-April 11

Re: If else with strings not working

Posted 12 June 2011 - 12:46 AM

Ye either way does the trick but I find:
if(gfx == "XFX" || etc... ) 
{
//do something
}



A lot easier but I also know of coders who insist on using the string's functions, its fine either way tbh.
Was This Post Helpful? 1
  • +
  • -

#8 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 856
  • View blog
  • Posts: 2,339
  • Joined: 20-August 07

Re: If else with strings not working

Posted 12 June 2011 - 02:34 AM

View Postv0rtex, on 12 June 2011 - 08:11 AM, said:

Ah ok, I stand corrected then. I know with Java, One generally never compares Strings like you would a Integer. Thanks Bench. Either way the string functions have their uses...
I agree that compare can certainly be useful sometimes.

In both C++ and Java, the == operator compares whatever a variable directly represents in memory. The difference between the languages is that Java variables are usually "pointers" in memory; storing an address value (a.k.a references), whereas C++ variables directly represent the objects themselves in memory. This is why using == to compare String variables will not necessarily work as expected in Java.


I believe that there are other issues with strings in Java too, relating to the way that the String Pool works; although I'm not much of a Java programmer so I can't recall the ins-and-outs.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1