Loops [solved]



  • I'm not really working on any apps, but just thinking to myself, I cannot seem to grasp these loops, In a way, I find them very confusing, and I have no clue why, it could be due to my AD/HD, but I'm not blaming it on that, because AD/HD can be controlled, here's an example of what I'm thinking about.

    [code]

    my_var[4] = { "Var1", "Var2", "Var3", "Var4"};

    function::function()
    {
    for (i =0; i < (my_var); i++) {

    /* this is where I"m getting confused at
    It looks incomplete, maybe even invalid, but I'm not very sure.
    */
    };

    [/code]

    /* I have read over the loops stuff, and a friend has been helping to, in fact, he taught me the my_var [ 4 ] = { }; part. He also taught me, the for loop part goes in a function. I'd just ask him, but he's not online right now, and probably getting annoyed with me, LOL.. I think once I get this loop stuff conquered, everything else will be a sinch. Oh, he also taught me that you can use while (0) { } as a comment, I never known this.*/

    Thanks in advance, =).



  • But... what's exactly the question? What are you trying to achieve?



  • [quote author="peppe" date="1315155068"]But... what's exactly the question? What are you trying to achieve?[/quote]

    Just to get those 4 variables to display in a QComboBox.

    [code]

    QComboBox *comboBox;

    [/code]

    That's all, like I said, I'm confused here, and can't really answer your question, sorry.


  • Moderators

    from what I gather, what you need is a basic book about programming. Doesn't even have to be on C++.

    Some more code for you:
    @
    QComboBox *comboBox;
    comboBox = new QComboBox();

    for (i = 0; i < 4; i++) {
    comboBox.addItem(my_var[i]);
    }
    @
    Wrote quite quickly without thinking, might contain nuts!



  • Another possibility would be:

    @
    my_var[4] = { "Var1", "Var2", "Var3", "Var4"};

    QComboBox *comboBox;
    comboBox = new QComboBox();

    for (i = 0; i < sizeof(my_var); i++) {
    comboBox.addItem(my_var[i]);
    }
    @

    sizeof (array variable) returns the length of the array, if the array is defined as fixed size ;-)


  • Moderators

    [quote author="Gerolf" date="1315157192"]
    sizeof (array variable) returns the length of the array, if the array is defined as fixed size ;-)[/quote]

    Well, yes, it does ;) Was actually thinking about using it, but - strangely - decided not to.



  • [quote author="Gerolf" date="1315157192"]
    sizeof (array variable) returns the length of the array, if the array is defined as fixed size ;-)[/quote]

    No. It returns the size (in "char" units) of the array. sizeof(array)/sizeof(element) is what you want.



  • Gerolf, I think it's a little bit confusing.

    sizeof, as defined by the standard yields the number in bytes of its operand (since it's an unary operator).

    First,

    @
    my_var[4] = { "Var1", "Var2", "Var3", "Var4"};
    @

    lacks a type. This should be :

    @
    const char *arr[] = { "Var1", "Var2", "Var3", "Var4"};
    @

    Then, @sizeof arr@ will yield the total number of bytes for arr representation. It won't give you the length of the array. Thus, if you need the length, you'll need to divide by the size of its element :

    @
    for (std::size_t i = 0; i < sizeof my_var / sizeof *my_var; i++) {
    qDebug() << my_var[i];
    }
    @

    and notice that the result of sizeof is a constant of type std::size_t which is defined in the cstddef header



  • LOL, thanks for all the posts, this is great, =). And now I think it's starting to come back to me now, I did this one a long time ago, but it was just once, so my memory wasn't kept up on it.. I do remember using the

    [code]

     for (i = 0; i < (my_var); I++) {
           comboBox->addItem(my_var[i]);
    

    }

    [/code]

    part, and it worked, this is considered solved, =). Some of that other stuff went over my head, hehe.. The const char *arr[] = { }; part, hehehe..


  • Moderators

    octal - true, I've just checked on a live program. Been using QList and such for too long, as it would seem :)



  • I'm just getting tired of doing Text Editor after Text Editor, which honestly, isn't getting anywhere, I'm trying to improve my Qt Knowledge some, learn different things. Like, because of this loop issue, I was trying to learn how to make a QProgressBar increase in % when you used the QSlider Object, this might be a little more advanced looping feature, or am I wrong? I set ScrollBar as 0% default setting, and 100% at maximum setting. But, I've since deleted that project, just because I wasn't really getting anywhere with it. I type my apps from ground up, surprised I can remember that much, but I can, it's amazing at what we can remember, =P. Eventually I'll have to learn even more features of Qt, it's a pretty good TK, I showed a friend it, and he couldn't believe how much Qt Supports, from C to C++ to Python, he tried to run the old text editor I had, but kept getting errors he said, he thinks it's he Qt installation.. But anyway, thanks for the responses, =).



  • Yes, Qt is an amazing toolkit, but there is no secret, before using it, you need some basic C++ knowledge.

    I then suggest you to refresh your memory with some C++ basics before going on :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.