How to read all the numbers indicated with a space in the line Edit1 in C ++ Builder?

My code is:

AnsiString str, ap; int mas[1000],i,len; str = Edit1->Text; len = str.Length(); for(i=1;i<=len;i++){ ap = str.Pos(' ')-1; mas[i] = StrToInt(str.SubString(1,StrToInt(ap))); str.Delete(1,StrToInt(str.Pos(' '))); len= str.Length() ; } 

But it works crookedly. Tell me the method of solving the problem

  • What is the "curvature"? - Athari
  • @Vlad And the indices in AnsiString start with 1 or still with 0? And why use the expression ap = str.Pos ('') -1 ;? - Vlad from Moscow
  • the curvature was that the software went out in time after reading the last value - Vlad
  • @Vlad from Moscow oddly enough but the substring starts from 0 and delete from 1 - Vlad
  • substring, delete, all index starts at 1 - Alexcei Shmakov

2 answers 2

Try this. It should work. At hand there is no builder, I can be mistaken.

 AnsiString str = Edit1->Text; int mas[1000]; int posWhite; int index = 0; for(int i = 0; i < str.Length(); index++) { // posWhite - позиция символа, где встретился пробел(индекс начинается с 1) posWhite = str.Pos(' '); // проверка, найден ли пробел if(posWhite > 0) { mas[index] = StrToInt(str.SubString(1, posWhite - 1)); str.Delete(1, posWhite)); // увеличиваем счетчик количества обработанных символов в строке(не забываем по пробел) i += posWhite; } else { // делаем вывод, что все что осталось в строке, это число mas[index] = StrToInt(str); i += str.Length(); } } 

    Found a solution to the problem

     int i,j,k,m,n,l,a[1000]; str=Edit1->Text; str=str+" "; l=str.Length(); k=0; for(i=1;i<=l;i++){ j = str.Pos(' ')-1; a[i-1]=StrToInt(str.SubString(1,j)); str.Delete(1,j+1); l-=j; k++; } 
    • one
      Why do we need the variable k? - Alexcei Shmakov