String not being printed by C++

后端 未结 4 879
自闭症患者
自闭症患者 2021-01-16 05:28

Sorry for the noob question, I\'m a newbie programmer and transitioning from C to C++. I could easily write a program to reverse a string in C the same way with minor change

4条回答
  •  情歌与酒
    2021-01-16 06:13

    string p; doesn't have enough allocated space for directly accessing by something like p[j]

    You can change to initialize p from copying s like below, your code will work.

    string s;
    getline(cin,s);
    string p(s);  // p will be allocated and be the same as s
    

提交回复
热议问题