将字符存储到int变量中

发布于 2025-01-21 23:23:34 字数 1122 浏览 2 评论 0原文

我正在学习C ++中的隐式转换。我阅读了以下示例:

char a;
std::cin>>a; //I can enter an integer like 56 here
std::cout<<a<<std::endl; //for the input 56 it will display 5 because of its ASCII value

我通过在有关的不同书籍和帖子中阅读上述示例来理解上述示例。例如,如果我提供输入j,则该程序在控制台上成功打印j。同样,如果我提供输入说56,则由于其ASCII值,输出将为5


但是随后我尝试了相反的内容,如下所示:

int a;
std::cin>>a;//if i provide the input as the character J then why is the output 0 instead of the corresponding code point of `J`
std::cout<<a<<std::endl;

对于上述片段,如果我提供输入56,则将输出正确打印为56。但是,如果我提供输入为j,则 output 0 。

因此,我的问题是在上面的第二个片段中,为什么未打印与字符j相对应的代码点,而是我们在控制台上打印了0。我的意思是,a是一个整数变量,因此它能够存储与字符j相对应的代码点,然后当我们进行cout&lt;&lt; a;我们应该将该代码点作为输出而不是0。这里发生了什么。这是否与隐式转换有关,例如char可以促进到int或其他内容。

I am learning about implicit conversions in C++. And i read the following example:

char a;
std::cin>>a; //I can enter an integer like 56 here
std::cout<<a<<std::endl; //for the input 56 it will display 5 because of its ASCII value

I understood the above example by reading about it in different books and posts on SO. For example, if i provide the input J, then the program successfully prints J on the console. Similarly if i provide the input say 56 then the output will be 5 because of its ASCII value.


But then i tried the opposite as shown below:

int a;
std::cin>>a;//if i provide the input as the character J then why is the output 0 instead of the corresponding code point of `J`
std::cout<<a<<std::endl;

For the above snippet, if i provide the input 56 then the output is correctly printed as 56. But if i provide the input as J then the output is 0.

So my question is in the above 2nd snippet why the code point corresponding to the character J is not printed and instead we get 0 printed on the console. I mean, a is an integer variable so it is able to store the code point corresponding to the character J and then when we do cout<<a; we should be getting that code point as the output instead of 0. What is happening here. Is this related to implicit conversion like a char can be promoted to an int or something else.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

愛放△進行李 2025-01-28 23:23:34

User4581301的评论指向正确的方向。

为了了解正在发生的事情,需要更好地了解提取操作员(操作员&gt;)。

当分配给算术类型时,只要字符的序列可以将其完全解释为该类型的值,则输入是按字符解析的。如果不再有可能这样做,则停止的停止和直到使用该点的有效值。

因此,当仅输入一个字符时,毫无疑问,字符的ASCII值被分配给整数变量时。相反,这是一个失败,可以在std :: num_get&lt; Chart,Inputit&gt; :: get链接下列出的链接中找到以下句子。

如果转换功能无法转换整个字段,
值0存储在v。

以下是对此主题有广泛了解的好地方:

std :: cin和处理无效输入

std :: istream ::操作员&gt;

为了更加认真的理解,以下链接很有用:

操作

员“ rel =“ nofollow noreferrer”> std :: basic_istream&lt; traits&gt; ::操作员&gt;&gt;

std :: num_get&lt; Chart,inputit&gt; :: get,std :: num_get&lt; chart,inputit&gt; :: do_get

user4581301's comment points in the right direction.

One needs to understand better about the extraction operator (operator>>) in order to understand what is going on.

When assigning to an arithmetic type, the input is parsed character by character as long as the sequence of characters can be exactly interpreted as a value of that type. When it is no longer possible to do so, the parsing stops and the valid value that has been obtained till that point is used.

Thus there is no question of implicit conversion when just a character is entered and that of the character's ASCII value being assigned to an integer variable. Rather it is a failure and one can find the following sentence in the std::num_get<CharT,InputIt>::get link listed below.

If the conversion function fails to convert the entire field, the
value ​0​ is stored in v.

The following are good places to get a broad understanding of this topic:

std::cin and handling invalid input

std::istream::operator>>

For a much more serious understanding, the following links are useful:

operator>>(std::basic_istream)

std::basic_istream<CharT,Traits>::operator>>

std::num_get<CharT,InputIt>::get, std::num_get<CharT,InputIt>::do_get

柠檬 2025-01-28 23:23:34

&lt;&lt;操作员的工作不同,具体取决于右侧的东西。在第一个示例中,它以ASCII值为53读取字符“ 5”,并将“ 6”抛出。在第二种情况下,它读取INT五十六。当您输入“ J”不是INT时,因此尝试阅读int的结果为0

。操作员执行相同的操作:当您输出“ 5”时,它是编写的,将其编写为char,而INT 56则写为int。

The << operator works different depending on what is on the right hand side. In the first example it reads the char ‘5’ with a ASCII value of 53 and throws the ‘6’ away. In the second case it reads the int fifty-six. When you enter “J” that’s not an int, so trying to read an Int gets a result of 0.

And the >> operator does the same thing: When you output ‘5’ it’s written, it is written as a char, and the int 56 is written as an Int.

短叹 2025-01-28 23:23:34

istream操作员&lt;&lt;期望有些东西,但会得到其他东西,其failbit已设置,在此状态下,流将不会做任何事情。
因此,如果有链条,当满足错误的输入时,它会停止。
关于处理此在这里

但是还有其他事情正在发生。
由于C ++ 11,如果提取失败,则将零写入其值。

int a; // default-initializing fundamental type (no init / garbage value)
std::cin >> a;

如果遇到char,a将变为0。

对于整数,如果输入的值或多或少超过类型可以保持的,则设置了failbit已设置成为std :: numeric_limits&lt; t&gt; :: max/min()max(max()如果T未签名。

When istream operator<< expects something but gets something else, its failbit is set and in this state, the stream will not do anything.
So if there is a chain, it is stopped when a wrong input is met.
About handling this here.

But something else is also happening.
Since C++11, if extraction fails, zero is written to its value.

int a; // default-initializing fundamental type (no init / garbage value)
std::cin >> a;

If a char is encountered, a will become 0.

And also for integers, if the entered value is more or less than the type can hold, failbit is set and it will become std::numeric_limits<T>::max/min() or max() if T is unsigned.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文