C++——判断字符串是否为空的一个方法

if(string[0]!='\0') return 1;

正常字符串以'\0'作为字符串结尾的标志符号

 

 

代码实例:

int setgolf(golf &g)

{

    cout<<"please input the player's name:";

    cin.get(g.fullname,Len);

    if(g.fullname[0]=='\0') return 0;

    cout<<"please input the handicap for "<<g.fullname<<" :";

    cin>>g.handicap;

    cin.get();

    return 1;

}

注意上述代码以回车空输入作为结束输入的判断,所以须在代码尾部加上

cin.get();以吃掉回车符,同时可以使用cin.get(g.fullname,Len);来接收一行字符串


版权声明:本文为m0_37965811原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。