Comparing strings

You can't use C string functions on C++ strings. With C++ strings you compare two strings using the equality operator==.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>

int main()
{
   std::string a;
   std::string b("hello");

   std::cout << "Enter a word to compare: ";
   std::cin >> a;

   if (a == b)
   {
      std::cout << "Equal\n";
   }
   else
   {
      std::cout << "Not equal\n";
   }
}


It's very helpful if you post a Short, Self Contained, Correct (hopefully Compilable), Example (SSCCE). as I did.
http://www.sscce.org/
Another thing to note, there is a newer way to initialize variables, std::string b {"hello"};

This is known as "Uniform Initialization," added in C++11.
https://mbevin.wordpress.com/2012/11/16/uniform-initialization/

There is a very good online and free C++ tutorial you might want to poke around: https://www.learncpp.com/
You can't use C string functions on C++ strings.


Not directly, but can if you .c_str() with them - although using the specific C++ string operations (or std::algorithm functions) are much the better way as George shows above.
> but can if you .c_str() with them

Only if the C++ string does not contain null characters.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
#include <cstring>

int main()
{
    std::string str = "this is a string" ;
    str[7] = 0 ;
    const char* cstr = str.c_str() ;
    const char* substr = "str" ;

    std::cout << "C++: len == " << str.size() << '\n' // 16
              << "  C: len == " << std::strlen(cstr) << '\n' // 7
              << "C++: " << ( str.find(substr) != str.npos ? "found" : "didn't find" ) << " substring '" << substr << "'\n" // found
              << "  C: " << ( std::strstr(cstr,substr) ? "found" : "didn't find" ) << " substring '" << substr << "'\n" ; // didn't find
}

http://coliru.stacked-crooked.com/a/af0de985bcb49969
And "not directly" is why I said the C functions can't (shouldn't) be used on C++ strings. Using c_str() is not exactly a topic for a beginner just starting out IMO.
Last edited on
wiwbiz wrote:
I am coding in C++ in Dev-C 6.3

Dev-C++ in all its various incarnations have problems, including the recent forks by Embarcadero and Red Panda. The IDE won't allow for any use of C++ language standard beyond C++11 even when the underlying compiler is capable of a later language standard.

If you are able get a newer compiler/IDE, there are free ones available that will allow using a later C++ language standard.

Visual Studio 2019/2022 is considered one of the best for Windows, along with Code::Blocks.
https://visualstudio.microsoft.com/downloads/
https://www.codeblocks.org/

If command-line compiling is needed consider MSYS2.
https://www.msys2.org/

If your OS is not Windows there are versions of Code::Blocks available. MacOS and *nix have pre-compiled binaries available.

Another issue with Dev-C++ that can show up when using it on Windows 10 x64. 32-bit apps can get blocked by the OS. They will not run. Not always, but enough times it becomes a PITA. 64-bit versions of the same code have no problems.
that seems off as well.
consider:

a += getch(); //string concat works for individual letters

a[i++] seems like it could go out of bounds here.

also you call getch() twice, that seems wrong, and your condition is wrong.
I think what you want is

1
2
3
4
5
6
char c{};
do
{
    c = getch();
    a+= c;
}while (c != '\r' && c!= '\n') //c++ does not understand your conditions, you have to be explicit.   




note that a can have spaces, b cannot due to cin mechanics.
if you want to allow input of spaces or other whitespace, use getline instead of cin.
Last edited on
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <string>
using namespace std;
int main()
{
   string a, b;
   cout << "Enter a: ";   getline( cin, a );
   cout << "Enter b: ";   getline( cin, b );
   cout << ( a == b ? "Matched" : "Not matched" ) << '\n'; 
}
Two non-standard headers <bits/stdc++h> and <conio.h> Don't use them.

You are using some bastardized Windows I/O (getch), don't use it. Use C++ I/O. std::cin, std::cout.

Using operator[] for string manipulation is decidedly not a good thing to do IMO.

Did you try the code snippet I provided?

I mentioned earlier a free online C++ tutorial you might want to check out: https://www.learncpp.com/

Learning C++ the correct way would be beneficial instead of the mix of C/C++ you are currently having problems with.

Where did you get that code mess?

I don't mean to sound harsh, but this code is not good C++. It looks like some bad C++ rewrite of string handling from C.
I tried it, yes, I tried it. That is why I said it is not good C++ code. IMO sloppy and a royal mess from just reading what was provided and after throwing it into my copy of Dev-C++.

Hint, hint, using the non-standard headers, especially <bits/stdc++h>, won't work with all compilers. Visual Studio, another IDE I have and use, doesn't have that header.

<conio.h> is a Windows only header. Compilers for other OSes, MacOS and *nix, don't have the header. Even if they did the functionality is very specific to Windows.

Well-written C/C++ code should be as platform neutral as possible.

You want to continue writing sub-standard/non-standard code, that is your choice.

Pax, I'm outta here.
wiwbiz wrote:
I don't think a+ or a[i++] are any different, as both are string.

You instantiate your C++ string as being empty.

a[i++] doesn't increase the string's size, it assumes the string is already of the desired size. That is C string logic that will end up writing past the end of the string's memory.

a+= c; concatenates to the end of the C++ string, increasing the string's size with each added character read from the input stream. Still C string logic that is marginally better. Not good C++, though.

Just read your C++ string directly from the input stream either using std::cin::operator>> or std::getline.
https://cplusplus.com/reference/string/string/getline/
Heh, reporting me, wiwbiz, only shows how trollish you are in refusing to accept criticism and advice instead of kissing your ass.
Well the OP has now gone the way of all things and disappeared...
I could say I am surprised or disappointed.

Well, I could, but I shan't.
Topic archived. No new replies allowed.