C++/how to let program cheque that two name are same or not
I am new to c programming and having problem of let program decide that one name from file and one that is input by user are same or not my program is giving me wrong answer.
If correct print yes if not print no.
They are the same but my programs answer is no
Well the code you show implies Bipul is a program identifier and _not_ a literal string:
if ( Bipul==Bipul )
std::cout << "Bipul variable equals itself!!\n";
std::cout << "WEIRD: Bipul variable DOES NOT equal itself!!\n";
If you meant a C/C++ string literal value having the characters B i p u l then you enclose the string in double quotes:
"Bipul" // C/C++ string literal containing single char sized characters
In which case you would have had to have written something like so:
if ( "Bipul"=="Bipul" )
std::cout << "Left \"Bipul\" string literal equals right \"Bipul\" string literal.\n";
std::cout << "Left \"Bipul\" string literal does not equal right \"Bipul\" string literal.\n"";
And either the if or else clause could have been executed as string literals with the same characters may or may not evaluate as equal - it is a compiler implementation detail.
The reason for this has to do with the type of C and C++ string literals. They have type:
"'array of n const char', where n is the size of the string"
(quoted from a current edition of the ISO C++ standard section 2.14.5 String literals, paragraph 8)
In C and C++ when comparing built in arrays the array types decay to pointers to the type of arrays' elements, with the pointers' values pointing to the address of the first elements of the arrays being compared.
compares two string literals which are of type array of 6 const char (5 characters in the string plus one for the C string zero terminator). This then becomes a comparison of the addresses of the first characters of the string literals compared. That is it compares the locations of the string literals and not the contents of the strings.
As the characters of the string literals are the same the compiler might be clever enough to use the same copy of the string literal in memory. In this case the two literals in fact refer to the same string at the same locations in memory and therefore their starting addresses will be the same and the result of the expression will be true.
However, if the compiler is not clever, or has be asked not to map same valued literal strings to the same strings in memory, then each "Bipul" string literal will be at a different location in memory so comparing their starting addresses will yield a result of false.
So if in your program you are using built in arrays of char (or pointers to such arrays - much the same in C/C++) - i.,e. C strings - then comparing strings in different variables is equivalent to comparing the pointer address values of the first characters of the C strings - which as they are different variables will be at different locations and so will differ. That is you are comparing where the strings are in memory not what the contents of the strings are.
To perform operations on C strings (i.e. built in arrays of char with a zero character value terminating the string) use the functions C++ inherits from the C library in the cstring header (called string.h in C or pre 1998 ISO standard C++). The function to compare two C strings is called strcmp - or more properly in ISO standard C++ std::strcmp.
The strcmp function takes two pointers to (const) char arrays representing C strings to compare and returns 0 if they are lexicographically equal (see for example http://en.cppreference.com/w/cpp/string/byte/strcmp
for more details).
If you are using a fairly recent C++ implementation - i.e. after 1998 - that includes an implementation of the C++ standard library then you can use std::string (include string) to hold your strings. Objects of type std::string are much easier to use than C-strings. They can be compared on the string contents for a start and can be manipulated much easier - e.g. be extended, have parts replaced with other strings etc. They also interact with C-strings quite well. Here is an example program a bit like that you describe in your question that shows using C++ library std::string objects:
// Function to return a std::string - as if from file but in this case from
// a static array of strings that wraps around to first after returning last.
static char const * file_strings =
static std::size_t const NumberOfDatums( sizeof(file_strings)/sizeof(char const *) );
static unsigned int index(0);
index = index % NumberOfDatums;
return file_strings[index++]; // Note: C string converted to C++ library std::string
// Function to check if passed std::string indicates we should continue
// Returns true if we should continue or false if we should quit.
bool not_done( std::string const & s )
return s!="Q" && s!="q";
// Main entry point:
// Repeatedly get string from user and see if it matches current string
// returned from pseudo_string_from_file.
// Quits if user enters "q" or "Q" (followed by enter/return of course).
std::cout << "Guess what string I am thinking about (enter Q to quit): ";
std::cin >> user_guess;
if ( not_done(user_guess) )
std::string file_string( pseudo_string_from_file() );
if ( user_guess==file_string )
std::cout << "Congratulations! You guessed correctly.\n";
std::cout << "Sorry your guess was wrong. I was thinking about '"
<< file_string << "'\n";
while ( not_done(user_guess) );
return 0; // not need for main but here for broken compilers.
Please note that this code is for example purposes only and should not be considered production quality. It may contain typos, errors etc. - if so then I apologise. Finally please note that the code is laid and indented for display / editing using a fixed pitch font such as Courier (as most source code is) and may have lost some indenting when posting through AllExperts.
Hope this helps you sort out your program.