Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Compare with string and print the value assigned to the letter
QtWS25 Last Chance

Compare with string and print the value assigned to the letter

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 5 Posters 1.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    fender
    wrote on last edited by
    #1
                struct Property
                {
                    QString data3[1];
                    QString properties[1];
                };
                QVector<Property> property;
                property =
                {
                    {{ "М2"}, {"Single thread modification."}},
                    {{ "М"  }, {"Single thread modification."}},
                    {{ " "     }, {"Single thread modification."}},
                    {{ "D"   }, {"Modification for two threads" }},
                };
                for (int i = 0; i < property.size(); i++)
                {
                    if (data3.at(0)==data_[3] || data3.at(1)==data_[3])
                       return description = property.properties(0);
                }
    

    Confused.
    I compare with the string: if there is one of the values, the message Single thread modification or Modification for two threads is displayed.
    Now nothing is displayed

    JonBJ jsulmJ J.HilkJ 3 Replies Last reply
    0
    • F fender
                  struct Property
                  {
                      QString data3[1];
                      QString properties[1];
                  };
                  QVector<Property> property;
                  property =
                  {
                      {{ "М2"}, {"Single thread modification."}},
                      {{ "М"  }, {"Single thread modification."}},
                      {{ " "     }, {"Single thread modification."}},
                      {{ "D"   }, {"Modification for two threads" }},
                  };
                  for (int i = 0; i < property.size(); i++)
                  {
                      if (data3.at(0)==data_[3] || data3.at(1)==data_[3])
                         return description = property.properties(0);
                  }
      

      Confused.
      I compare with the string: if there is one of the values, the message Single thread modification or Modification for two threads is displayed.
      Now nothing is displayed

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by
      #2

      @fender
      Before beginning to decipher your code/address your question: in your struct Property you define the data3 & properties members as arrays of QStrings, each holding just 1 element.

      Is that really what you intend, or should a single Property instance hold just one (of each) data3 & properties QString?

      F 1 Reply Last reply
      2
      • JonBJ JonB

        @fender
        Before beginning to decipher your code/address your question: in your struct Property you define the data3 & properties members as arrays of QStrings, each holding just 1 element.

        Is that really what you intend, or should a single Property instance hold just one (of each) data3 & properties QString?

        F Offline
        F Offline
        fender
        wrote on last edited by
        #3

        @JonB
        That's right, you understand, an array with one element

        Each letter / number from the line has its own decryption
        For example: SAB-A-F-30-1-M2
        Decryption
        A - text
        F - text
        30 - text
        1 - text
        M2 - Single thread modification

        JonBJ 1 Reply Last reply
        0
        • F fender
                      struct Property
                      {
                          QString data3[1];
                          QString properties[1];
                      };
                      QVector<Property> property;
                      property =
                      {
                          {{ "М2"}, {"Single thread modification."}},
                          {{ "М"  }, {"Single thread modification."}},
                          {{ " "     }, {"Single thread modification."}},
                          {{ "D"   }, {"Modification for two threads" }},
                      };
                      for (int i = 0; i < property.size(); i++)
                      {
                          if (data3.at(0)==data_[3] || data3.at(1)==data_[3])
                             return description = property.properties(0);
                      }
          

          Confused.
          I compare with the string: if there is one of the values, the message Single thread modification or Modification for two threads is displayed.
          Now nothing is displayed

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @fender What is data3 and data_?

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • F fender

            @JonB
            That's right, you understand, an array with one element

            Each letter / number from the line has its own decryption
            For example: SAB-A-F-30-1-M2
            Decryption
            A - text
            F - text
            30 - text
            1 - text
            M2 - Single thread modification

            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by JonB
            #5

            @fender

            I admit I am not the world's expert C++-er, but:

            • Where are the declarations/values of all the local variables or member variables you do not show (data3, data_, description)?

            • Following on from that: your data3.at() do not refer to the data3 in your Property/property. Is that really right?

            • What is property.properties(0) supposed to refer to?

            • Are you indeed intending an assignment rather than a comparison in the return result?

            Also, I'm surprised statements like property.properties(0) even compile....

            F 1 Reply Last reply
            0
            • F fender
                          struct Property
                          {
                              QString data3[1];
                              QString properties[1];
                          };
                          QVector<Property> property;
                          property =
                          {
                              {{ "М2"}, {"Single thread modification."}},
                              {{ "М"  }, {"Single thread modification."}},
                              {{ " "     }, {"Single thread modification."}},
                              {{ "D"   }, {"Modification for two threads" }},
                          };
                          for (int i = 0; i < property.size(); i++)
                          {
                              if (data3.at(0)==data_[3] || data3.at(1)==data_[3])
                                 return description = property.properties(0);
                          }
              

              Confused.
              I compare with the string: if there is one of the values, the message Single thread modification or Modification for two threads is displayed.
              Now nothing is displayed

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by J.Hilk
              #6

              hi @fender

              this

              return description = property.properties(0);

              is not a comparison, but an assignment.

              I'm not sure what's supposed to be return bool? QString ? I'm also not sure about the order of execution here, description might be return befor it gets it's new value.


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              JonBJ 1 Reply Last reply
              3
              • J.HilkJ J.Hilk

                hi @fender

                this

                return description = property.properties(0);

                is not a comparison, but an assignment.

                I'm not sure what's supposed to be return bool? QString ? I'm also not sure about the order of execution here, description might be return befor it gets it's new value.

                JonBJ Online
                JonBJ Online
                JonB
                wrote on last edited by
                #7

                @J.Hilk
                In case you're asking: the code as written will return the value assigned to description, which I presume to be QString. And as I said probably not what the OP intended?

                For my edification, could you explain to me how the user;s code compiles:

                QVector<Property> property;
                ...
                return description = property.properties(0);
                

                Even if we ignore the description, what is property.properties(0) at compilation-time??

                J.HilkJ 1 Reply Last reply
                0
                • JonBJ JonB

                  @J.Hilk
                  In case you're asking: the code as written will return the value assigned to description, which I presume to be QString. And as I said probably not what the OP intended?

                  For my edification, could you explain to me how the user;s code compiles:

                  QVector<Property> property;
                  ...
                  return description = property.properties(0);
                  

                  Even if we ignore the description, what is property.properties(0) at compilation-time??

                  J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by
                  #8

                  @JonB
                  I'm a bit lost about that myself,
                  maybe the op has omitted a function declaration in the struct, because neither arrays nor QString support direct access via ().

                  If It's a typo and it's meant to be [] than it would be a QChar or QCharRef


                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  JonBJ 1 Reply Last reply
                  0
                  • J.HilkJ J.Hilk

                    @JonB
                    I'm a bit lost about that myself,
                    maybe the op has omitted a function declaration in the struct, because neither arrays nor QString support direct access via ().

                    If It's a typo and it's meant to be [] than it would be a QChar or QCharRef

                    JonBJ Online
                    JonBJ Online
                    JonB
                    wrote on last edited by JonB
                    #9

                    @J.Hilk

                    maybe the op has omitted a function declaration in the struct

                    My point is that QVector<Property> property makes property a QVector<Property> avector of Propertys, it's not a struct Property. So the property.properties the OP uses has nothing to do with the properties member in struct Property. properties would need to be a member of the QVector, not the struct....

                    1 Reply Last reply
                    1
                    • M Offline
                      M Offline
                      mpergand
                      wrote on last edited by mpergand
                      #10
                      QVector<Property> property;
                                  property =
                                  {
                                      {{ "М2"}, {"Single thread modification."}},
                                      {{ "М"  }, {"Single thread modification."}},
                                      {{ " "     }, {"Single thread modification."}},
                                      {{ "D"   }, {"Modification for two threads" }},
                                  };
                      

                      You try to populate an array of Property struct, but that doesn't work for a container.

                      Populate an array of struct:

                      struct Property
                          {
                              QString data3;
                              QString properties;
                          };
                      Property properties[] =
                          {
                              { "М2", "Single thread modification."},
                              { "М",  "Single thread modification."},
                              { " ",  "Single thread modification."},
                              { "D",  "Modification for two threads" },
                          };
                      

                      Populate a vector:

                      QVector<Property> _properties;
                          _properties<< Property {"М2", "Single thread modification."}
                          << Property { "М",  "Single thread modification."}
                          << Property { " ",  "Single thread modification."}
                          << Property { "D",  "Modification for two threads" };
                      

                      Access to values:

                      // array
                      for (int i = 0; i < sizeof(properties)/sizeof(Property); i++)
                          {
                          qDebug()<<properties[i].data3;
                          }
                      
                      // vector
                      for (Property p : _properties)
                          {
                          qDebug()<<p.data3;
                          }
                      
                      1 Reply Last reply
                      3
                      • JonBJ JonB

                        @fender

                        I admit I am not the world's expert C++-er, but:

                        • Where are the declarations/values of all the local variables or member variables you do not show (data3, data_, description)?

                        • Following on from that: your data3.at() do not refer to the data3 in your Property/property. Is that really right?

                        • What is property.properties(0) supposed to refer to?

                        • Are you indeed intending an assignment rather than a comparison in the return result?

                        Also, I'm surprised statements like property.properties(0) even compile....

                        F Offline
                        F Offline
                        fender
                        wrote on last edited by
                        #11

                        @JonB
                        I have a string

                        QString MainWindow::dataString() const
                        {
                            QString format = "%0%1%2%3%4%5%6%7%8%9%10%11";
                            format
                                    .arg(data_[0])     
                                    .arg(data_[1])     
                                    .arg(data_[2])  
                                    .arg(data_[3])             
                                    .arg(data_[4])            
                                    .arg(data_[5])             
                                    .arg(data_[6])             
                                    .arg(data_[7])             
                                    .arg(data_[8])             
                                    .arg(data_[9])             
                                    .arg(data_[10])            
                                    .arg(data_[11]);          
                            return format;
                        }
                        

                        The idea was as follows: if the date [3] contains "M2", then the following tab displays the decryption "M2 - Single thread modification."
                        So I tried to compare the value from the array with the string and if it matches the output text

                        JonBJ 1 Reply Last reply
                        0
                        • F fender

                          @JonB
                          I have a string

                          QString MainWindow::dataString() const
                          {
                              QString format = "%0%1%2%3%4%5%6%7%8%9%10%11";
                              format
                                      .arg(data_[0])     
                                      .arg(data_[1])     
                                      .arg(data_[2])  
                                      .arg(data_[3])             
                                      .arg(data_[4])            
                                      .arg(data_[5])             
                                      .arg(data_[6])             
                                      .arg(data_[7])             
                                      .arg(data_[8])             
                                      .arg(data_[9])             
                                      .arg(data_[10])            
                                      .arg(data_[11]);          
                              return format;
                          }
                          

                          The idea was as follows: if the date [3] contains "M2", then the following tab displays the decryption "M2 - Single thread modification."
                          So I tried to compare the value from the array with the string and if it matches the output text

                          JonBJ Online
                          JonBJ Online
                          JonB
                          wrote on last edited by
                          #12

                          @fender

                          1. Your original code is so all-over-the-place/uncompilable/unintelligible that it's hard to help.

                          2. Your new code seems to have nothing to do with it.

                          3. Your new code will not work as you might think it does because when you read http://doc.qt.io/qt-5/qstring.html#arg (you did read that, didn't you?) you see that it does not alter your format variable --- which specifies a format --- but rather returns a new QString which is the result of applying the formatting.

                          I don't wish to sound harsh, but you really need to gather your ideas together a bit more carefully, and perhaps look at some other peoples' examples of C++ coding in general if you hope to get anywhere useful.

                          1 Reply Last reply
                          3

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved