-1

I want my std::cout to print 000111222333 ..., but what i'm getting instead is 0111222333 ..., and i don't know why, here is my code:

int threeCounter =1;
int outPut =0;
for (int i =0; i<300;i++){

    if(threeCounter > 3 ){
        outPut++ ;
        threeCounter=1;

    }

    cout << outPut << endl;
    threeCounter++;

}

I tried the very same code in matlab, and it's producing the correct sequence:

threeCounter=1;
outPut =0;
for i =1:300

    if(threeCounter >3)

        outPut=outPut+1;
        threeCounter=1;

    end
    disp(outPut)
    threeCounter=threeCounter+1;

end
Javia1492
  • 862
  • 11
  • 28
Apastrix
  • 114
  • 2
  • 12
  • The "very same" code? It´s not. And stop thinking that all languages have same semantics. – deviantfan May 15 '15 at 20:05
  • 1
    I cannot reproduce your error. – Beta May 15 '15 at 20:05
  • 3
    The leading zeros of numbers may not get displayed. See the `fill` I/O manipulator. – Thomas Matthews May 15 '15 at 20:06
  • 1
    all correct: http://ideone.com/8evkQd –  May 15 '15 at 20:07
  • @Thomas Matthews, i think this is it, it was so confusing. – Apastrix May 15 '15 at 20:15
  • If you change your printout to before the `if` condition, everything then works as intended except you do not print the third `99`, so you're left with `98 98 98 99 99`. This makes sense since it will not iterate the loop the third time, but may be a clue as to why you do not get the initial `0`. – Javia1492 May 15 '15 at 20:19
  • @Apastrix Did the code in answer solve your problem? – shruti1810 May 15 '15 at 20:20
  • @ shruti1810 No, it didn't compile, an error about to_string poped out, when i looked it out, i found that it's a known bug for MinGW to not take to_string as a member of std, Anyway, the code i posted was correct, the display via cout was omitting the first zeros, so thanks anyway! – Apastrix May 15 '15 at 20:28

1 Answers1

0

Try this code in C++:

#include <iostream>
#include <string>
using namespace std;

int main()
{
for (int i =0; i<300;i++){
   string outPut = "";
   int threeCounter =0;
    while(threeCounter < 3 ){
        outPut = outPut+to_string(i) ;
        threeCounter+=1;

    }

    cout << outPut;
    threeCounter++;

}

}
shruti1810
  • 3,920
  • 2
  • 16
  • 28