Re: beginner string question

From: Catalin Pitis (catalin.pitis.removeme_at_iquestint.com)
Date: 11/12/03


Date: Wed, 12 Nov 2003 10:30:02 +0200

Hi,

see below

"Sonoman" <fcarpio@cse.fau.edu> wrote in message
news:Skksb.11179$jA6.6383@bignews6.bellsouth.net...
> I am trying to do this:
>
> cin >> temp;
> if (temp == "n"){
> Then do something...
> }
>
> temp was declared as a string and the input I give at the prompt is n, but
> it skips the condition for the if statement when I think it should go into
> the if statement. I tried swithching to 'n' for the condition but it gave
me
> a compile error. The complete code is here:
>
> http://www.cse.fau.edu/~fcarpio/
>

Couldn't find the code you mentioned.

> and the offending lines are in the person.cpp file line 66. I know it must
> be something simple, but I am failing to see it. There is no compiler
error.
>

Use std::string instead of plain C-style strings. If you declared temp
something as:

char temp[ SOME_LEN];

then temp == "n" will compare two pointers instead of two strings. The
result of comparing will be always false.

If you use std::string like:

std::string temp;

then the operator== is overloaded and it will compare the two strings.

Catalin



Relevant Pages

  • Re: beginner string question
    ... Sorry but I gave the wrong info, I declared "temp" as a character array if ... I did use strings but they were breaking up in ... > a compile error. ...
    (comp.lang.cpp)
  • Re: String() declaration?
    ... the temp array is holding strings ... Dim temp as String ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Problem with this text-sorting algorithm
    ... I need to write an algorithm which will sort an array of text strings ... I'd done the sort. ... Dim temp As String ...
    (microsoft.public.vb.general.discussion)
  • Re: humidifiers
    ... So if the temp goes up a couple of degrees, the strings will expand, ... I don't see your pitch test as diagnostic of anything. ...
    (alt.guitar.beginner)
  • Re: Some optimization tale
    ... >> (and that the right algorithm makes indeed a difference). ... >Since Python does not have pointers, comparing characters within strings ... >requires pulling out the chars as separate strings. ... >truncate prefix to the length of item first. ...
    (comp.lang.python)