91.166 Copyright © 2002, Department of Systems and Computer Engineering, Carleton University 1 INPUT STREAMS ifstream xin; // declares an input stream.

Slides:



Advertisements
Similar presentations
C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
Advertisements

 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Strings.
Character String Manipulation. Overview Character string functions sscanf() function sprintf() function.
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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 1 Overview 8.1 An Array Type for Strings 8.2 The Standard string.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
Copyright © 2012 Pearson Education, Inc. Chapter 3: Expressions and Interactivity.
CS31: Introduction to Computer Science I Discussion 1A 5/7/2010 Sungwon Yang
1 String Library and Stream I/O Ying Wu Electrical Engineering & Computer Science Northwestern University ECE230 Lectures Series.
© 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5/e Starting Out with C++: Early Objects 5 th Edition Chapter 2 Introduction.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 3- 1.
Chapter 2: Introduction to C++.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 3: Expressions and Interactivity.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
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.
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
Strings in C. Strings are Character Arrays Strings in C are simply arrays of characters. – Example:char s [10]; This is a ten (10) element array that.
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Seventh.
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.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 2 Introduction to C++
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved.1 Chapter 4 Mathematical Functions, Characters, and Strings.
You gotta be cool. Stream Stream Output Stream Input Unformatted I/O with read, gcount and write Stream Manipulators Stream Format States Stream Error.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
Functions Why we use functions C library functions Creating our own functions.
How to Write a Fortran Jiffy (C)2005 Mark Rould University of Vermont.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
C++ Programming: Basic Elements of C++.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Program Outline // this slide illustrates the basic outline.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 “cin” Overview screen keyboard Everything typed is “echoed”
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Formatting Output.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Department of Electrical and Computer Engineering Introduction to C++: Primitive Data Types, Libraries and Operations By Hector M Lugo-Cordero August 27,
Copyright © 2002, Department of Systems and Computer Engineering, Carleton University CONTROL STRUCTURES Simple If: if (boolean exp) { statements.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Sixth.
Introduction As programmers, we don’t want to have to implement functions for every possible task we encounter. The Standard C library contains functions.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 3 Expressions and Interactivity.
Representing Strings and String I/O. Introduction A string is a sequence of characters and is treated as a single data item. A string constant, also termed.
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
1 Character Strings (Cstrings) Reference: CS215 textbook pages
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction An array is a collection of identical boxes.
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.
Sahar Mosleh California State University San MarcosPage 1 Character String.
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.
2/4/2016Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 2 Simple C++ Programs.
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Characters and Strings
CS 1704 Introduction to Data Structures and Software Engineering.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
ECE 103 Engineering Programming Chapter 29 C Strings, Part 2 Herbert G. Mayer, PSU CS Status 7/30/2014 Initial content copied verbatim from ECE 103 material.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
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.
Today Variable declaration Mathematical Operators Input and Output Lab
Chapter 2: Introduction to C++
C Short Overview Lembit Jürimägi.
Introduction to Programming in Java
Chapter 2: Introduction to C++.
Presentation transcript:

Copyright © 2002, Department of Systems and Computer Engineering, Carleton University 1 INPUT STREAMS ifstream xin; // declares an input stream object (for reading files) xin.open(string); // attaches the input stream object to the file specified xin >> resetiosflags(ios::ws); // turns off whitespace skipping when reading characters xin >> setw(size_of_char_array) >> string; // prevents overflow when reading strings xin.fail() // produces true if the stream is in the failed state (something has gone wrong) xin.eof() // produces true if the program has tried to read past the end of the file xin.clear() // resets the failure flags xin.ignore (count, ch); // discards input characters until “count” characters have been discarded // or character “ch” has been discarded (whichever comes first) xin.get(array, size, ch); // copies input characters into character array “array” until “size – 1” // characters have been copied or character “ch” is seen. in the second // case character “ch” is not read. xin.get(); // gets the next input character xin.getline(array, size); // copies the next line of input (everything up to the next ‘\n’) into the // character array supplied. the ‘\n’ which ends the input process is // discarded. at most “size – 1” characters will get read (if the line is // too long to be read, the extra characters are just left in the pipeline) OUTPUT STREAMS ofstream xout; // declares an output stream object (for reading files) xout << setiosflags (ios::fixed | ios::showpoint); // forces use of non-scientific notation and the display // of digits to the right of the decimal point. // this option remains in effect until changed. xout << setprecision(value); // selects the number of digits to be displayed to the right of the decimal point // when outputting double values. the choiice remains in effect until changed. xout << setwidth (value) << … // indicates that the next value output is to occupy the specified number of // columns. a one shot deal – affects only the next value output xout << setfill(ch); // selects the fill character to be used when padding output values to a specified width. // the choice remains in effect until changed. OPERATORS ! = logical NOT (true is changed to false and vice versa) % = modulus (remainder). may only be used with integral (int or long) values a = b, where is an operator is equivalent to a = a (b) ++a means increment first, then use the new value a++ means use the old value, then increment afterwards if (either operand is double) { the result will be double } else (if either operand is long) { the result will be long } else { the result will be int }

Copyright © 2002, Department of Systems and Computer Engineering, Carleton University 2 LIBRARY FUNCTIONS Mathematical double fabs (double x); returns the absolute value of “x” int abs (int x); same but returns an integer result double sin (double x); returns the sine of “x” (note: “x” is in radians) double cos (double x); returns the cosine of “x” (note: “x” is in radians) double asin (double x); returns the inverse sin of “x” (in radians) double acos (double x); returns the inverse cosine of “x” (in radians) String: int strlen (const char str[]); returns length of string (not counting the termination) void strcpy (char tostr[], const char fromstr[]); copies “fromstr” into “tostr” int strcmp (const char str1[], const char str2[]); compares “str1” to “str2” if str1 > str2, returns a value greater than zero if str1 == str2, returns zero if str1 < str2, returns a value less than zero int stricmp (const char str1[], const char str2[]); as for “strcmp” but ignores case double atof (const char str[]); converts a string to a floating point value int atoi (const char str[]); converts a styring to an integer value Miscellanous int rand(void); returns a “randomly” chosen value between 0 and RAND_MAX – 1 void randomize (void); Borland only – intended to be called once before making use of “rand”. not really necessary. Character: bool isalnum(char ch); returns true if the character is alphanumeric (letter or digit) bool isdigit(char ch); returns true if the character is a digit (one of ‘0’ through ‘9’) char toupper(char ch); returns the uppercase equivalent of a character ASCII CODES ‘a’ = 97 ….. ‘z’ = 122 ‘A’ = 65 …..’Z’ = 90 ‘0’ = 48…..’9’ = 57 MISCELLANOUS Array elements are numbered starting with 0. If an array contains “n” values, the last value is stored at position “n – 1”. Sample structure definition:struct point { double lat; double lng; }; // *** note the semi-colon Global constants (and structure definitions) are OK. Global variables are forbidden.