?
Solved

If statements

Posted on 2004-10-16
4
Medium Priority
?
180 Views
Last Modified: 2010-04-01
#include <iostream.h>
#include <conio.h>


int main ()
{
      char name [255];
      cout << "Please input your name: ";
      cin >> name;
      
      if (name == "Josh")
      {
            cout << "My name is josh also!";
      }
      else
      {
            cout << "We don't share the same name.";
      }

      getche();
}

That's my code, I'm using Borlands C++ Compiler.

My question:

How come it won't print "My name is josh also!" when I type in "Josh" in the actual program?

Thanks for your time,

Josh
0
Comment
Question by:Ultimas
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 16

Accepted Solution

by:
nonubik earned 500 total points
ID: 12328016
>if (name == "Josh")
use
>if (strcmp(name ,"Josh") == 0)

'name' is a pointer and "Josh" another one, they both have different values.
strcmp compares the strings.
0
 
LVL 19

Expert Comment

by:drichards
ID: 12328017
You are doing pointer comparison, not string comparison.  You can do:

  if ( strcmp(name, "Josh") == 0 )
0
 
LVL 15

Expert Comment

by:efn
ID: 12328020
Or you could use the string class from the standard library, and then comparing with == would work.
0
 

Author Comment

by:Ultimas
ID: 12328050
Problem solved.

#include <iostream.h>
#include <conio.h>


int main ()
{
      char name [255];
      cout << "Please input your name: ";
      cin >> name;
      
      if (strcmp(name, "Josh") == 0)
      {
            cout << "My name is josh also!";
      }
      else
      {
            cout << "We don't share the same name.";
      }

      getche();
}
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question