C++ Input Fun

Eli Huebner
2 min readApr 16, 2021

A nice thing about learning a new programming language is geeking out about features you’ve never seen before. As writing this post, I am nearly a complete novice at C++, and that means I get to experience everything for the first time. For the most part, it is pretty common stuff for a lower order language, and the differences from Ruby or JavaScript usually have to do with explicitly telling the computer to do things that are automated in the languages I already know.

Remember, I am a complete noob at C++, so don’t make fun of me for being amazed and amused by the cin stream. Ruby and JavaScript allow the user to input long strings (ah, the vagueries of language) of data, and the program will try to store them all. C++, on the other hand, will only read until it comes upon whitespace. While I imagine this will become annoying if I try to get complex input from a user, like a sentence, it also opens some fun possibilities. Consider the following code:

#include <iostream>int main() {   std::cout << "Enter Str, Dex, Con, Int, Wis, and Char, with a
space between each:\n";
int str; int dex; int con; int smart; //int is reserves as a datatype int wis; int comm; //"communication", as char is reserves as well std::cin >> str >> dex >> con >> smart >> wis >> comm; std::cout << "Str: " << str << "\nDex: " << dex << "\nCon: " <<
con << "\nInt: " << smart << "\nWis: " << wis << "\nChar: " <<
comm << "\n";
}

This is a basic program that asks a user to input the 6 attributes of a Dungeons and Dragons character, then recites them to the user. Notice that there is only one input stream. While you could write a separate std::cin >> for each attribute, since C++ stops reading at whitespace, it will go on and place the next input in the next variable, all in a single line of code. While the code might get unwieldy, as it certainly does in the cout stream, it also cuts a tremendous amount of repetition.

While this is certainly not earth shattering information, especially to anyone who already knows C++, it tickled me to learn.

--

--

Eli Huebner

I taught high school history for 4 years, before pivoting to software development in search of something more creative.