100 likes | 256 Views
C++ Basics #7. Basic Input and Output. Standard output ( cout ) Standard input ( cin ) stringstream. In this video. Interaction with the user. Changing program according to user input. C++ uses streams to perform input and output.
E N D
C++ Basics #7 Basic Input and Output
Standard output (cout) • Standard input (cin) • stringstream In this video
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. Input and Output?
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) Standard output (cout)
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). 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 cout continues…
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; Standard input (cin)
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 getlineas 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. cincountinues
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: stringUserInput; floatNumValue = 0; getline(cin, UserInput); stringstream(UserInput) >>NumValue; • This is extract the number from UserInput and store to NumValue. stringstream
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. Please rate, comment and subscribe