Module 5: I/O and Strings #1 2000/01Scientific Computing in OOCourse code 3C59 Module 5: I/O and STRINGS In this module we will cover The C++ input and.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Chapter 3 DATA: TYPES, CLASSES, AND OBJECTS. Chapter 3 Data Abstraction Abstract data types allow you to work with data without concern for how the data.
CS 1620 File I/O. So far this semester all input has been from keyboard all output has been to computer screen these are just two examples of where to.
C Strings. The char Data Type for Storing Characters The char data type can is used to declare a variable that can hold a single character. Examples:
Week 2: Primitive Data Types 1.  Programming in Java  Everything goes inside a class  The main() method is the starting point for executing instructions.
CSE202: Lecture 2The Ohio State University1 Variables and C++ Data Types.
Chapter 10.
Engineering Problem Solving With C++ An Object Based Approach Chapter 6 One-Dimensional Arrays.
Lecture 1 The Basics (Review of Familiar Topics).
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
CS 117 Spring 2002 Review for Exam 3 arrays strings files classes.
Program Input and the Software Design Process ROBERT REAVES.
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
C++ Programming Language Day 1. What this course covers Day 1 – Structure of C++ program – Basic data types – Standard input, output streams – Selection.
Section 2 - More Basics. The char Data Type Data type of a single character Example char letter; letter = 'C';
Agenda Review Compiling Review Data Types Integer Division Composition C++ Mathematical Functions User Input Reading: , 8.11 Homework #3.
Stream Handling Streams - means flow of data to and from program variables. - We declare the variables in our C++ for holding data temporarily in the memory.
In Addition... To the string class from the standard library accessed by #include C++ also has another library of string functions for C strings that can.
STREAMS AND FILES OVERVIEW.  Many programs are "data processing" applications  Read the input data  Perform sequence of operations on this data  Write.
Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value.
Chapter 6 One-Dimensional Arrays ELEC 206 Computer Tools for Electrical Engineering.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
1 CS 101 Lecture 2. 2 Input from the Keyboard Here is a program to accept input from the keyboard and put into into two variables. #include main(){ cout.
Functions Why we use functions C library functions Creating our own functions.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
CS 206 Introduction to Computer Science II 09 / 10 / 2009 Instructor: Michael Eckmann.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
C++ for Engineers and Scientists Second Edition Chapter 8 I/O File Streams and Data Files.
File I/O ifstreams and ofstreams Sections 11.1 &
Module 3: Steering&Arrays #1 2000/01Scientific Computing in OOCourse code 3C59 Module 3: Algorithm steering elements If, elseif, else Switch and enumerated.
Lecture #6 OPERATORS AND ITS TYPES By Shahid Naseem (Lecturer)
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
Module 8: more on methods #1 2000/01Scientific Computing in OOCourse code 3C59 Module 8: More on methods: In this module we will cover: Overloading of.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Vladimir Misic: Characters and Strings1Tuesday, 9:39 AM Characters and Strings.
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
Module 4: I/O and Strings #1 2000/01Scientific Computing in OOCourse code 3C59 Module 4: I/O In this module we will cover Keyboard/screen input and output.
C++ String Class nalhareqi©2012. string u The string is any sequence of characters u To use strings, you need to include the header u The string is one.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++
Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script.
CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]
Chapter 11 Standard C++ Strings and File I/O Dept of Computer Engineering Khon Kaen University.
Streams One of the themes of this course is that everything can be reduced to simple (and similiar) concepts. Streams are one example. Keyboard and Screen.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files.
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
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++ 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.
CS 1430: Programming in C++ 1. File Input in VS Project Properties Debugging Command Arguments quiz8-1.out We want to know how to do it ourselves, right?
Module 9: Operator overloading #1 2000/01Scientific Computing in OOCourse code 3C59 Module 9: Operator Overloading In this module we will cover Overloading.
CSE 232: Moving Data Within a C++ Program Moving Data Within a C++ Program Input –Getting data from the command line (we’ve looked at this) –Getting data.
Basic concepts of C++ Presented by Prof. Satyajit De
ifstreams and ofstreams
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists File I/O Dr. Xiao Qin Auburn University.
Introduction to C++ October 2, 2017.
COMP 2710 Software Construction File I/O
Today’s Lecture I/O Streams Tools for File I/O
File I/O in C++ I.
Engineering Problem Solving with C++, Etter
CS150 Introduction to Computer Science 1
ifstreams and ofstreams
Today’s Objectives 28-Jun-2006 Announcements
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
An Introduction to STL.
File I/O in C++ I.
Presentation transcript:

Module 5: I/O and Strings #1 2000/01Scientific Computing in OOCourse code 3C59 Module 5: I/O and STRINGS In this module we will cover The C++ input and output streams cin, cout, cerr C++ file handling The C++ string class

Module 5: I/O and Strings #2 2000/01Scientific Computing in OOCourse code 3C59 Aims of this module You do not get far with any program without the need to either inputto and/or output something from the program. This is particularly true in scientific analysis where the very minimum is likely to be a file of data points to analyse. This module covers the things you need to know to deal with user and file I/O In several precediing modules we have used "hands on examples" cout > to write things to the screen and read in from the keyboard. In this module the aim is to cover these more formally. We then show you how to get things from files. Finally, and a little out of place, we introduce the string class which provides a convenient way to deal with text strings. We do this here as it naturally fits with I/O.

Module 5: I/O and Strings #3 2000/01Scientific Computing in OOCourse code 3C STRINGS Old style C character handling (which we havnt covered) is awkward to use. By constrast, the C++ string class lets you handle text strings in a fairly intuitive way. These are easier to use and provide many more things for you #include // declare a string // it is empty string a; // declare a second string // but use the constructor that takes // a string of characters in quotes // like this: string b(“Hello World!”);

Module 5: I/O and Strings #4 2000/01Scientific Computing in OOCourse code 3C STRINGS You can set a string with the assign ( ) method #include //declare a string string a(“Hello World!”); //declare a second string //it starts off empty... string b; b.assign(a); // b is now // “Hello World!” b.assign(“Farewell!”); // b is now // “Farewell!”

Module 5: I/O and Strings #5 2000/01Scientific Computing in OOCourse code 3C STRINGS You can concatenate (add one string to the end of another) string objects with the append( ) method #include //declare a string string a(“Hello World!”); //declare a second string //it starts off empty... string b(“ And Goodbye!”); a.append(b); // a is now // “Hello World! And Goodbye!”

Module 5: I/O and Strings #6 2000/01Scientific Computing in OOCourse code 3C SOME MORE METHODS OF STRING #include string a(“Hello World!”); string b(“Goodbye cruel world!”); //you can find out how long they are int lengthOfa = a.size(); //you can access characters inside bool containsH = false; for (int i = 0; i < lengthOfa; i++){ if (a.at(i) == ‘H’) { containsH = true; } As always C++ counts from 0 to n-1 You can find out how long the string is with size( ) You can find out what character is at a certain position in the string with at( ) - this returns a C char Note single quotes for a C char

Module 5: I/O and Strings #7 2000/01Scientific Computing in OOCourse code 3C COMPARISONS OF STRINGS #include string a(“Hello World!”); string b(“Hello World!”); int test = a.compare(b); if (test == 0) { // string a was equal to string b... } You can use the compare( ) method on strings to see if one string is less than, equal to, or greater than another The compare( ) method returns an int: negative if you are less than this string I’ve given you 0 if you are equal to this string I’ve given you positive if you are greater than this string I’ve given you

Module 5: I/O and Strings #8 2000/01Scientific Computing in OOCourse code 3C COMPARISONS OF STRINGS #include string author1(“Clarke”); string author2(“Phillips”); string firstNameOnPaper; int test = author1.compare(author2); if (test < 0) { firstNameOnPaper = author1; } else { firstNameOnPaper = author2; } //sadly Clarke gets to be first Beware - there is a subtlety about what “greater than”, “less than” and “equal to” mean You might think the order would be “in alphabetical order”. But is “a” equal to “A”? And is “1” greater than “a”? The computer codes each symbol as an integer number (most use a system called ASCII to translate from numbers to symbols) ‘A’ and ‘a’ are different symbols and “Alpha” might be greater than or less than “alpha” on different systems So you can use compare to put things in alphabetical order but be a bit careful

Module 5: I/O and Strings #9 2000/01Scientific Computing in OOCourse code 3C59 A NOTE TO THE CLEVER PEOPLE WAY AHEAD OF THE REST OF US You may feel that using myString.assign(anotherString); and having to use an integer like int test = author1.compare(author2); is a bit painful when what we really want to say is myString = anotherString; if (author1 < author2) {... } There is a way to do that in C++ It is called operator overloading and we haven’t covered it yet

Module 5: I/O and Strings # /01Scientific Computing in OOCourse code 3C A C++ STUPIDITY #include string filename(“Degrees.dat”); ofstream myOutputFile(filename); If any sane person had written the C++ standard, this code would work: But it doesn’t, I’m afraid Instead you have to use the method of string called c_str to give you the C-style representation of the string before it will work: #include string filename(“Degrees.dat”); ofstream myOutputFile(filename.c_str());

Module 5: I/O and Strings # /01Scientific Computing in OOCourse code 3C59 Student exercise Sometime ago you wrote a little program that sorted numbers We’re going to write a class called GuestList that holds a list of guests’ names for us and can print them out in alphabetical order We will use the same algorithm as our number sorting program Copy my main program- it sets things up to read in the names from a file Copy my guestlst.hand guestlst.cxx files Implement the method void GuestList::sortIntoAlphabeticalOrder( ) that takes the array of strings held by the object and sorts them into alphabetical order main.cpp guestlst.hpp guestlst.cpp

Module 5: I/O and Strings # /01Scientific Computing in OOCourse code 3C59 Summary of Module 13: STRINGS AND I/O In this module we have covered: Input and output with iostreams cin cout and cerr file i/o with ofstream and ifstream The string class assign( ) compare( ) size( ) at( )