The "cin"... proper way of using cin, tips, tricks...

This is a discussion on The "cin"... proper way of using cin, tips, tricks... within the C++ Programming forums, part of the General Programming Boards category; So far, I know that cin >> lets me input to a variable of any type... is that correct?
I'm ...

The "cin"... proper way of using cin, tips, tricks...

So far, I know that cin >> lets me input to a variable of any type... is that correct?

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...

For example,

- If I want to input to a string variable / character string array, I'd use:

Code:

fgets([varname], sizeof([varname]), stdin);

- If I want to input to a character variable, I'd use:

Code:

[character variable] = getchar();

- 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

- If I want to input [EDIT: "output" NOT input... lol] to a float / double variable WITH precision, I'd use:
[EDIT: NEW CODE... -_+]

Code:

printf("The output: %.2f", [variablename]); // %.2lf for double

...obviously that's not the C++ way, so I would like to ask you people how am I going to those stuffs above using the C++ way... are there any cin equivalent for that? Thanks in advance and God bless...

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

If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Of course, the demonstration should be equally applicable to other types of numbers. And I didn't demonstrate anything with strings although personally I never do more than dump them pretty much unformatted anyway.

The objects inserted into streams to manipulate things are given the inventive name of manipulators. A pretty complete listing of standard ones is here: manipulators - C++ Reference

If you actually go through that reference, you'll start looking into other things like ios_base, the class that standard streams are based on. You'll find a lot of member functions there that duplicate functionality. In my example though, I show you something with them that without them would be annoying, TBH. And you have to fix the stream when you're done if you ever want a 'normal' stream again, because some changes, like precision or base, are persistent, and others, like width, are applied once to a particular object.

Welcome to a rather dark corner of C++ programming. Bjarne apologizes.