none
for loop usage, about the element in side for ( ) RRS feed

  • Question

  • ofstream loc_outfile("uadata/loc.txt");

    for (int i = 0; i < *amount + 1; i++) { loc_outfile << loc[i].code << endl; }

    loc_outfile.close();


    is not equivalent to

    ofstream loc_outfile("uadata/loc.txt");

    for (int i = 0; i < *amount + 1; i++, loc_outfile << loc[i].code << endl;) ;

    loc_outfile.close();


    ???????

    The First one , i = 0 and print the loc[0].code to the loc_outfile  (which is a .txt)

    assume loc[0].code = CSX001

    then the txt file shows CSX001

    but the second one, i have test for that and it shows that the loc[-7952].code  , the number inside [ ] is not i =0

    and that shit shows 日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日


    Sunday, March 27, 2016 1:14 PM

Answers

  • On 3/27/2016 9:14 AM, Leung Daniel wrote:

    ofstream loc_outfile("uadata/loc.txt");for (int i = 0; i < *amount + 1; i++) {
    loc_outfile << loc[i].code << endl;
    }loc_outfile.close();


    is not equivalent to

    ofstream loc_outfile("uadata/loc.txt");for (int i = 0; i < *amount + 1; i++, loc_outfile << loc[i].code << endl;) ;loc_outfile.close();

    It would be equivalent if you print first, and increment afterwards.

    • Marked as answer by Leung Daniel Sunday, March 27, 2016 8:22 PM
    Sunday, March 27, 2016 2:12 PM

All replies

  • Hello,

     Quick question for you... Why are you derefernced object amount to get the value and then

    adding 1 to it.  If "amount" is not a pointer object then just use "amount + 1" in the for loop.

    Thanks. :)


    • Edited by User3DX Sunday, March 27, 2016 3:04 PM Clarity
    Sunday, March 27, 2016 1:38 PM
  • On 3/27/2016 9:14 AM, Leung Daniel wrote:

    ofstream loc_outfile("uadata/loc.txt");for (int i = 0; i < *amount + 1; i++) {
    loc_outfile << loc[i].code << endl;
    }loc_outfile.close();


    is not equivalent to

    ofstream loc_outfile("uadata/loc.txt");for (int i = 0; i < *amount + 1; i++, loc_outfile << loc[i].code << endl;) ;loc_outfile.close();

    Of course. In the second case, the part that does the printing never sees i == 0, but does see i == *amount + 1 (which is probably out of bounds).

    Sunday, March 27, 2016 1:47 PM
  • ofstream loc_outfile("uadata/loc.txt");

    for (int i = 0; i < *amount + 1; i++) { loc_outfile << loc[i].code << endl; }

    loc_outfile.close();


    is not equivalent to

    ofstream loc_outfile("uadata/loc.txt");

    for (int i = 0; i < *amount + 1; i++, loc_outfile << loc[i].code << endl;) ;

    loc_outfile.close();


    ???????

    The First one , i = 0 and print the loc[0].code to the loc_outfile  (which is a .txt)

    assume loc[0].code = CSX001

    then the txt file shows CSX001

    but the second one, i have test for that and it shows that the loc[-7952].code  , the number inside [ ] is not i =0

    and that shit shows 日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日



    The third part of the 'for' statement is not executed the first time through the loop. Why are you trying to use the second form of your code?

    David Wilkinson | Visual C++ MVP

    Sunday, March 27, 2016 1:53 PM
  • On 3/27/2016 9:14 AM, Leung Daniel wrote:

    ofstream loc_outfile("uadata/loc.txt");for (int i = 0; i < *amount + 1; i++) {
    loc_outfile << loc[i].code << endl;
    }loc_outfile.close();


    is not equivalent to

    ofstream loc_outfile("uadata/loc.txt");for (int i = 0; i < *amount + 1; i++, loc_outfile << loc[i].code << endl;) ;loc_outfile.close();

    It would be equivalent if you print first, and increment afterwards.

    • Marked as answer by Leung Daniel Sunday, March 27, 2016 8:22 PM
    Sunday, March 27, 2016 2:12 PM