C++ CIN cin skips randomly

后端 未结 1 724
刺人心
刺人心 2021-01-21 22:15

I have this program, but cin in randomly skips.. I mean sometimes it does, and sometimes it doesn\'t. Any ideas how to fix this?

    int main(){ 


        /** g         


        
1条回答
  •  不思量自难忘°
    2021-01-21 23:14

    I would guess that some of your inputs have spaces in them, which the >> operator treats as the end of a particular input item. The iostreams >> operator is really not designed for interactive input, particularly for strings - you should consider using getline() instead.

    Also, you are needlessly using dynamic allocation:

    assignment = new Assignment(assignment_name);
    

    would much better be written as:

    Assignment assignment(assignment_name);
    

    you should avoid the use of 'new' in your code wherever possible, and instead let the compiler take care of object lifetimes for you.

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