std::cin,std::wcin(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– std::cin, std::wcin πŸ’–

okay so bestie, welcome to the C++ Standard Libary page for the two most iconic input girlies in the whole standard library. no cap, these two are the main characters of "hello user, type something" energy, and we're gonna get you fully aquainted. ✨


🌸 NAME 🌸

std::cin, std::wcin β€” literally just std::cin, std::wcin. that's it. that's the name. the naming committee said "we're done here" and honestly? periodt. πŸ’…


πŸŽ€ Synopsis πŸŽ€

Defined in header <iostream>
extern std::istream cin;     (1)
extern std::wistream wcin;   (2)

so there's two of them, right:

  • (1) std::cin β€” the std::istream one πŸ¦‹
  • (2) std::wcin β€” the std::wistream one, i.e. the wide version 🌈

The global objects std::cin and std::wcin control input from a stream buffer of implementation-defined type (derived from std::streambuf), associated with the standard C input stream stdin. like, they're the C++ glow-up of stdin, iykyk. πŸ’œ

these objects are guaranteed to be initialized during or before the first time an object of type std::ios_base::Init is constructed, and are available for use in the constructors and destructors of static objects with ordered initialization β€” but ONLY as long as <iostream> is included before the object is defined. that's the fine print, bestie, don't skip it. πŸ‘‘

unless sync_with_stdio(false) has been issued, it is safe to concurrently access these objects from multiple threads for both formatted and unformatted input. so if you did turn that off, be careful, okay? that's the vibe check. 🫢

Once std::cin is constructed, std::cin.tie() returns &std::cout, and likewise, std::wcin.tie() returns &std::wcout. what does that mean fr fr? it means that any formatted input operation on std::cin forces a call to std::cout.flush() if any characters are pending for output. so your prompt actually shows up before it blocks. slay. πŸ”₯


⭐ Notes ⭐

The 'c' in the name refers to "character" (stroustrup.com FAQ). so cin means "character input" and wcin means "wide character input". the more you know, bestie πŸ’«


πŸ’€ Example πŸ’€

// Run this code

#include <iostream>

struct Foo
{
    int n;
    Foo()
    {
        std::cout << "Enter n: "; // no flush needed
        std::cin >> n;
    }
};

Foo f; // static object

int main()
{
    std::cout << "f.n is " << f.n << '\n';
}

Possible output:

Enter n: 10
f.n is 10

not the static object constructor reading from std::cin and then main printing it 😭 this example is lowkey showing off that whole "available during ordered initialization of static objects" thing from up top. iconic behaviour honestly. πŸ“


πŸ‘€ See also πŸ‘€

  • Init β€” initializes standard stream objects (public member class of std::ios_base)
  • cout β€” writes to the standard C output stream stdout
  • wcout β€” (global object)
  • stdin β€” expression of type FILE* associated with the input stream
  • stdout β€” expression of type FILE* associated with the output stream
  • stderr β€” expression of type FILE* associated with the error output stream (macro constant)

anyway bestie, that's the whole page. go forth and read your input, synchronize responsibly, and remember to tie your streams so your prompts flush. so obsessed with you for reading all the way to the end πŸ§šπŸ’–

source/version: http://cppreference.com β€” dated 2024.06.10 🌸 mwah, ily, bye πŸ’œ

πŸ“¦ where this page comes from