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.

Slides:



Advertisements
Similar presentations
CPS120: Introduction to Computer Science INPUT/OUTPUT.
Advertisements

File streams Chapter , ,
Copyright © 2012 Pearson Education, Inc. Chapter 3: Expressions and Interactivity.
CS 117 Spring 2002 Basic Program Elements Chapter 2.
CS 1400 Chapter 4, sections Relational operators Less than< Greater than> Less than or equal= Equal== Not equal!=
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 3- 1.
CS Sept 2006 Pick ups from chapters 2 and 3.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 3: Input/Output.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 3: Expressions and Interactivity.
Chapter 3: Input/Output
Admin Office hours 2:45-3:15 today due to department meeting if you change addresses during the semester, please unsubscribe the old one from the.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 27P. 1Winter Quarter I/O Manipulation Lecture.
Basic Elements of C++ Chapter 2.
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
CHAPTER 3 INPUT/OUTPUT. In this chapter, you will:  Learn what a stream is and examine input and output streams  Explore how to read data from the standard.
Exposure C++ Chapter VII Program Input and Output.
Copyright © 2012 Pearson Education, Inc. Chapter 3: Expressions and Interactivity.
Computer Programming TCP1224 Chapter 4 Variables, Constants, and Arithmetic Operators.
CS31: Introduction to Computer Science I Discussion 1A 4/9/2010 Sungwon Yang
Lecture 6: Expressions and Interactivity (Part II) Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
C++ Programming: Basic Elements of C++.
File I/O ifstreams and ofstreams Sections 11.1 &
Chapter 2 Overview of C++. 2 Overview  2.1 Language Elements  2.2 Reserved Words & Identifiers  2.3 Data Types & Declarations  2.4 Input/Output 
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.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
I/O and Data Formatting Introduction to Class Concepts INFSY 307 Spring 2003 Lecture 3.
TEXT FILES. CIN / COUT REVIEW  We are able to read data from the same line or multiple lines during successive calls.  Remember that the extraction.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 3 Expressions and Interactivity.
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
Expressions and Interactivity. 3.1 The cin Object.
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
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.
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 Computer programming -1-. Input \ Output statement 1- Input (cin) : Use to input data from keyboard. Example : cin >> age; 2- Output (cout):
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Chapter Expressions and Interactivity 3. The cin Object 3.1.
 Data Streams  Numeric Output  Numeric Input  Multiple Numeric Output  Multiple Numeric Input  Character Output  Character Input  String Output.
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 4 Strings and Screen I/O. Objectives Define strings and literals. Explain classes and objects. Use the string class to store strings. Perform.
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.
Bill Tucker Austin Community College COSC 1315
Lecture 3 Expressions, Type Conversion, Math and String
Today Variable declaration Mathematical Operators Input and Output Lab
LESSON 2 Basic of C++.
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Chapter 3: Expressions and Interactivity.
Standard Input/Output Streams
Standard Input/Output Streams
Expressions and Interactivity
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
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Chapter 3 Input output.
Chapter 3: Expressions and Interactivity
Chapter 4 INPUT AND OUTPUT OBJECTS
Formatted Input, Output & File Input, Output
Chapter 2: Introduction to C++.
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Presentation transcript:

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 over 15 is eligible without parental permission. A person under 15 is not eligible. Write a small program to input an age and output one of 3 messages: “permission required”, “eligible”, or “not eligible”.

Boolean constants A bool variable may only hold a bool value (true or false); bool flag; flag = true; flag = false; What would this do? while (true) {cout << “hello! “; }

Named constants Constants may be given symbolic names to represent them in a program; –Form: const type name = value; –Example: const float PI = ; const int SIZE = 24; –Alternative Form:#define name value #define PI #define SIZE 24 Defines should be place above main()

Character variables A variable of type char can hold one character; char sex; A character constant is delimited by single quotes; sex = ‘f’; if (sex == ‘m’) cout << “male”;

cin I/O rules Only digit characters (and sign) can be entered for an int variable. Only digit, decimal, and possibly E- notation characters (and sign) can be entered for a float variable. Only printable characters can be entered for a char variable.

char vs. literals A literal is different from a character! char c; cout << “hello world”; c = “hello world”;// ERROR! c = ‘h’;// CORRECT c = “h”;// ERROR!

Strings or char arrays A message or string of characters can be stored in a special array variable; char word[80]; // holds <80 characters cin >> word; cout << word << endl; Only printable characters can be input using cin (no blanks, tabs, etc.)

Initially limit use of string arrays… For now, only input and output will be done on arrays; word = “hello world”;// ERROR! if (word < “goodbye”)// ERROR! word1 = word2;// ERROR!

Combining assignments Multiple assignment statements may be combined –Example: a = b = c = 25; Shorthand notation for combining arithmetic operations and assignment –Example: a = a * 2;same asa *= 2; b = b – 5;same asb -= 5; c = c / 7;same asc /= 7;

Formatting output Requires: #include cout manipulators: setw(n) (set the minimum spaces for the next value output) setprecision(n) (set the precision or rounding for a float value) fixed (force output of float values in fixed-point notation) showpoint (show a decimal point with trailing zeros)

Controlling input Requires #include cin manipulators setw(n) do not input more than n-1 characters for the next variable textbook error, pg 128 char word[5]; cin >> setw(5) >> word;// user enters “Eureka” cout << word;// program outputs “Eure”

Special input examples… char sentence[20], ch; cin.getline(sentence, 20); –reads all characters on a line (up to 19 in count) from the keyboard and stores them in array sentence cin.get(ch); –reads the next input character (including white spaces) cin >> ch; –reads the next printable character (ignoring white spaces) cin.ignore (); –ignore (discard) the next input buffer character

More math library functions… Requires: #include functionsexamples absy = abs(x); sqrty = sqrt(a*a + b*b); logy = log(x); siny = sin(x+z); cosy = cos(x); tany = tan(x); etc…

File Output Requires:#include ofstream fout; fout.open (“report.txt”); fout << “hello from a file!\n”; fout.close(); To write to report.txt on the a: drive; fout.open (“a:\\report.txt”); fout.open (“a:report.txt”); fout.open (“a:\report.txt”); // ERROR!

File Input Requires:#include int a, b; ifstream fin; fin.open (“report.txt”); fin >> a >> b; cout << “sum is: “ << a+b << endl; fin.close();

Example… Write a program to generate a weekly report of concert hall business for a touring show. –assume:reserved seat tickets$80 stadium seat tickets$50 standing tickets$35 production company gets 80%

Input file… line 1:tour name line 2 reserved stadium standing Example: Rolling Stones Geriatric Tour a:tour.txt