Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

If statements

Posted on 2004-10-16
4
Medium Priority
?
181 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

604 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