Download presentation
Presentation is loading. Please wait.
Published byKristopher Hawkins Modified over 8 years ago
1
C++ Basics #7 Basic Input and Output
2
In this video Standard output (cout) Standard input (cin) stringstream
3
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.
4
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)
5
cout continues… Remember “hello” and hello are different when printing or doing cout. Example cout << “hello”; (prints hello to screen) cout << hello; (prints contents of hello variable to screen) Multiple << can be written in same statement Example: cout << “Hello ” << “World”; Multiple << can be used with combination of variables and strings. Example: cout << “My age is: “ << age; Will give: My age is: 32 (if age = 32).
6
cout continues… Writing cout in multiple lies does not give multiline outputs. Example: cout << “This is a string.”; cout << “This is another string”; Will give as: This is a string.This is another string. Using endl or “\n” will take output to next line. Example: cout << “This is a string.\n”; or cout << “This is a string.” << endl; cout << “This is another string.”; Will give: This is a string. This is another string
7
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;
8
cin countinues When getting string with cin, it will stop taking character as soon as it gets to a blank space. Example: cin >> AString; And user inputs Hello world, then only Hello will be taken into AString. For this we will use a function called getline as following. Example: getline(cin, Astring); Using same variable to get input in different places will just replace the previous value of variable with new one.
9
stringstream Header file 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; This is extract the number from UserInput and store to NumValue.
10
Please rate, comment and subscribe Done with basics Next: Control structures and functions and codes!! Please visit www.LearnFromSuzzett.com for more materials to learn Quizzes, challenges, articles and news.www.LearnFromSuzzett.com
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.