0

just learning C++ here.

#include <iostream>
#include <string>   

int main()
{
    char name[1000];
    std::cout << "What is your name?\n";
    std::cin.get(name, 50);

    name == "Shah Bhuiyan" ? std::cout << "Okay, it's you\n" : std::cout<< "Who is this?\n";


    
}

So here I wrote a program where I created a variable name[100]. I use the cin iostream object thing to take input for the variable name. If the name is equal to my name (as seen in name == "Shah Bhuiyan" :) then output the first thing or output 'Who are you?'

Instead of outputting 'Oh, it's you' it outputs 'who is this?'

Why doesn't this work?

18
  • 1
    You've accidentally performed a pointer comparison. Arrays decay to pointers and "Shah Bhuiyan" is a String Literal, another array. So name == "Shah Bhuiyan" compares two addresses and, because they are both different objects, they can't possibly be pointing at the same place. Commented Sep 23, 2021 at 21:02
  • 2
    You will learn the C++ basics much faster if you spend some time with a good C++ book. Just guessing will be slow and error prone. Commented Sep 23, 2021 at 21:20
  • 1
    "is getline() an object" std::getline is a function, std::cin is a variable. "first why didn't my original code work" Most things you can do with arrays will implicitly convert the arrays to pointers to their first elements (said conversion is also called "decay"). This happened to name and "Shah Bhuiyan" in your original code (both are arrays). Since those are two different arrays (I'm not talking about the contents), their first elements have different addresses, hence == returned false. Commented Sep 23, 2021 at 21:20
  • 1
    strings allow comparisons with ==, but char name[1000]; is not a string. It is an array. "Shah Bhuiyan" is also not a string. It is a String Literal, another array. Commented Sep 23, 2021 at 21:28
  • 1
    after reading that article and thinking it over - correct me if i got it wrong. so i compared to arrays, one of them was whatever was inside the input stream in cin against '"Shah Bhuiyan" in 'char name[100]`. but that's wrong, because what it actually ended up doing was compare the memory addresses of the first element in both arrays because of 'pointer decay'. how close am i? Commented Sep 23, 2021 at 22:33

3 Answers 3

2

Your code is using arrays of characters. Any comparisons using == will compare their memory address. Since name and "Shah Bhuiyan" are two distinct arrays of characters, it will always be false.

The obvious solution is to use c++ strings from the standard library:

#include <iostream>
#include <string>   

int main()
{
    std::string name;
    std::cout << "What is your name?\n";
    std::getline(std::cin, name);

    name == "Shah Bhuiyan" ? std::cout << "Okay, it's you\n" : std::cout<< "Who is this?\n";
}

The std::string type has operators defined that do the right thing here, and will compare the values of each.

Sign up to request clarification or add additional context in comments.

7 Comments

i marked your answer as the correct one, thanks. however i have a question. why do you say my src code involved pointers? i understand i was using arrays when i did char name[1000], but why are pointers and memory addresses involved? all i did was do the == "Shah Bhuiyan" thing by comparing equality to a string, no? someone said "Shah Bhuiyan" was actually a string.
There is the concept of a string, which is nothing but a bunch of characters in a row, and a string which is an object that manages a string. By itself a string is pretty stupid and in C++ you often don't know much more than where it starts. Usually you have to keep reading characters until you find a null character that marks the end. A string object is much more complicated and has a much smarter interface for working with it. "Shah Bhuiyan" is the stupid sort of string, just a bunch of characters and a null terminator.
@ShahJacob To have a meaningful conversation with other C++ programmers you need to know a certain minimum set of terminology, and if you do not have this knowledge, you can't even ask basic questions effectively. If the other programmers understand you and can answer the question, odds are high you won't understand their answers. That's the case here. The best tool to learn this minimum terminology is a good C++ text book.
@user4581301 just downloaded the C++ Programming Practices book by Bjarne. thanks
@ShahJacob c-style arrays decays into pointer during any operations. This is true for "hello" which is a character array, and also char str[1000]. Any operation like ==, [3], ++, passing it in parameters of a function will first decay the array into a pointer. The types std::string and std::string_view are wrappers around character arrays to make them behave like string values, and implement value semantics.
|
1

Arrays do not have the comparison operator. In fact in this expression

name == "Shah Bhuiyan"

there are compared two pointers (due to the implicit conversion of array designators to pointers to their first elements): the first one is a pointer to the first character of the character array name and the second one is a pointer to the first character of the string literal.

As the array and the string literal occupy different extents of memory the comparison of the addresses will always evaluate to false.

You need to use the standard C string function strcmp to compare two strings.

#include <cstring>

//...

std::strcmp( name, "Shah Bhuiyan" ) == 0 ? std::cout << "Okay, it's you\n" : std::cout<< "Who is this?\n";

If you want to use the equality operator == then instead of the character array use an object of the type std::string.

2 Comments

why do i need to use strcmp? i want to use the functionality of strings in cpp not do the c-string stuff. also i am quite confused at what you just did... why are you setting strcmp() to 0??? i mean why are you checking if strcmp is equal to 0, this is so confusing. im trying to check if the input is equal to the string Shah Bhuiyan.
@ShahJacob You declared a character array and are comparing character strings. If you want to use the operator == then declare an object of the type std::string.
0

You could use std::getline to read into std::string instead of C-style char buffer.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.