Discussion Locked

If statements in C++

0 helpful
4 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Please help I am new to C++ programming.

Recently I tried to write a program in C++ 6.0 which had an if statement like

(if today == "Sunday) then do something

but if I type Sunday when running program it does not do what asked.

Can anyone tell me what is wrong?

Mark

4 replies

by monkeybutter

You can't compare strings using ==

Have a look at:
http://www-math.unice.fr/laboratoire/help/C/ctutor/ctutor.html

It will tell you what you need to know about datatypes, strings and the C syntax to get started. I strongly suggest you work with vanilla C before you move onto C++. The syntax is almost identical, apart from input streams and the bits related to object oriented programming which you don't need to worry about yet.

In fact, use Turbo C as your IDE/compiler for your first few programs. It's a basic but tidy compiler that'll let you find your feet. The blue background is also very soothing :-)

by mmmova

The == operator is used to check references rather than the content. In your code, you should use string comparison functions like strcmp(dest, source) and it will definitely solve your problem.
Check this out or email me your program and i will solve your problem.

Regards
MMM

by a.dhaene

== test the value of primitieve datatypes such as int,double,char. One exception is string. It follows the rules of a reference type (object,..) In this case it compares the reference (the adress).

by gy24hz13

The correct "if()" statement is
-----------------------
if(condition) action1; // only 1 action
------or-----
if(condition)
{
action1;
action2; // more than 1 action
actionn;
}
-------------------------
Example
-------------------------
void getmydate(char * today)
{
if(today == "Sunday")
MessageBox("Today is Sunday. Enjoy your holiday!", "Message", MB_OK);
}

This discussion is closed to new replies.