RE: [Dev-C++] I am new to C ++ and need some help
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Nick <en...@ho...> - 2000-12-12 07:48:37
|
Hello,
> In fact the following stuff can be used in C++ only.
>
> char s1[20], s2[20];
> ...
>
> if(s1==s2)
> ...
>
> The overloaded string operator == accepts 2 strings (objects) and the
> C-style strings are "upgraded" to temporary strings so as the
> comparison to work.
You can use == as long as one of the arguments is a (C++) string
object. In your example above, it does not use the string operator.
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int main(){
char temp1[20], temp2[20];
string temp3;
strcpy(temp1,"John");
strcpy(temp2,"John");
temp3 = "John";
if (temp1 == temp2) cout << "temp1 == temp2" << endl;
else cout << "temp1 != temp2" << endl;
if (temp3 == temp1) cout << "temp3 == temp1" << endl;
else cout << "temp3 != temp1" << endl;
if (temp2 == temp3) cout << "temp2 == temp3" << endl;
else cout << "temp2 != temp3" << endl;
system("pause");
return(0);
}
> But the better is string s1, s2; to be used instead (including the
> <string>).
Agreed.
--
regards,
Nick
|