C、C++中如何实现以一个空行代表输入的结束

版权声明:欢迎评论与转载,转载时请注明出处! https://blog.csdn.net/wjl_zyl_1314/article/details/83691869

首先,我们要用到gets()函数,头文件为<cstdio>
其次就是方法了:

 char s[15];
 while(gets(s))
 {
 if(s[0]=='\0')break;//gets函数如果读取了换行符会将其自动转换成字符串结束符'\0'
 /*
 方法体
 */
 }

测试代码:

#include <cstdio>
using namespace std;
int main()
{
    char s[15];
    while(gets(s))
    {
        if(s[0]=='\0')break;
        printf("%s\n",s);
    }
    printf("Hello world!");
    return 0;
}

运行结果:

//输入abc
//结果:
abc
abc

Hello world!

猜你喜欢

转载自blog.csdn.net/wjl_zyl_1314/article/details/83691869