Solved

If statements

Posted on 2004-10-16
4
176 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
4 Comments
 
LVL 16

Accepted Solution

by:
nonubik earned 125 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

816 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now