std::cout not working inside a for-loop

前端 未结 2 2012
暗喜
暗喜 2021-01-22 12:13

I\'m new to C++, and right now I\'m learning from the book called Accelerated C++. I finished the third chapter (vectors), and I came to this exercise:

\"Write

相关标签:
2条回答
  • 2021-01-22 12:47

    Add << flush to flush your output buffer (each place).

    Or use << endl, which both adds newline and flushes.

    There are problems with the code, especially for empty input, but that's what you're out to learn about, so I'll leave you to it! :-)

    Cheers & hth.,

    0 讨论(0)
  • 2021-01-22 13:04

    I'm afraid the language eludes me in terms of variable names, but this "Works for Me™".

    Here is my output (First 3 lines input:)

    ytreyert
    tyryteter
    gdhdfgdf
    ^Z
    
    test0, test, !test2, test4, test2, test4, ytreyert: 1
    test, !test2, test4, tyryteter: 1
    test5,
    

    You should definitely try flushing the cout buffers after printing (as per Alf's answer).

    I notice that gdhdfgdf is not counted, this is because of this line:

    for (int i = 0; i < duz_recenice - 1; ++i)  
    

    If you only give 1 input word, this loop will not run, as you do duz_recenice = recenica.size(); before looping.

    Changing this line to

    for (int i = 0; i < duz_recenice; ++i)  
    

    solves this problem.

    0 讨论(0)
提交回复
热议问题