2

This is the program that I wrote:

    #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";
        }
    }

enter image description here

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?

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?

  • 1
    The character `a` is not an `int` and you are trying to extract an `int`. This creates an *error* in the input `cin` and it stops working. – Galik Sep 19 '15 at 09:59

4 Answers4

2

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];
1

You should write

if(cin >> hex >> hex[i]) {
    cout << "input worked\n";
}
else {
    string dummy;
    cin.clear();
    cin >> dummy;
}

to determine if input worked. For the hex IO manipulator you must #include <iomanip>.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
1

By default input streams like std::cin expect integer input to be in decimal (characters '0'-'9'). If you want to input in hexadecimal you need to use std::hex:

#include <iostream>
#include <iomanip>
using namespace std;

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

Also you should always check the stream for errors after reading from it.

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int main()
{
    int hex[16];
    cout << "enter the 16 hexadecimal numbers\n";
    for(int i = 0;i < 16;i++)
    {
        if(cin >> std::hex >> hex[i])
            cout << "input worked\n";
        else
        {
            cout << "bad input try again\n";
            cin.clear(); // remove the error
            std::string skip;
            cin >> skip; // read past bad input
        }
    }
}
Galik
  • 47,303
  • 4
  • 80
  • 117
1

The fact that you named your array hex does not matter to the compiler. It expects decimal input.

When you enter a, the input cannot be parsed as a decimal integer, so std::cin is set to an error mode. The subsequent call to >> then no longer prompts the user for text input but immediately returns, leaving the stream in its error state, until the loop finishes.

You can solve this problem in several ways:

  • Check the stream's state after the user has entered something. if (!cin) { /* error handling */ }.
  • Additionally, allow hexadecimal input with std::hex.

Or use a different strategy altogether. Instead of reading ints directly, each loop iteration could read a whole line of input into an std::string. That string can then be converted to an int with C++11's std::stoi, specifying base 16:

#include <iostream>
#include <string>

int main()
{
    int hex[16];
    std::cout << "enter the 16 hexadecimal numbers\n";
    for (int i = 0; i < 16; ++i)
    {
        std::string line;
        std::getline(std::cin, line);
        try
        {
            hex[i] = std::stoi(line, 0, 16);
            std::cout << "input worked: " << hex[i] << "\n";
        }
        catch (std::exception const& exc)
        {
            std::cout << "input failed\n";
        }
    }
}
Christian Hackl
  • 27,051
  • 3
  • 32
  • 62