Quiz Next Monday.

Slides:



Advertisements
Similar presentations
C++ provides the following classes to perform output and input of characters to/from files: ofstream: Stream class to write on filesofstream ifstream:
Advertisements

1 CS 105 Lecture 11 Arrays Version of Wed, Apr 6, 2011, 6:20 pm.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 4 Programming with Data Files.
Computer Science 1620 Loops.
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
1 CS 105 Lecture 8 Strings; Input Failure Mon, Mar 7, 2011, 3:39 pm.
1 Programming with Data Files Chapter 4 2 Standard Input Output C++ Program Keyboard input cin Output Screen cout.
1 CS 105 Lecture 9 Files Version of Mon, Mar 28, 2011, 3:13 pm.
1 Programming with Data Files Chapter 4. 2 Standard Input Output C++ Program Keyboard input cin Output Screen cout.
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
File Review Declare the File Stream Object Name –ofstream for output to file –ifstream for input from file Associate a File Name with the File Stream Object.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
What is the out put #include using namespace std; void main() { int i; for(i=1;i
Streams, Files. 2 Stream Stream is a sequence of bytes Input stream In input operations, the bytes are transferred from a device to the main memory Output.
Announcements Quiz 2 Grades Posted on blackboard.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
CS Class 07 Topics –  When software goes wrong  Count controlled loops  Sentential controlled loops  putting it all together Announcements.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Fundamental Programming: Fundamental Programming Introduction to C++
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
Disk Files for I/O your variable (of type ifstream) your variable (of type ofstream) disk file “myInfile.dat” disk file “myOut.dat” executing program input.
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
Quiz 2 Results. What Is Wrong? #include using namespace std int Main() { // Say Hello 4 times for(i == 0; i < 3; i++) { cout >> "Hello World!"
Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Lecture  I/O Streams  Console I/O  File I/O  Tools for File I/O  Sequential.
File I/O in C++. Using Input/Output Files A computer file  is stored on a secondary storage device (e.g., disk);  is permanent;  can be used to provide.
Exam 2 EXAM 2 Thursday!!! 25% of Final Grade Know: loops, switch/case Files Input failure (e.g. scan.hasNextInt())
File I/O in C++ I. Using Input/Output Files A computer file is stored on a secondary storage device (e.g., disk); is permanent; can be used to provide.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students File Input and Output Checking input for errors.
Chapter 2 Creating a C++ Program. Elements of a C++ Program Four basic ways of structuring a program Four basic ways of structuring a program 1.Sequencing.
Working with Batches of Data
ifstreams and ofstreams
LESSON 2 Basic of C++.
Computing Fundamentals
What is a File? A file is a collection on information, usually stored on a computer’s disk. Information can be saved to files and then later reused.
Introduction to C++ October 2, 2017.
CSCI 161: Introduction to Programming
Copyright © 2003 Pearson Education, Inc.
Parallel Arrays Parallel array =>Two or more arrays with the same number of elements used for storing related information about a collection of data. Example:
FILE INPUT OUTPUT Skill Area 315 Part B Materials Prepared by
File I/O.
CS 1430: Programming in C++ Turn in your Quiz1-2 No time to cover HiC.
Intro to Programming Week # 4 Control Structure Lecture # 8
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
More About File Reading
Today’s Lecture I/O Streams Tools for File I/O
Know for Quiz Everything through Last Week and Lab 7
Exam 2 EXAM 2 Thursday!!! 25% of Final Grade
File Review Declare the File Stream Object Name
Standard Input/Output Stream
No I/O is built into C++ instead, a library provides input stream and output stream Keyboard Screen executing program istream ostream 1.
Chapter 3 Input output.
If Statements.
Programming File I/O.
Engineering Problem Solving with C++ An Object Based Approach
CHAPTER 4 File Processing.
Fundamental Programming
Reading from and Writing to Files
Programming with Data Files
Reading Data From and Writing Data To Text Files
Using string type variables
(Dreaded) Quiz 2 Next Monday.
ifstreams and ofstreams
Today’s Objectives 28-Jun-2006 Announcements
File I/O in C++ I.
Reading from and Writing to Files
Programming Fundamental
Presentation transcript:

Quiz Next Monday

Strings Revisited string Is a Type in C++ Library <string> May Initialize or Assign with Double Quoted Values May Compare using Equality Operators Can Access Single Characters using Square Bracket Operator ([ ]) Can Use length() Object Function Can Add to String

#include <iostream> #include <string> using namespace std; int main() { string lastname, firstname; cout << "Enter last name: "; cin >> lastname; cout << "Enter first name: "; cin >> firstname; cout << "Your name is " << firstname << " " << lastname << endl; return(0); }

#include <iostream> #include <string> using namespace std; int main() { string lastname, firstname; bool instructor; cout << "Enter last name: "; cin >> lastname; cout << "Enter Firstname: "; cin >> firstname; if (lastname == "hanrath") instructor = true; else instructor = false; return(0); }

#include <iostream> #include <string> using namespace std; int main() { string word = "start"; cout << word << endl; word[0] = 'h'; word[1] = 'e'; return(0); }

#include <iostream> #include <string> using namespace std; int main() { string word = "start"; cout << word << endl; word[0] = 'h'; word[1] = 'e'; cout << "String length is: " << word.length() << endl; return(0); }

#include <iostream> #include <string> using namespace std; int main() { string word = "start"; cout << word << endl; word[0] = 'h'; word[1] = 'e'; cout << "String length is: " << word.length() << endl; word = word + " " + "healthy" + " " + word; return(0); }

More on Input Input Failure Common *MUST* Be Part of Test Plan cin Used for Input If User Enters Bad Type of Data, cin Left in failed state Must Use cin.clear() function to Start Over Must Use cin.ignore() function to Get Beyond Bad Input May Use cin as Condition in if Statement Pages 111-113, 175 in Malik

Input Failure Example #include <iostream> #include <string> using namespace std; int main() { int numEmployees; bool done = false; string garbage; while (!done) cout << "Enter number of employees: "; cin >> numEmployees; if (!cin) //if input stream in failed state cout << "Bad input, try again." << endl; cin.clear(); // changes input state to ok cin.ignore(200,'\n'); // gets beyond bad input characters } else done = true; return(0);

Know for Quiz Everything through Exam 1 while, for, and do-while Loops New string Features Input Failure

What Is Wrong? #include <iostream> #include <string.h> using namespace std int Main() { // Say Hello 4 times for(i == 0; i < 3; i++) cout >> "Hello World!" << endl; } return(0);

What Is Wrong? (Corrections) #include <iostream> #include <string> using namespace std; int main() { int i; for(i = 0; i < 3; i++) cout << "Hello World!" << endl; } return(0);

Files Data in Main Memory is “volatile” File: Place for “permanent” data storage C: drive, A: drive, Flash drive, etc. Main Memory progEx2Prob3.exe File main() int num; string firstname; Disk

Output File Streams In C++, the Programmer Can Declare Output Streams #include <iostream> #include <fstream> using namespace std; ofstream outputStream; //ofstream object int num1, num2, num3; cout << “Enter 3 Numbers for datafile: “; cin >> num1 >> num2 >> num3; outputStream.open(“datafile.txt”); outputStream << num1 << “ “; outputStream << num2 << “ “; outputStream << num3; outputStream.close();

Input File Streams In C++, the Programmer Can Declare Input Streams #include <iostream> #include <fstream> using namespace std; ifstream inputStream; //ifstream object int num1, num2, num3; inputStream.open(“datafile.txt”); inputStream >> num1 >> num2 >> num3; inputStream.close();

To Read or Write from a File Declare the File Stream Object Name Associate a File Name with the File Stream Object by Opening the File Read or Write to the File using << or >> Operators Close the File

mydata.txt file 5 8 9.3 Jon 6 14.335 Bill 0 35.67e9 Mary #include <iostream> #include <fstream> #include <string> using namespace std; int main() { ifstream input; int loops, integer, i; float decimal; string name; input.open("mydata.txt"); input >> loops; for(i= 0 ; i < loops; i++) input >> integer; input >> decimal; input >> name; cout << integer << " "; cout << decimal << " "; cout << name << endl; } return(0); mydata.txt file 5 8 9.3 Jon 6 14.335 Bill 0 35.67e9 Mary -23 -4.55 Smith -3 -4e3 xyz Output: 8 9.3 Jon 6 14.335 Bill 0 3.567e+010 Mary -23 -4.55 Smith -3 -4000 xyz

Functions Lines of code with a Name() Name()can be executed over and over again in the same program.