Solved

If statements

Posted on 2004-10-16
4
174 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

760 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

18 Experts available now in Live!

Get 1:1 Help Now