将字符值输入int数组? [英] Inputting a character value into an int array?

查看:192
本文介绍了将字符值输入int数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我写的程序:

    #include<iostream>
    using namespace std;

    int main()
    {
        int hex[16];
        cout << "enter the 16 hexadecimal numbers\n";
        for(int i = 0;i < 16;i++)
        {
            cin >> hex[i];
            cout << "input worked\n";
        }
    }

您可以向我解释为什么输入工作输出在我输入'a'后再次显示。
如果我这样做,程序不应该停止?

Can you please explain to me why the "input worked" output is again being displayed after I inputted 'a'. Shouldn't the program stop if I did that?

我的意思是为什么输入工作线输出后cin停止接受输入?

What I mean is why does "input worked" line output after cin stopped taking input?I mean if there is a problem in taking input from cin why is cout still working?

推荐答案

你必须通过std :: hex ,否则它只接受一个整数,并且输入流在内部切换到错误状态,并且跳过下一次迭代的输入。这就是为什么你看到剩余迭代的输出。

You must pipe it through std::hex, otherwise it accepts just an integer and your input stream switches internally to an error state and the inputs of the next iteration are skipped. That's why you see the outputs of the remaining iterations.

cin >> std::hex >> hex[i];

这篇关于将字符值输入int数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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