从用户获取输入使用cin [英] Getting input from user using cin

查看:124
本文介绍了从用户获取输入使用cin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Turbo C ++ 3.0编译器

I am using Turbo C++ 3.0 Compiler

使用下列代码..

char *Name;
cin >> Name;
cout << Name;

当我给输入空格时...它在空格前输入的唯一的保存字符
喜欢如果我给输入QWERT YUIOP...名称将包含QWERT;

When I gave input with space ... its only saving characters typed before space .. like if I gave input "QWERT YUIOP" ... Name will contain "QWERT";

任何解释为什么??

推荐答案

为要读取的名称的字符数组。 char * Name; 将无法工作,因为它只声明一个字符指针不是一个字符数组。 char Name [30];

You need to allocate space for the char array into which you want to read the Name. char *Name; will not work as it only declares a char pointer not a char array. Something like char Name[30];

cin<< / code>只允许我们在一个字符串中输入一个单词(char name [30])。

然而,有一个cin函数读取包含空格的文本。

Also the cin << only allows us to enter one word into a string (char name[30]).
However, there is a cin function that reads text containing blanks.

cin.get(name, MAX)  

get将读取所有包含空格的字符,直到Max字符已读取
或者到达行尾字符('\\\
'),并将
添加到name变量中。

get will read all characters including spaces until Max characters have been read or the end of line character (‘\n’) is reached and will put them into the name variable.

这篇关于从用户获取输入使用cin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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