1 / 10

C++ Basics #7

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.

jon
Télécharger la présentation

C++ Basics #7

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C++ Basics #7 Basic Input and Output

  2. Standard output (cout) • Standard input (cin) • stringstream In this video

  3. 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?

  4. 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)

  5. 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…

  6. 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…

  7. 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)

  8. 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

  9. 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

  10. 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

More Related