C ++,'\0'的用户输入检查在空格处停止? [英] C++, user input check for '\0' stops at spaces?

查看:251
本文介绍了C ++,'\0'的用户输入检查在空格处停止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果用户输入包含空格的句子,则while循环停在这些空格之一。为什么会发生这种情况? '\0'和一个空格相同或者我做了别的错误?

If the user inputs a sentence containing spaces the while loop stops at one of these spaces. Why is this happening? are '\0' and a space the same or did i do something else wrong?

int main ( )
{
    char user_input[200];
    cin>>user_input;
    int i=0;
    while(user_input[i]!='\0')
    {
        i++;
    }
    cout<<i;
    return 1;

}

感谢大家,感谢您的帮助。

Thanks everyone, I appreciate your help.

推荐答案

\0 是使用ASCII代码的空终止字符 0
空格是另一个用ASCII 32的字符。
其实你是这样做的。

\0 is the null terminating character with ASCII code 0. Space is another character with ASCII 32 i suppose. In fact you are doing this.

cin >> user_input;

它需要输入,直到你按空格或输入。因此,您的 user_input 字符串中不存在空格。
使用此代替 cin

It takes input till you press space or enter. So no space is present in your user_input string. Use this instead of cin.

cin.getline (user_input, 200, '\0') ;

这篇关于C ++,'\0'的用户输入检查在空格处停止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆