Solved

If statements

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Handling string inputs in C/Linux 23 179
add elements to existing standard structure 2 96
SendMessage fails while PostMessage works 2 107
Unresolved External Symbols 3 68
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

920 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

16 Experts available now in Live!

Get 1:1 Help Now