CIS 22B - Notes for Wed Oct 25

  Announcements and Reminders

  • Assignment 5 is due Monday
  • On line time tomorrow night, 7:00 pm.  Use access code 124488
  • Midterm is next Wednesday
  • Free flu shot today & tomorrow this week in the Don Bautista room upstairs in the Campus Center, 10 am to 2 p.m.


  C++ string class


Member Functions Prototype Description
constructor string();
string(const char* str);
string(const str& str);
Creates a string object
Returns the lenth of the string
clear clear() Clears the string
c_str c_str() Returns the null-terminated char array contents of the string
find size_t find (const string& str, size_t pos = 0) const;
size_t find (const char* s, size_t pos = 0) const;    
size_t find (char c, size_t pos = 0) const;
Locates text in a string.  Returns npos if not found
substr string substr (size_t pos = 0, size_t len = npos) const; Returns a substring of the string object
Member Operators
= assignment operator= (const string& str);    
string& operator= (const char* s);
string& operator= (char c);
Assigns a value to a string
[ ] index       char& operator[] (size_t pos);
const char& operator[] (size_t pos) const;
Returns the char at a location in the string
+= plus equal string& operator+= (const string& str);
string& operator+= (const char* s);
string& operator+= (char c);
Concatenates text to a string
Non-member Operators
+  plus string operator+ (const string& lhs, const string& rhs);
string operator+ (const string& lhs, const char*   rhs);
string operator+ (const char*   lhs, const string& rhs);    
string operator+ (const string& lhs, char          rhs);
string operator+ (char          lhs, const string& rhs);
Concatenates a string and text.  Returns a new string.
Non-member functions
getline istream& getline (istream& in, string& str, char delim);
istream& getline (istream& in, string& str);
Reads from an input stream into a string
Member Constants
npos Indicates not found



A struct is 
  • an abstract data type
  • a composite type
  • a user-defined type
  • almost the exact same thing as a class in C++ (but we'll get to that later)
Structs contain members, accessible through the . (dot) operator, and maybe through the -> (arrow) operator.


struct something        // struct declaration
   int a;               // members
   float b;
   char c;


something var;          // you can also say "struct something var;"

// assign the members of the something variable (object)
var.a = 19;
var.b = 3.14;
var.c = 'x';

cout << var.a << ' ' << var.b << ' ' << var.c;    // prints 19 3.14 x

something* ps;         // ps is a pointer to something
ps = &var;             // assign the address of a struct variable to a pointer

cout << (*ps).a;       // prints 19
cout << ps->a;         // prints 19

struct initialization

Arrays of structs

Structs with arrays

The tm struct


Lab Exercise #5

Put your name, the compiler used, and Lab Exercise #5 in a comment at the top of your program. Email your source code. This lab exercise is due 11:30 am on Sunday, 10/29.
Write a program to read the input file, shown below and write out the output file shown below.  Use only string objects and string functions to process the data.  Do not use c-string functions or stringstream (or istringstream or ostringstream) class objects for your solution.  

Input File
Cincinnati 27, Buffalo 24
Detroit 31, Cleveland 17
Kansas City 24, Oakland 7
Carolina 35, Minnesota 10
Pittsburgh 19, NY Jets 6
Philadelphia 31, Tampa Bay 20
Green Bay 19, Baltimore 17
St. Louis 38, Houston 13
Denver 35, Jacksonville 19
Seattle 20, Tennessee 13
New England 30, New Orleans 27
San Francisco 32, Arizona 20
Dallas 31, Washington 16

Output File
Cincinnati over Buffalo 27 to 24
Detroit over Cleveland 31 to 17
Kansas City over Oakland 24 to 7
Carolina over Minnesota 35 to 10
Pittsburgh over NY Jets 19 to 6
Philadelphia over Tampa Bay 31 to 20
Green Bay over Baltimore 19 to 17
St. Louis over Houston 38 to 13
Denver over Jacksonville 35 to 19
Seattle over Tennessee 20 to 13
New England over New Orleans 30 to 27
San Francisco over Arizona 32 to 20
Dallas over Washington 31 to 16