CMSC 202 Lesson 2 C++ Primer. Warmup Create an array called ‘data’ Define a constant called DATA_SIZE with value 127 Write a loop to fill the array with.

Slides:



Advertisements
Similar presentations
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Advertisements

Strings.
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.
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.
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:
1 Objectives Understand streamed input and output.
C++ Data Type String A string is a sequence of characters enclosed in double quotes. Examples of strings: “Hello” “CIS 260” “Students” The empty string.
1 CS 105 Lecture 8 Strings; Input Failure Mon, Mar 7, 2011, 3:39 pm.
Computer Science 1620 Other Data Types. Quick Review: checklist for performing user input: 1) Be sure variable is declared 2) Prompt the user for input.
CS31: Introduction to Computer Science I Discussion 1A 5/7/2010 Sungwon Yang
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
String What it is Why it’s useful library routines for handling strings how to input a string from the keyboard.
Programming is instructing a computer to perform a task for you with the help of a programming language.
Section 2 - More Basics. The char Data Type Data type of a single character Example char letter; letter = 'C';
Chapter 5 Arrays and Strings C Programming © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Elements of a C++ program 1. Review Algorithms describe how to solve a problem Structured English (pseudo-code) Programs form that can be translated into.
Chapter 8 Strings and Vectors (8.1 and 8.2). An Array of characters Defined as: char firstName[20]; char firstName[] = {‘T’, ‘i’, ‘m’}; // an array of.
Working with Strings Lecture 2 Hartmut Kaiser
February 14, 2005 Characters, Strings and the String Class.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT8: Characters and Strings CS2311 Computer Programming.
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
CSE 232: C++ Input/Output Manipulation Built-In and Simple User Defined Types in C++ int, long, short, char (signed, integer division) –unsigned versions.
CS31: Introduction to Computer Science I Discussion 1A 4/9/2010 Sungwon Yang
1 C++ Syntax and Semantics, and the Program Development Process.
Basic Elements of C++ Chapter 1.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
Define our own data types We can define a new data type by defining a new class: class Student {...}; Class is a structured data type. Can we define our.
Input/Output Sujana Jyothi C++ Workshop Day 2. C++ I/O Basics 2 I/O - Input/Output is one of the first aspects of programming that needs to be mastered:
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
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.
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.
CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]
CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
Chapter 11 Standard C++ Strings and File I/O Dept of Computer Engineering Khon Kaen University.
CMPSC 121- Spring 2015 Lecture 6 January 23, 2015.
1 What is a Named Constant? A named constant is a location in memory that we can refer to by an identifier, and in which a data value that cannot be changed.
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Intro to Classes via the C++ String Class November 18, 2002 CSE103 - Penn State University Online at
More about strings in C++. String member functions The next three slides present information about functions that are members of the C++ string class.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
Array. Array is a group of data of the same type. Array elements have a common name –The array as a whole is referenced through the common name Individual.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
Chapter Structured Data 11. Combining Data into Structures 11.2.
Chapter 4 Strings and Screen I/O. Objectives Define strings and literals. Explain classes and objects. Use the string class to store strings. Perform.
String in C++. 2 Using Strings in C++ Programs String library or provides functions to: - manipulate strings - compare strings - search strings ASCII.
C++ Basics Lecture 2.
C++ Basic Input and Output (I/O)
Chapter Topics The Basics of a C++ Program Data Types
CMSC 202 Lesson 2 C++ Primer.
Basic Elements of C++.
Chapter 2 part #3 C++ Input / Output
Basic Elements of C++ Chapter 2.
Chapter 3: Input/Output
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
String What it is Why it’s useful
CS150 Introduction to Computer Science 1
Chapter 2 part #3 C++ Input / Output
Using string type variables
CS-161 Computer Programming Lecture 15 & 16: Arrays II
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Presentation transcript:

CMSC 202 Lesson 2 C++ Primer

Warmup Create an array called ‘data’ Define a constant called DATA_SIZE with value 127 Write a loop to fill the array with integers from the user

Intro to C++ Minor differences from C Major additions to C Today  Differences Semester  Additions

Output cout  Output stream  Print to screen <<  Output stream separator  Between every object in stream endl  Output stream end of line character Example: cout << "Hello World!" << endl;

Input cin  Input stream  Standard input (keyboard) >>  Input stream separator  Between every object in stream Example int age; cout << "What is your age?" << endl; cin >> age;

In Action! #include using namespace std; int main( ) { // greet the user cout << "Hello reader.\n" << "Welcome to C++.\n"; // prompt user and get response int numberOfLanguages; cout << "How many programming languages have you used? "; cin >> numberOfLanguages; // determine appropriate response if (numberOfLanguages < 1 ) cout << "Read the preface. You may prefer" << endl << "a more elementary book by the same author" << endl; else cout << "Enjoy the book." << endl; return 0; }

Formatting Decimals Magic code  cout.setf( ios::fixed );  cout.setf( ios::showpoint );  cout.precision( 2 ); Fix & show the decimal with 2 digits of precision  Collection of flags, set until you UN-set them! Example: double temperature = 81.7; cout.setf( ios::fixed ); cout.setf( ios::showpoint ); cout.precision( 2 ); cout << temperature << endl;// 81.70

Casting Old C-style casting double DegreesCelsius( int degreesFahernheit ) { return (double)5 / 9 * ( degreesFahrenheit - 32 ); } New C++-style casting double DegreesCelsius( int degreesFahernheit ) { return static_cast ( 5 ) / 9 * ( degreesFahrenheit - 32 ); } Why?  Compiler makes a check – so it’s safer

Constants Old C-style #defines #define PI ; New C++-style constant variables const double PI = ; Why?  Type-checking!

Enumerations enum  Allows you to define new, limited types  Maps to integers (so comparisons are allowed) Syntax: enum TYPE_NAME { VAL1, VAL2, …, VALN }; Example: enum DAYS { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }; DAYS today = TUESDAY;

Practice! Work with your neighbor Rewrite the warmup to use the following C++ constructs:  cout  cin  const Warmup  Create an array called ‘data’  Define a constant called DATA_SIZE with value 127  Write a loop to fill the array with integers from the user

Review: C-style Strings Declare a new string: char string1[4] = "abc"; char string2[ ] = "abc"; char *stringp = "abc"; // What about this? char string3[ ] = {'a', 'b', 'c'}; What’s the difference?

Review: C-style Strings Library Functions  strcpy -- copy a C-style string  strcat -- concatenate two C-style strings  strlen -- return the length of a C-style string  strcmp -- compare two C-style strings

C++-style Strings Library header file #include Create an empty string string stringName; Create a string with initial value string carMake( “Nissan” ); string carModel = “Pulsar”; Create a copy of a string string newCarMake( carMake );

C++-style Strings Copy value into existing string firstString = secondString; Access individual character (like arrays!) firstString[7] = ‘x’; Compare two strings (==,, !=, …) if (firstString == “ham”) Concatenation string name = firstName + “ “ + lastName;

C++-style Strings Length of string if (fileName.size() > 8) if (fileName.length() > 8) Is string empty? if (fileName.empty()) Treat C++-string as C-string  (HINT: important for file streams!) string s1 = "bob"; char s2[] = "This is a C-style string"; // this code copies s1 onto s2 using strcpy() strcpy (s2, s1.c_str());

C++-style Strings Substring Syntax: stringName.substr( index, length); Example string name = “Your Name”; string your = name.substr(0, 4); cout << your << endl;// “Your”

Input/Output with C++ Strings Output  Same as with other variables… cout << stringName << endl; Example string firstName = “Dana”; string lastName = “Wortman”; cout << firstName << “ “ << lastName;

Input/Output with C++ Strings Input  Same as with other variables  One issue – strings are broken by whitespace cin >> stringName; Example: string name; cout << “Please enter your name” << endl; // User types in: Dana Wortman cin >> name; cout << name << endl;// “Dana” // “Wortman” is waiting // to be read…

Input/Output with C++ Strings Input – Whole lines! Syntax: getline(inputStream, stringName);  Getline reads until an end-of-line character is seen (return!) Example: string name; cout << “Please enter your name” << endl; // User types in: Dana Wortman getline(cin, name); cout << name << endl;// “Dana Wortman”

Strings In Action! string Reverse (string s) { string result(s);// copy of original string int start = 0; // left-most character int end = s.length(); // right-most character // swap chars until end and start // meet in the middle while (start < end) { --end; char c = result[end]; result[end] = result[start]; result[start] = c; ++start; } return result; } R e v e r s e ! ! e s r e v e R

Practice! Prompt for 3 words from the user Read in 3 separate words Concatenate them together into 1 string in REVERSE ORDER Print that string

Challenge! Part 1:  Define a constant representing the minimum “Gateway” GPA (3.0)  Request the user’s GPA for CMSC 201  Print it using 3 decimal points of precision Part 2:  Define a constant representing the computer science major code (CMSC)  Request the user’s 4-letter Major code  Print it Part 3 (use your constants!) :  If the user is a CMSC major and their GPA is above the cuttof Print a congratulatory message  If they are a CMSC major and their GPA is too low Tell them to retake 201  If they are any other major Tell them to change majors to CMSC – because it’s the best!