std::cout not working inside a for-loop

前端 未结 2 2036
暗喜
暗喜 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 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.

提交回复
热议问题