I'm trying to string

char peremen_t[255]; 

It contains numbers separated by spaces.

 qDebug()<<"peremen_t " << peremen_t; 

Here's what's in it (peremen_t) variable

 peremen_t 1 26 48 1 48000 0.1 

Search

  int tex_massiw_per[100]; // массив переменный куда буду скидывать переменные(по одной) memset(tex_massiw_per, 0, sizeof(char)*255); // обнуляем массив состоящий из 255 элементов int texn_per_nach =0; // технисечкая переменная начало int texn_per_kon =0; // техническая переменная конец int texn_per_f =0; // техническая переменная связанная тс ЭФФФФ int s4et4ik_per_nach = 0; // счетчик который управляет этим дерьмом int wihid = 0; for (size_t d=0;d<sizeof(peremen_t)/sizeof(*peremen_t);d++) { if (s4et4ik_per_nach == 0) { texn_per_nach = d; s4et4ik_per_nach = 1; } if (peremen_t[d] == ' ') { wihid= wihid +1; texn_per_kon = d; s4et4ik_per_nach =0; //----- int schet=0; //присвоение массиву переменной for (int g = texn_per_nach-1; g<texn_per_kon-1;g++) { tex_massiw_per[schet] = peremen_t[g]; schet = schet+1; qDebug() << "tex_massiw_per[schet ="<< schet <<"]: " << tex_massiw_per[schet]; } if(wihid == 6) { qDebug() << "break"; break; } } } qDebug() <<"peremen_t[0]" << peremen_t[0] <<"peremen_t[2],peremen_t[3]" << peremen_t[2] << peremen_t[3]; qDebug() <<"peremen_t[5],peremen_t[6]" << peremen_t[5] << peremen_t[6]; qDebug() <<"peremen_t[8],peremen_t[9]" << peremen_t[8] << peremen_t[9]; qDebug() <<"peremen_t[11]" << peremen_t[11]; qDebug() <<"peremen_t " << peremen_t ; 

Here is the result

 peremen_t 1 26 48 1 48000 0.1 tex_massiw_per[schet = 1 ]: 0 tex_massiw_per[schet = 1 ]: 0 tex_massiw_per[schet = 2 ]: 0 tex_massiw_per[schet = 1 ]: 50 tex_massiw_per[schet = 2 ]: 0 tex_massiw_per[schet = 1 ]: 52 tex_massiw_per[schet = 1 ]: 52 tex_massiw_per[schet = 2 ]: 0 tex_massiw_per[schet = 3 ]: 0 tex_massiw_per[schet = 4 ]: 0 tex_massiw_per[schet = 5 ]: 0 tex_massiw_per[schet = 1 ]: 52 tex_massiw_per[schet = 2 ]: 56 tex_massiw_per[schet = 3 ]: 48 break peremen_t[0] 1 peremen_t[2],peremen_t[3] 2 6 peremen_t[5],peremen_t[6] 4 8 peremen_t[8],peremen_t[9] 1 peremen_t[11] 8 peremen_t 1 26 48 1 48000 0.1 

He counts the computer correctly 6 numbers he noticed. But here are the results, even those that fell out ( tex_massiw_per[schet = 1 ]: 52 tex_massiw_per[schet = 1 ]: 52 ) I do not even have these.

  • 2
    there is a standard atoi and atof , by harder sscanf (mscrt) - nick_n_a
  • What is the problem ?? - Alex.B
  • First, do not confuse but enter the numbers correctly. Enter and immediately on the screen. When input is normal - then you can do next. - nick_n_a

2 answers 2

 QVector<double> charToDouble(const char *_input) { QVector<double> outputVec; QStringList strVec = QString(_input).split(" "); for(int i = 0 ; i < strVec.size(); i++) { outputVec.push_back(strVec.at(i).toDouble()); } return outputVec; } int main(int argc, char *argv[]) { char var[255] = "1 26 48 1 48000 0.1"; QVector<double> vec = charToDouble(var); for(int i = 0 ; i < vec.size(); i++) { qDebug() << vec[i]; } return 0; } 
  • I work in Qt 4.8. I can say that this code does not work (at least for me) - timob256
  • @ timob256 what does "not work" mean? not compiled or the result is not as expected? - Alexander
  • "QVector <double> charToDouble (const char * _input) {" a class is created here and so it is impossible for me (or I just don’t know how to do it) - timob256
  • C: \ Works \ res_glsso1 \ mainwindow.cpp: 1192: Error: C3861: 'charToDouble': identifier not found - timob256
  • @ timob256 you have the wrong code organization. Lay out the full list of files main.cpp, mainwindow.h, mainwindow.cpp - Alexander

Not simpler type

 istrstream in(peremen_t); ... double x; while(in >> x) { // Делать с x что надо } 

Yes, and save in the vector?

Here is the working code:

 int main(int argc, const char * argv[]) { char * peremen_t = "1 26 48 1 48000 0.1"; istrstream in(peremen_t); vector<double>per; double x; while(in >> x) per.push_back(x); for(auto y: per) cout << y << endl; } 
  • I do not have istrstream, I work in Qt 4.8. Alas, I cannot switch to a newer version. - timob256
  • istrstream is a standard C ++ stream and has no relation to Qt ... - Harry
  • #include <istrstream.h> or #include <istrstream>. Green wavy underscore and jumps out a small inscription "strstream: There is no such file or directory." - timob256
  • Why do you recommend obsolete classes? It would be necessary to replace stringstream and <sstream> . - αλεχολυτ
  • @alexolut I use them so rarely that I get confused all the time, which ones are old and which ones are new ... What mnemonics would I come up with? :) - Harry