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?
"Shah Bhuiyan"is a String Literal, another array. Soname == "Shah Bhuiyan"compares two addresses and, because they are both different objects, they can't possibly be pointing at the same place.std::getlineis a function,std::cinis 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 tonameand"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.strings allow comparisons with==, butchar name[1000];is not astring. It is an array."Shah Bhuiyan"is also not astring. It is a String Literal, another array.cinagainst '"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?