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

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

Jan 02, 2016

Download

Documents

Deborah Taylor
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Lecture 6: Expressions and Interactivity (Part II) Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.

Lecture 6: Expressions and Interactivity (Part II)

Professor: Dr. Miguel Alonso Jr.

Fall 2008

CGS2423/COP1220

Page 2: 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

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

// 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 <iostream>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;}

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

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 <iomanip> Larger numbers will be fully printed

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

// This program displays three rows of numbers.#include <iostream>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;}

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

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

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

// This program asks for sales figures for 3 days. The total// sales are calculated and displayed in a table.#include <iostream>#include <iomanip>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;}

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

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

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

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

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

Introduction to File Input and Output

Simple techniques to write input and output operations with files

#include <fstream>; Define filesream objects

ofstream – output to files Ifstream – input from files Fstream – w/r to and from files

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

More Math Library Functions

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

// This program writes data to a file.#include <iostream>#include <fstream>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;}

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

// This program reads information from a file.#include <iostream>#include <fstream>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;}

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

// This program reads rectangle dimensions from a file.#include <iostream>#include <fstream>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;}

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

randome numbers Y = rand();

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

Problem Solving: Case Study

Class work: pg 137