Click to See Complete Forum and Search --> : Easy Problem ? I hope !


Paul
04-10-2001, 08:44 AM
Hi guys,

You've all been a great help so far and I'm making some serious headway with
my code now, there is just one small question. When I'm doing compares like
:

if (string1 != string2)

If the variables have been defined like :

char string1[] = "a"
char string2[] = "b"

It throws up an error during compilation (something along the lines of cannot
convert something or other

However........ if I define them like this :

char string1;
char string2;

And then ask the user to define the value of the through a "CIN>>" - the
comparison works fine ! I just don't get it ! My program works OK so far,
but I don't really want the user to have to type in certain things, I want
them 'ready specified' in the soure if you get what I mean ?!

Help please !!!

Regards

Paul

David
04-10-2001, 11:23 AM
I've tried simulating your error using Microsoft Visual C++ 6.0 but my code
seems to work fine. This is what I used:

#include <iostream>

using namespace std;

int main()
{
char string1[] = "a";
char string2[] = "b";

if (string1 != string2)
cout << "Not equal" << endl;

return 0;
}

What I have here compiled and ran fine. Hope this helped.

David

"Paul" <paul_s_b@hotmail.com> wrote:
>
>Hi guys,
>
>You've all been a great help so far and I'm making some serious headway
with
>my code now, there is just one small question. When I'm doing compares
like
>:
>
>if (string1 != string2)
>
>If the variables have been defined like :
>
>char string1[] = "a"
>char string2[] = "b"
>
>It throws up an error during compilation (something along the lines of cannot
>convert something or other
>
>However........ if I define them like this :
>
>char string1;
>char string2;
>
>And then ask the user to define the value of the through a "CIN>>" - the
>comparison works fine ! I just don't get it ! My program works OK so far,
>but I don't really want the user to have to type in certain things, I want
>them 'ready specified' in the soure if you get what I mean ?!
>
>Help please !!!
>
>Regards
>
>Paul
>
>

Urs
04-10-2001, 02:03 PM
use the strcmp-function which is in the string.h.

if(strcmp(s1,s2))
cout << "not equal";

by the way, the string class makes strings a lot
easier with C++ :)

hope this helped, Urs


"David" <David.B.Swanson@wcom.com> wrote:
>
>I've tried simulating your error using Microsoft Visual C++ 6.0 but my code
>seems to work fine. This is what I used:
>
>#include <iostream>
>
>using namespace std;
>
>int main()
>{
> char string1[] = "a";
> char string2[] = "b";
>
> if (string1 != string2)
> cout << "Not equal" << endl;
>
> return 0;
>}
>
>What I have here compiled and ran fine. Hope this helped.
>
>David
>
>"Paul" <paul_s_b@hotmail.com> wrote:
>>
>>Hi guys,
>>
>>You've all been a great help so far and I'm making some serious headway
>with
>>my code now, there is just one small question. When I'm doing compares
>like
>>:
>>
>>if (string1 != string2)
>>
>>If the variables have been defined like :
>>
>>char string1[] = "a"
>>char string2[] = "b"
>>
>>It throws up an error during compilation (something along the lines of
cannot
>>convert something or other
>>
>>However........ if I define them like this :
>>
>>char string1;
>>char string2;
>>
>>And then ask the user to define the value of the through a "CIN>>" - the
>>comparison works fine ! I just don't get it ! My program works OK so far,
>>but I don't really want the user to have to type in certain things, I want
>>them 'ready specified' in the soure if you get what I mean ?!
>>
>>Help please !!!
>>
>>Regards
>>
>>Paul
>>
>>
>

Danny Kalev
04-10-2001, 04:10 PM
David wrote:
>
> I've tried simulating your error using Microsoft Visual C++ 6.0 but my code
> seems to work fine. This is what I used:
>
> #include <iostream>
>
> using namespace std;
>
> int main()
> {
> char string1[] = "a";
> char string2[] = "b";
>
> if (string1 != string2)

this works indeed, but it does a very different thing: the comparison
compares the POINTERS, nit the strings themselves. To compare C-strings
you have to use the strcmp() function.

Danny
> cout << "Not equal" << endl;
>
> return 0;
> }
>
> What I have here compiled and ran fine. Hope this helped.
>
> David
>
> "Paul" <paul_s_b@hotmail.com> wrote:
> >
> >Hi guys,
> >
> >You've all been a great help so far and I'm making some serious headway
> with
> >my code now, there is just one small question. When I'm doing compares
> like
> >:
> >
> >if (string1 != string2)
> >
> >If the variables have been defined like :
> >
> >char string1[] = "a"
> >char string2[] = "b"
> >
> >It throws up an error during compilation (something along the lines of cannot
> >convert something or other
> >
> >However........ if I define them like this :
> >
> >char string1;
> >char string2;
> >
> >And then ask the user to define the value of the through a "CIN>>" - the
> >comparison works fine ! I just don't get it ! My program works OK so far,
> >but I don't really want the user to have to type in certain things, I want
> >them 'ready specified' in the soure if you get what I mean ?!
> >
> >Help please !!!
> >
> >Regards
> >
> >Paul
> >
> >