-
cin and char arrays
Can you cin a char array?
Code:
char Buffer[81];
cin>>Buffer;
Shouldn't this work? As long as there is no space shouldn't it read in the array as an array of chars?
I was advised that we can do it with an integer array, but can I do it with a char array. I'm assuming that there shouldn't be any issue with it, but it doesn't work on my current compile.
-
-
The >> operator isn't defined for arrays. That operator is defined for STL strings. Or you could use the getline function to read in character arrays.
-
cin.getline
I'm trying to avoid using cin.getline().
I'm trying to trouble shoot with only my pc.
My books are unavailable for now.
So I do appologize for remedial questions.
(as if any of mine are not.)
So cin>> doesn't work for char arrays?
Am I required to use cin.getline for char arrays v.s. int arrays?
-
Actually, this worked for me on VC++ 6.0:
Code:
#include <iostream>
int main()
{
char buffer[81];
std::cin >> buffer;
return 0;
}
although I think I read that better code to prevent overflow would be:
Code:
#include <iostream>
#include <iomanip>
int main()
{
const int bufSize = 81;
char buffer[bufSize];
std::cin >> std::setw(bufSize) >> buffer;
return 0;
}
Not sure why it didn't work for you.