Hi
I'm rather new to C++ and am rather confused about something.

I noticed, say, in an "if else" statement or a "Switch" statement it only evaluates numerical expressions. In an If Else statement I can compare two strings by using a returned boolean result from say 'strcmp' but I can't evaluate an espresions like if(variable1 == "Test") or the like. It seems to only take numerical values.

I'm thinking that this comes down to my fundamental understanding of C++. Is it the case that in all statements such as if and switch and anything else that evaluates and expression it will only evaluate numerical expressions or something that returns a numerical value and not a string comparrison? Do I always have to use something like 'strcmp' to evaluate whether a certain string has been input using cin >> ?


I've just started programming. The closest thing to programming I have used is VBA and I beleive this may be where I'm geting my misconception about how C++ works.

Thanks very much.

Danny2000 :-/

You're not too far off.

With c-strings (strings terminated in '\0' as opposed to std::string which I'll get to in a minute) the variable is essentially a pointer, so saying

char * str1 = "Hello"; //a '\0' is automatically appended at the end of each of these
char * str2 = "Hello";

asking str1 == str2 will come up false every time because the pointers are not equal, they are stored in two different spots in memory. This same idea holds true for character arrays. Hence there's a need for a function like strcmp which does all the finagling for you.

On the other hand, std::strings, when you have

#include <string>
std::string mystr = "Hello";
std::string mystr2 = "Hello";

mystr == mystr2 will be true because the std::strings have an overloaded == operator (essentially a custom recipe specific to the class as to which components of the strings should be compared against each other).

Switch statements in C++ (and C) can only take ints (as well as chars which are basically 1 byte integers, and enumeration types which are implicitly converted to integers). It's been a while since I used VBA but it might be closer to .NET, where like in C# you can use strings in a switch statement.

I've probably glossed over some important points but others will be able to clarify if necessary. I may have known the internals of some of the string functions at one point but I can't recall them offhand. I'm sure they are out on the net somewhere.

commented: Great Answer +3

If/Else will evaluate only bools.
Switch will evaluate integers in whatever form they come from.
Jonsca have given solid examples. BTW have you passed this tutorial and this one?

Thank you very much Jonsca. Thats a really good answer and fully understand now. Thanks too for coming back evstevemd, I will take a look at these tutes. I have several books on C++ but couldn't find a think which explains the above to me.

Cheers
Danny2000

I have several books on C++ but couldn't find a think which explains the above to me.

Cheers
Danny2000

I would defer to use Books and get jumpstart with short concise tutorials until I get brush up. Then I will go back to the book and get comprehensive tutorial

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.