Using string type variables

Slides:



Advertisements
Similar presentations
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
Advertisements

1 9/13/06CS150 Introduction to Computer Science 1 Type Casting.
What Data Do We Have? Sections 2.2, 2.5 August 29, 2008.
Character I/O. COMP104 Character I/O Slide 2 Data Type: char * Constant declaration const char star = '*'; * Variable declaration char resp; * Variable.
Lecture 18:Topic: I/O Formatting and Files Dale/Weems/Headington
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
Computer Science 1620 Strings. Programs are often called upon to store and manipulate text word processors chat databases webpages etc.
Programming is instructing a computer to perform a task for you with the help of a programming language.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
1 Chapter 4 Program Input and the Software Design Process.
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
February 11, 2005 More Pointers Dynamic Memory Allocation.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
1 CS161 Introduction to Computer Science Topic #3.
C++ Basics #7 Basic Input and Output. In this video Standard output (cout) Standard input (cin) stringstream.
Review the following : Flowcharting Variable declarations Output Input Arithmetic Calculations Conditional Statements Loops.
Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect.
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
CS Class 03 Topics  Sequence statements Input Output Assignment  Expressions Read pages Read pages 40 – 49 for next time.
1 Character Strings (Cstrings) Reference: CS215 textbook pages
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.
Operating System Using setw and setprecision functions Using setiosflags function Using cin function Programming 1 DCT
More about strings in C++. String member functions The next three slides present information about functions that are members of the C++ string class.
Program Input and the Software Design Process ROBERT REAVES.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
Math Operators and Output Formatting. Incrementing and Decrementing StatementEquivalent Counter++;Counter = Counter + 1; ++Counter;Counter = Counter +
Slide 1 Chapter 9 Strings. Slide 2 Learning Objectives  An Array Type for Strings  C-Strings  Character Manipulation Tools  Character I/O  get, put.
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.
Chapter 1.2 Introduction to C++ Programming
Basics (Variables, Assignments, I/O)
What Actions Do We Have Part 1
Chapter 1.2 Introduction to C++ Programming
LESSON 2 Basic of C++.
CMSC 202 Lesson 2 C++ Primer.
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Strings (part 2) Dr. Xiao Qin Auburn.
Chapter 2 Assignment and Interactive Input
getline() function with companion ignore()
Quiz Next Monday.
Dynamic Memory Allocation Reference Variables
Variables with Memory Diagram
CS 1430: Programming in C++ Turn in your Quiz1-2 No time to cover HiC.
Basics (Variables, Assignments, I/O)
Starting Out with C++: From Control Structures through Objects
Learning Objectives String Class.
Basic Input and Output C++ programs can read and write information using streams A simple input stream accepts typed data from a keyboard A simple output.
Starting Out with C++: From Control Structures through Objects
String in C++.
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Wednesday 09/23/13.
Chapter 3 Input output.
CPS120: Introduction to Computer Science
Programs written in C and C++ can run on many different computers
CHAPTER 2 Arrays and Vectors.
CHAPTER 4 File Processing.
What Actions Do We Have Part 1
Reading from and Writing to Files
CHAPTER 2 Arrays and Vectors.
(Dreaded) Quiz 2 Next Monday.
CS31 Discussion 1D Winter19: week 4
Programming Strings.
CS31 Discussion 1D Fall18: week 2
Chapter 1 c++ structure C++ Input / Output
CSE Module 1 A Programming Primer
Dr. Khizar Hayat Associate Prof. of Computer Science
getline() function with companion ignore()
Reading from and Writing to Files
CSE Module 1 A Programming Primer
Presentation transcript:

Using string type variables Section 3.4 day 1 Using string type variables

The ASCII Character Set 41 = ')'

Using string: Input and Output #include <iostream> #include <string> using namespace std; int main() { string name; cout << "Enter your name: "; cin >> name; cout << "Your name is " << name << endl; return 0; } >> reads a word up to a space or newline character Type the above code into a new project. The next slide will tell us what else we need to do before compilation.

Using string: getline #include <iostream> #include <string> int main() { string name; cout << "Enter your full name: "; getline(cin, name); cout << "Your full name is " << name << endl; return 0; } getline reads an entire line, including blanks, up to a newline character

Using >> Before getline #include<iostream> #include<string> // program segment below string name; int age; cout << "Enter your age: "; cin >> age; cout << "Enter your full name: "; getline(cin, name); cout << "Your age is " << age << endl; cout << "Your full name is " << name << endl; Do you see any problems with this segment of code? Type and run a program that includes this segment. getline encounters the trailing newline after the age, stores an empty string in name, and does not wait for user input

Reverse the order of the inputs, with getline being called before >>. apstring name; int age; cout<<"Enter your full name:"; getline(cin,name); cout<<"Enter your age:"; cin>>age; cout << name << “,you are ”<<age<<endl;

Or use cin.ignore(); before getline #include <iostream> #include <string> string name; int age; cout << "Enter your age: "; cin >> age; cin.ignore(); // consume \n cout << "Enter your full name: "; getline(cin, name); cout << "Your age is " << age << endl; cout << "Your full name is " << name << endl;

Memory for String Variables When a string variable is declared, no memory is initially provided. When a value is assigned to a string variable, the computer automatically adjusts the memory to accommodate the number of characters to be stored.

apstring Member Functions

Group Activity Write a program that will prompt the user for the name of the school and save it into a single variable of the string data type. Your program will output the users school name and the string length as you see below: Your school name is NA Intermediate High School. There are 27 characters in this name. (including space characters) Raise your hand when your program functions properly.