I need to input a space, a tab, and a comma from keyboard and then judge if the input is a space, a tab, or a comma. I used the following code but it doesn't work.

 bool ST, COMMA;
 AnsiString HH;
 HH = Edit1->Text;
 strcpy(CR, HH.c_str());

 if (CR == " " || CR == "    ")   //the first CR is a space and the second CR is a tab
 {
     ST = true;
  }
 else if (CR == ",")
 {
     COMMA = true;
 }
 else Edit2->Text = AnsiString("Bad Input!");

Can some one help me out?

Another question is if I use ST[1], and use ST[CR], what should I do for the code?
Thx!

Recommended Answers

All 5 Replies

A space character is ' ' and a tab character is '\t'. But you also have a problem with how you're doing the comparison:

>if (CR == " " || CR == " ")
Because you're using strcpy to fill CR, I can only assume that it's a C-style string. You can't compare C-style strings with the == operator. You have to use strcmp:

if ( strcmp ( CR, " " ) == 0 || strcmp ( CR, "\t" ) == 0 )

But if you just want the first character, it's much easier:

if ( CR[0] == ' ' || CR[0] == '\t' )

Ideally you would get rid of the C-style string entirely and just use HH (your names need work). The == operator is overloaded for that class (assuming std::string):

bool ST, COMMA;
AnsiString HH;
HH = Edit1->Text;

if ( HH == " " || HH == "\t" )
  ST = true;
else if ( HH == "," )
  COMMA = true;
else
  Edit2->Text = AnsiString ( "Bad Input!" );

Or you can just test the first character. Either works

bool ST, COMMA;
AnsiString HH;
HH = Edit1->Text;

if ( HH[0] == ' ' || HH[0] == '\t' )
  ST = true;
else if ( HH[0] == ',' )
  COMMA = true;
else
  Edit2->Text = AnsiString ( "Bad Input!" );

THank you so much!

Very helpful!

For future reference, the reason you can't compare C-style strings with the == operator is because C-style strings aren't a first class type. C-style strings are actually arrays of char with a special convention of ending the "string" with the '\0' character. The simple guideline is that if you can't do it with an array, you can't do it with a C-style string.

Also note that the string literal syntax is really an array behind the scenes. When you say "This is a test", your compiler is creating an array like so:

const char __anon0x12345[] = {
  'T','h','i','s',' ','i','s',' ','a',' ','t','e','s','t','\0'
};

That's confusing because you can do this and it's perfectly legal as well as intuitive:

char my_string[] = "This is a test";

But like the string literal, this initialization syntax (it's not assignment!) is just syntactic sugar for the real work[1] that goes on in the background:

const char __anon0x12345[] = {
  'T','h','i','s',' ','i','s',' ','a',' ','t','e','s','t','\0'
};

char my_string[15];

memcpy ( my_string, __anon0x12345, 15 );

[1] I say real work, but that's just what logically happens. In reality it's optimized into a compile-time operation.

Thanks again! This makes me clearer.

If I want to use a function to implement the boolean operation, can I directly pass HH to the function? What should I do? Some like like:

bool ST(const string& HH)?

>bool ST(const string& HH)?
That's perfect (though I still don't like your variable names).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.