N
N
Nik_Haker2015-01-12 20:46:01
Programming
Nik_Haker, 2015-01-12 20:46:01

C++: how to make string variable read 1 character at a time?

The situation is this. Available: Cbuilder(1pc) Edith (1pc) User (~1pc). From Edith, the text goes to a variable of type string. Is it possible to read text from a variable by 1 character (for example, the user entered the expression x + 2 = 4, the program looks - yeah, here is X, it goes further, it looks here plus, it goes further here 2, rewrites the deuce into a variable, etc.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Armenian Radio, 2015-01-12
@gbg

for(const auto&i:str)
{
  //в i посимвольно  попадет строка str
}

T
tsarevfs, 2015-01-12
@tsarevfs

The user may want to correct the text. Therefore, calculations are best done by pressing Enter or a button on the form. Read the entire line and iterate over 1 character in a loop:

void __fastcall TForm1::Edit1KeyDown(TObject *Sender, WORD &Key,
      TShiftState Shift)
{
   if (Key == VK_RETURN)
   {
      string input = Edit1->Text;
      for (size_t i = 0; i < input.length(); ++i)
      {
        //тут делайте с input[i] что хотели
      }
   }
}

But if you really want to, then the handler just receives the Key parameter with the last entered character.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question