-2

So I'm using cin for taking input for a char array.

char mycroft[2];
    cin>>mycroft;
    cout<<mycroft;

It works fine. Now I'm using cin for taking input for an int array:

 int mycroft[2];
    cin>>mycroft;
    cout<<mycroft;

However, I am getting this error which I do not understand:

error: no match for 'operator>>' (operand types are 'std::istream {aka std::basic_istream}' and 'int [2]')|

Can someone please tell me why it's not working? Also my professor said taking input using cin isn't advisable. To what extent is that true?

Leigh
  • 28,765
  • 10
  • 55
  • 103
shivam13juna
  • 329
  • 4
  • 11
  • That's because a `char` array is treated as an old C-style *string*. There are no such special handling for any other array or pointer. This of course means that for your example character array only can input a single character. – Some programmer dude Apr 21 '17 at 06:02
  • @Someprogrammerdude umm...no? i've given "HELLO" as a word and it worked! – shivam13juna Apr 21 '17 at 06:05
  • Then the input operator will write six characters (don't forget the string terminator) to a two-element array. It will write out of bounds and you will have *undefined behavior*. That's why you should always use `std::string` for string input! – Some programmer dude Apr 21 '17 at 06:06

3 Answers3

0

You probably need:

for(i=0; i<2; i++) 
{
   cin>>mycroft[i];
 ...
}
Jacob Seleznev
  • 8,013
  • 3
  • 24
  • 34
  • ummm..that's simplest approach..ik it..but the way how I took it is takes less time in competitive coding...I think i'm looking for a way to make that way work with int array – shivam13juna Apr 21 '17 at 06:07
  • I don't understand – Jacob Seleznev Apr 21 '17 at 06:16
  • @Mark Learn to code. Then learn to compete. Doing it the other way around typically teaches you to write really, really bad code. For example, in the name of coding speed, you're wasting time on the impossible. – user4581301 Apr 21 '17 at 06:16
  • @user4581301 I couldn't agree more..but sometimes a way does exist!! I just wanna know that.. – shivam13juna Apr 21 '17 at 06:18
  • @Mark `iostream`s chain. for example `cin>>mycroft[0]>>mycroft[1];` [or write a `>>` overload](http://stackoverflow.com/questions/4421706/operator-overloading) that performs the work for you. – user4581301 Apr 21 '17 at 06:22
0

You have misunderstood the concept of char arrays and int (and others) arrays.

When you create a char variable[x] you reserve space in memory to x * 8 bits (length of char) and the language treat it like a continuous space. To finish this chain you need to put the end character '\0'. The char mycroft[2] has 1 usable char (in theory) with 16 bits (2 * 8 bits) space in memory.

In the other hand, when you create an int variable[x] you reserve space in memory to x * 32 bit (length of int) and despite the system allocate it continuously, the language treat every x elements individually. In short, treat it like a real array. The int mycroft[2] has 2 usable elements and takes 64 bits (2 * 32 bits) space in memory.

Because of that cin works with char directly (cin >> mycroft and cout << mycroft) and doesn't work and with the other arrays (in your case int) you need to access them individually (cin >> mycroft[position] and cout << mycroft[position]).

Akira
  • 4,385
  • 3
  • 24
  • 46
TheArchitect
  • 1,160
  • 4
  • 15
  • 26
0

Because an array of characters can means it's a C-style string (if zero-terminated, i.e. the last element in the array is zero). The std::cout and the std::cin treat them in such way.

A char[] can be initialized as:

char mycroft[] = "Hello, World!";

An int[] can't be initialized in such way, it's an array of integers. The reason why your professor told you that taking input using std::cin isn't advisable is that it can cause overflows if you using it in a wrong way. In your first example the std::cin will write everything from the input into a two element long array causing the memory follows the char mycroft[2] will be overwritten.

Akira
  • 4,385
  • 3
  • 24
  • 46