7. Introduction to c++: Basic input and output in C++ :: Videos


C++ Basics #7 Basic Input and Output

Topics covered in this video:

  • Standard output (cout)
  • Standard input (cin)
  • stringstream

What is input and output?

  • Interaction with the user.
  • Changing program according to user input.
  • C++ uses streams to perform input and output.
  • Stream is object where an object can either insert or extract characters to/from it.
  • C++ have iostream file in library where the input and output streams objects are declared.
  1. Standard Output (cout):
    • Default standard output is screen.
    • To give output we use ‘cout’ with << (Two less than signs)
    • Example:
      • cout << 23; 
        (gives 23 in screen)   
      • cout << x; 
        (prints the value of x to screen)
      • cout << “Hello world!”  
        (prints Hello World to   screen)
      • cout << “My age is: “ << age; 
    • Using endl or “n” will take output to next line.
  2. Standard Input(cin):
    • cin is used with >> to get a input from the stream.
    • Example: cin >> VariableName(To store the input)
    • To request more than one datum we can use multiple >>
    • Example:
      cin >> data1 >> data2;
      Which is same as
      cin >> data1;
      cin >> data2;
    • When getting string with cin, it will stop taking character as soon as it gets to a blank space. Use getline() instead
    • Using same variable to get input in different places will just replace the previous value of variable with new one.
  3. stringstream
    • Header file <sstream> defines the class stringstream that allows a string-based objects to be treated as a stream.
    • Especially useful to convert numerical value or string and viceversa
    • Example:
      string UserInput;
      float NumValue = 0;
      getline(cin, UserInput);
      stringstream(UserInput) >> NumValue;


Please login to post your thoughts!

There are no commnets yet!


Don't have an account?
Register here

Subscribe Youtube

Network DigitalStage.org

Some interesting stuffs