Lecture 6: Expressions and Interactivity (Part II) Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.

Slides:



Advertisements
Similar presentations
Chapter 3. Expressions and Interactivity CSC125 Introduction to C++
Advertisements

CPS120: Introduction to Computer Science INPUT/OUTPUT.
CS1 Lesson 3 Expressions and Interactivity CS1 -- John Cole1.
Starting Out with C++, 3 rd Edition 1 Chapter 3. Expressions and Interactivity.
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
Copyright © 2012 Pearson Education, Inc. Chapter 3: Expressions and Interactivity.
1 Lecture 6: Input/Output (II) Introduction to Computer Science Spring 2006.
1 CS 105 Lecture 9 Files Version of Mon, Mar 28, 2011, 3:13 pm.
CS 1400 Pick ups from chapters 2 and 3. #include directive This pre-processing directive causes the textual contents of a named file to be inserted into.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 3 Expressions.
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 3- 1.
CS 1400 Feb 2007 Pick ups from chapters 2 and 3. Example; A person 15 years of age may acquire a driver’s lic. with permission from parents. A person.
CS Sept 2006 Pick ups from chapters 2 and 3.
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
1 CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
Classes and Objects Objects of class type string Input/Output Objects and Formatted Input/Output 6/30/2015MET CS 563--Fall A. Using Class Type Objects.
1 Chapter 3 Expressions and Interactivity. 2 Topics 3.1 The cin Object 3.2 Mathematical Expressions 3.3 When You Mix Apples and Oranges: Type Conversion.
Chapter 3: Input/Output
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 3: Expressions and Interactivity.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
1 9/26/07CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
Chapter 3: Input/Output
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
1 CS102 Introduction to Computer Programming Week 3 Chapter 3 Expressions and Interactivity.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Expressions and Interactivity.
Copyright © 2012 Pearson Education, Inc. Chapter 3: Expressions and Interactivity.
Chapter 3: Expressions & Interactivity
Output Formatting No, I don't want 6 digits…. Standard Behavior Rules for printing decimals: – No decimal point: prints as 1 – No trailing zeros:
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Formatting Output.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output.
I/O and Data Formatting Introduction to Class Concepts INFSY 307 Spring 2003 Lecture 3.
Input/Output Sujana Jyothi C++ Workshop Day 2. C++ I/O Basics 2 I/O - Input/Output is one of the first aspects of programming that needs to be mastered:
Lecture 9: Making Decisions Final Section Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 3 Expressions and Interactivity.
Lecture 7: Making Decisions Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Expressions and Interactivity. 3.1 The cin Object.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Formatting Output.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Starting Out with C++: From Control Structures through Objects
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
1 Manipulators manipulators are used only in input and output statements endl, fixed, showpoint, setw, and setprecision are manipulators that can be used.
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Math Operators and Output Formatting. Incrementing and Decrementing StatementEquivalent Counter++;Counter = Counter + 1; ++Counter;Counter = Counter +
Chapter Expressions and Interactivity 3. The cin Object 3.1.
Chapter 4: Introduction To C++ (Part 3). The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Information.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3: Expressions and Interactivity.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
Lecture 3 Expressions, Type Conversion, Math and String
Introduction to C++ (Extensions to C)
CS102 Introduction to Computer Programming
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Chapter 3. Expressions and Interactivity
Chapter 3 L7.
Chapter 3: Expressions and Interactivity.
Input and Output Chapter 3.
Standard Input/Output Streams
Standard Input/Output Streams
Expressions and Interactivity
Starting Out with C++: From Control Structures through Objects
Basic Input and Output C++ programs can read and write information using streams A simple input stream accepts typed data from a keyboard A simple output.
Expressions and Interactivity
Chapter 3 Input output.
Formatting the Output The C++ standard library supplies many manipulators: endl, setw, fixed, showpoint, setprecesion. If we want to use endl, fixed, or.
Chapter 3: Expressions and Interactivity
Formatted Input, Output & File Input, Output
Standard Version of Starting Out with C++, 4th Edition
C++ Programming Lecture 8 File Processing
Presentation transcript:

Lecture 6: Expressions and Interactivity (Part II) Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220

Multiple Assignment and Combined Assignment Multiple assignment: assign the same value to several variables with one statement example: a = b = c = d = 12; Combined assignment: number = number + 1; New Value Old Value

// This program tracks the inventory of three widget stores // that opened at the same time. Each store started with the // same number of widgets in inventory. By subtracting the // number of widgets each store has sold from its inventory, // the current inventory can be calculated. #include using namespace std; int main() { int begInv, // Begining inventory for all stores sold, // Number of widgets sold store1, // Store 1's inventory store2, // Store 2's inventory store3; // Store 3's inventory // Get the beginning inventory for all the stores. cout << "One week ago, 3 new widget stores opened\n"; cout << "at the same time with the same beginning\n"; cout << "inventory. What was the beginning inventory? "; cin >> begInv; // Set each store's inventory. store1 = store2 = store3 = begInv; // Get the number of widgets sold at store 1. cout << "How many widgets has store 1 sold? "; cin >> sold; store1 -= sold; // Adjust store 1's inventory. // Get the number of widgets sold at store 2. cout << "How many widgets has store 2 sold? "; cin >> sold; store2 -= sold; // Adjust store 2's inventory. // Get the number of widgets sold at store 3. cout << "How many widgets has store 3 sold? "; cin >> sold; store3 -= sold; // Adjust store 3's inventory. // Display each store's current inventory. cout << "\nThe current inventory of each store:\n"; cout << "Store 1: " << store1 << endl; cout << "Store 2: " << store2 << endl; cout << "Store 3: " << store3 << endl; return 0; }

Formatting Output More on output stream manipulation The way a value is printed is called its formatting set(w): sets the field width (in characters) for the value immediately following it Requires: #include Larger numbers will be fully printed

// This program displays three rows of numbers. #include using namespace std; int main() { int num1 = 2897, num2 = 5, num3 = 837, num4 = 34, num5 = 7, num6 = 1623, num7 = 390, num8 = 3456, num9 = 12; // Display the first row of numbers cout << num1 << " " << num2 << " " << num3 << endl; // Display the second row of numbers cout << num4 << " " << num5 << " " << num6 << endl; // Display the third row of numbers cout << num7 << " " << num8 << " " << num9 << endl; return 0; }

setprecision: floating point numbers are rounded setprecision(n) fixed Setprecision sometimes does not give you what you want, use fixed to force cout to print the digits in fixed- point notation or decimal showpoint Shows trailing zeros (not shown by default) left and right manipulators left right justification

// This program asks for sales figures for 3 days. The total // sales are calculated and displayed in a table. #include using namespace std; int main() { double day1, day2, day3, total; // Get the sales for each day. cout << "Enter the sales for day 1: "; cin >> day1; cout << "Enter the sales for day 2: "; cin >> day2; cout << "Enter the sales for day 3: "; cin >> day3; // Calculate the total sales. total = day1 + day2 + day3; // Display the sales figures. cout << "\nSales Figures\n"; cout << " \n"; cout << setprecision(2) << fixed; cout << "Day 1: " << setw(8) << day1 << endl; cout << "Day 2: " << setw(8) << day2 << endl; cout << "Day 3: " << setw(8) << day3 << endl; cout << "Total: " << setw(8) << total << endl; return 0; }

Formatted Input cin provides ways of controlling string and character input Similar to those of cout setw(10): prevents buffer overrun by limiting number of characters cin.getline() char sentence[81]; cin.getline(sentence,20); cin.get() Gets a single character, including spaces or the return character

Mixing cin and cin.get use cin.ignore(n,c) n: number of characters to skip c: character encountered Which ever comes first Member functions getline, get, ignore are all examples of cin’s member functions or procedures that are part of cin cin is an object

Introduction to File Input and Output Simple techniques to write input and output operations with files #include ; Define filesream objects ofstream – output to files Ifstream – input from files Fstream – w/r to and from files

More Math Library Functions

// This program writes data to a file. #include using namespace std; int main() { ofstream outputFile; outputFile.open("demofile.txt"); cout << "Now writing information to the file.\n"; // Write 4 great names to the file outputFile << "Bach\n"; outputFile << "Beethoven\n"; outputFile << "Mozart\n"; outputFile << "Schubert\n"; // Close the file outputFile.close(); cout << "Done.\n"; return 0; }

// This program reads information from a file. #include using namespace std; int main() { ifstream inFile; const int SIZE = 81; char name[SIZE]; inFile.open("demofile.txt"); cout << "Reading information from the file.\n\n"; inFile >> name; // Read name 1 from the file cout << name << endl; // Display name 1 inFile >> name; // Read name 2 from the file cout << name << endl; // Display name 2 inFile >> name; // Read name 3 from the file cout << name << endl; // Display name 3 inFile >> name; // Read name 4 from the file cout << name << endl; // Display name 4 inFile.close(); // Close the file cout << "\nDone.\n"; return 0; }

// This program reads rectangle dimensions from a file. #include using namespace std; int main() { ifstream inFile; int length, width, area; inFile.open("dimensions.txt"); cout << "Reading dimensions of 5 rectangles from the file.\n\n"; // Process rectangle 1 inFile >> length; inFile >> width; area = length * width; cout << "Area of rectangle 1: " << area << endl; // Process rectangle 2 inFile >> length; inFile >> width; area = length * width; cout << "Area of rectangle 2: " << area << endl; // Process rectangle 3 inFile >> length; inFile >> width; area = length * width; cout << "Area of rectangle 3: " << area << endl; // Process rectangle 4 inFile >> length; inFile >> width; area = length * width; cout << "Area of rectangle 4: " << area << endl; // Process rectangle 5 inFile >> length; inFile >> width; area = length * width; cout << "Area of rectangle 5: " << area << endl; // Close the file inFile.close(); cout << "\nDone.\n"; return 0; }

randome numbers Y = rand();

Problem Solving: Case Study Class work: pg 137