Another Problem

Do we HAVE to include the quotation marks ("") when we are comparing string literals or setting it while declaring a variable? Example, I have a variable named "happy", and happy's data is 'notsohappy'. but then I want to set "mood" to happy, this is the code anybody would use:

mood=happy;

But won't that set mood into notsohappy? Thats not wat I want. All I want is something so that it sets mood to happy, not inheriting from the VARIABLE! Is it the only way to make a significance between these two is to include the ""? Ah... makes sense! ANd does this rule apply to the if and while statements as well?

if (happy==happy)
if (happy=="happy")
while (happy==happy)
while (happy=="happy")

See significance? Can anybody correct me and tell me If Im wrong in any part?
Thanks. Note to self: One C++ program per week :)
closed account (o3hC5Di1)
Hi there,

You're confusing the variable identifier (its name) with its actual data.
Mostly because you are using ambiguous naming here.

What you would presumably do in this case is something like:

1
2
3
4
5
6
7
8
9
10
11
string mood, happy, unhappy;
happy = "happy";
unhappy = "unhappy";
mood = happy;

// another thing someone might do in this case
//where you have 2 options which are opposites is following:
bool happy;
string mood;
if (happy == true) { mood = "happy as can be"; }
else { mood = "sad puppy face"; }


So it's important to separate a name from its data, the name is merely wha you use in your code to refer to the data which it points to, ie int number = 3;, number is the name you use in the program to refer to the number 3 in this case. When handling raw data, such as a constant string, enclosed in quotation marks, you do need to use the quotation marks to tell the compiler te difference between data and a variable identifier.

For instance, consider these cases:

1
2
3
4
int number = 3;
string strnum = "number";
if (strnum == number) //this will be false, because "number" is not equal to 3
if (strnum == "number") //this will be true 


Hope that helps.

All the best,
NwN
Last edited on
Topic archived. No new replies allowed.