Classroom Programming
page was last updated: Sep 10, 2016 10:18
forum
sorry ): this page is incomplete. feel free to ask me directly for any help ^_^
Input Output A5

Intro

Input and output is ACTUALLY a complicated task. Thankfully, the standard library <iostream>, or Input Output STREAM makes it simple. There's more to output than just cout.

Escape Characters

A good resource.

There are these weird lil characters you can't "see". For example, a new line or the tab key. To specifically say these characters, we first use the "escape character" or in C++, the '\'. If we want to say "tab" character, we say '\t'. The compiler will know to treat those two characters as one. For now, the resource linked above has a full list of those special characters.

Input

Input is simple. But you also need to put the data you get somewhere, hence variables. Just as cout is "Character OUT", cin is "Character IN". Also the arrows point the other way with >>. The way I remember the arrow direction, the data goes INTO the variable, and so it points into it.

cin

cin will take whatever you type in and try to automatically convert it into the container you put it into. Just one problem. You'll notice that when you type in "baked potato", it will stop at "baked". This is because cin will stop immediatly when it sees any whitespace, which includes spaces AND the "new line" character (when you press enter).

#include <iostream> #include <string> using namespace std; int main() { string input_string; cout << "type something in plox:"; cin >> input_string; //ask the user for input and store it in 'input_string' cout << "you said:" << input_string; << endl; return 0; } Type something in plox: hablabla you said: hablabla |

getline()

Again, because cin stops at any whitespace, we'll need a helper to ignore it. getline() does exactly that! getline (cin,input_string); The "cin" part of getline is there because you're telling it where to listen from. The second part is where you want it to store the information it collected.