You are here:

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.
Please help.
i.e. Bipul==Bipul
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:

   int Bipul=0;
   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, 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:

   #include <string>
   #include <iostream>

   // 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.
   std::string pseudo_string_from_file()
       static char const * file_strings[] =
         { "first"
         , "second"
         , "third"
         , "forth"
       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).
   int main()
       std::string user_guess;
         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.  


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at


©2017 All rights reserved.