Originally Posted by
ShadeS_07
So far, I know that cin >> lets me input to a variable of any type... is that correct?
No, it is not.
"cin >> whatever" can be used to input to a variable of a type for which an operator>> is defined.
The standard library does define operator>>() for all basic types (int, double, char, etc) and for a number of types (eg std::string) in the standard library. If the programmer wants a user defined type (a struct/class or, more rarely, an enum type) to have such support, then the programmer needs to define it.
Originally Posted by
ShadeS_07
I'm not really sure what's the "proper" way of using cin... So, even though I'm programming C++, I tend to use some of "C's way" of inputting data...
That means you are really using C++ as "C with extensions". There is nothing inherently wrong with that, but what you are doing is probably only touching the surface of what you can do in C++. You are probably writing code that would be considered quite bad style - or possibly even inferior - by most C++ programmers. Note that these notions of "bad style" or "inferior" are subjective. Common practice - which is really what your question is about - in C++ is different from common practice in C.
Anyway, just as there is no "proper" way of doing C I/O, there is no "proper" way of doing C++ I/O. Technically, it is possible to use C I/O in C++, but it is also actively discouraged as it is a good way to make mistakes, particularly if C I/O and C++ I/O are both used in the one program.
What I'll do is give some possible ways of doing the things you ask. They are not the only ways, or even necessarily the best ways. C and C++ libraries both provide toolboxes, and we mortal programmers select tools out of toolboxes for any given task. Different programmers will prefer different tools.
Note also that I never employ "using namespace std;" in production code. So I always refer to cin by its full and unambiguous name of std::cin.
Originally Posted by
ShadeS_07
- If I want to input to a string variable / character string array, I'd use:
Code:
fgets([varname], sizeof([varname]), stdin);
Assuming you want to work with C-style strings (by convention an array of char terminated by a nul character) you might do this
Code:
std::cin.getline(varname), sizeof(varname));
Note that, as in C, there is a distinction between a pointer and an array, so the above will not work if given an arbitrary char pointer.
In C++, there is a better way, if you move away from the C notion of a string. If you use
Code:
std::string varname;
getline(std::cin, varname);
an entire line of input (up to the carriage return) is input, and the string is resized as needed to hold it. No possibility of getting the size wrong, as the programmer does not specify the size.
I would encourage this approach, although it does cause some headaches for C programmers, as there are some minor anomalies in how std::string interacts with C-style strings and string literals.
Originally Posted by
ShadeS_07
- If I want to input to a character variable, I'd use:
Code:
[character variable] = getchar();
Strictly speaking, I wouldn't use the above in C either, as getchar() returns an int, not a char. Converting it directly to a char prevents error checking (for example, detecting EOF). In C, I'd actually do
Code:
[int variable] = getchar();
if ([int variable] != EOF)
[char variable] = [int variable];
else
complain_bitterly();
The literal C++ equivalent of this is
Code:
[int variable] = std::cin.get();
if ([int variable] != EOF)
[char variable] = [int variable];
else
complain_bitterly();
And an alternative is
Code:
std::cin.get([char variable]);
if (!std::cin) // Test state of the stream
complain_bitterly();
Originally Posted by
ShadeS_07
- If I want to input to an integer variable, I'd use:
Code:
scanf("%i", &[variablename]);
- If I want to input to a float / double variable, I'd use:
Code:
scanf("%f", &[variablename]); // %lf for double
In both of the above
Code:
cin >> [variablename];
This works as the compiler knows what type variablename is, and calls the appropriate operator>>(). This obviously only works for types for which an operator>>() is defined.
Originally Posted by
ShadeS_07
- If I want to input to a float / double variable WITH precision, I'd use:
Code:
scanf("%.2f", &[variablename]); // %.2lf for double
Personally, I would never do that in C (better to make output a fixed precision, but input using a permissive format, as that allows the user more flexibility). However, the literal equivalent is;
Code:
std::cin >> set::setprecision(2) >> [variablename];