1 / 5

C++ Chapter 3 Notes

C++ Chapter 3 Notes. cin Uses and Functions: cin>> always skips white space when reading data. cin.get(ch) gets the next character in the input stream and stores it in ch, even if it is a space.

taro
Download Presentation

C++ Chapter 3 Notes

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++ Chapter 3 Notes cin Uses and Functions: • cin>> always skips white space when reading data. • cin.get(ch) gets the next character in the input stream and stores it in ch, even if it is a space. • cin.ignore(int value, char value) ignores an integer number of characters/spaces or continues to ignore them until a certain character is reached. (e. g. cin.ignore(80, ‘\n’) ignores 80 spaces or ignores all spaces until the data goes to the next line.) • getline(cin, stringVar) will read an entire line of input into a string variable. If your previous input is a simple data type (i.e. int or char), you must do cin.ignore(80, ‘\n’) before using getline(). • cin.putback(ch) lets you put the last character obtained from the get() function (ch) back into the input stream. • cin.peek() peeks at the next value in the input stream but doesn’t remove it. • If you try to read an invalid value into a variable (i.e. an integer into a character), the program will enter the fail state and all other characters in the input stream will be ignored.

  2. Examples • Suppose x is an int, y is a double, and ch is a character. Given the input: 56.8 L4 72 What value (if any) is assigned to x, y, and ch after each statement executes. • cin>>x>>y>>ch; • cin>>y>>ch>>x; • cin>>y; cin.get(ch); cin>>x; • cin.get(ch); cin.putback(ch); cin>>y>>x; • ch=cin.peek(); cin>>y; cin.ignore(50, ‘4’); cin>>x; x=56, y=.8, ch=‘L’ x=4, y=56.8, ch=‘L’ x=input failure, y=56.8, ch=' ' x=input failure, y=56.8, ch=‘5' x=72, y=56.8, ch=‘5'

  3. Formatting Output The following manipulators require the header file #include <iomanip> They are all used with cout<<! • setprecision(n) – rounds to n number of decimal places • fixed – outputs doubles in fixed decimal format, as opposed to scientific notation • showpoint – forces the output to show a decimal point with trailing zeros • setw(n) – sets the width of a column • setfill(ch) – fills the empty spaces in the column with a character (ch) • left and right – aligns the output in a column; the default is right

  4. Examples Write the code to produce the following outputs: • *****Wazzup!***** • Yo G!$$$$$$$ • This is so cool!.....Oh yeah! This***Rocks! My socks! cout<<setfill(‘*’)<<setw(12)<<“Wazzup!<<setw(5)<<“*”<<endl; cout<<left<<setfill(‘$’)<<setw(12)<<“Yo G!”<<endl; cout<<setfill(‘.’)<<setw(16)<<“This is so cool!”<<setw(13)<<“Oh yeah!” <<endl; cout<<setfill(‘*’)<<setw(4)<<“This”<<setw(9)<<“Rocks!”<<setfill(' ') <<setw(16)<<“My socks!”<<endl;

  5. File Input/Output • So far, we have only used the console for input/output. You can also use a file (usually a .txt file). You must include the header file #include <fstream> • You need to declare file stream variables: ifstream fin; ofstream fout; • You must open the files you want to use. For example, if you are getting data from “scores.text” and saving data to “results.text” you would type the following: fin.open(“scores.txt”); fout.open(“results.txt”); If you want the output file to add new info to the end of previous info, instead of replacing it, change the last line to fout.open(“results.txt”, ios::app); • Now you can use fin and fout just like cin and cout! • At the end, you should close your files: fin.close(); fout.close(); You don’t have to use fin and fout. You can call them anything!

More Related