String Variables and Output/Input. Adding Strings and Literals to Your Programming Skills and output/input formatting

Similar documents
Software Design & Programming I

The C++ Language. Output. Input and Output. Another type supplied by C++ Very complex, made up of several simple types.

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

CS 151 Review #3. // More than one variable can be defined // in a statement. Multiple variables are // separated by a comma.

I/O Streams and Standard I/O Devices (cont d.)

Lecture 3. Input and Output. Review from last week. Variable - place to store data in memory. identified by a name should be meaningful Has a type-

CS2141 Software Development using C/C++ Stream I/O

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

BITG 1233: Introduction to C++

Week 3: File I/O and Formatting 3.7 Formatting Output

Formatting outputs String data type Interactive inputs File manipulators. Access to a library that defines 3. instead, a library provides input

Chapter 3 - Notes Input/Output

C++ Input/Output: Streams

Piyush Kumar. input data. both cout and cin are data objects and are defined as classes ( type istream ) class

The C++ Language. Arizona State University 1

Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Definition Matching (10 Points)

Introduction to C++ (Extensions to C)

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

CHAPTER 3 Expressions, Functions, Output

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

! A literal represents a constant value used in a. ! Numbers: 0, 34, , -1.8e12, etc. ! Characters: 'A', 'z', '!', '5', etc.

3.1. Chapter 3: The cin Object. Expressions and Interactivity

Chapter Overview. I/O Streams as an Introduction to Objects and Classes. I/O Streams. Streams and Basic File I/O. Objects

Expressions, Input, Output and Data Type Conversions

Getting started with C++ (Part 2)

Chapter 6. I/O Streams as an Introduction to Objects and Classes. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

3.1. Chapter 3: The cin Object in Program 3-1. Displaying a Prompt 8/23/2014. The cin Object

Chapter 3: Expressions and Interactivity

3.1. Chapter 3: Displaying a Prompt. Expressions and Interactivity

CPE Summer 2015 Exam I (150 pts) June 18, 2015

Chapter 2: Introduction to C++

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Chapter 3: Expressions and Interactivity. Copyright 2012 Pearson Education, Inc. Thursday, October 9, 14

Fundamentals of Programming CS-110. Lecture 2

Engineering Problem Solving with C++, Etter/Ingber

LECTURE 02 INTRODUCTION TO C++

This watermark does not appear in the registered version - Slide 1

2 nd Week Lecture Notes

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

The cin Object. cout << "Enter the length and the width of the rectangle? "; cin >> length >> width;

We can even use the operator << to chain the output request as:

C++ Input/Output Chapter 4 Topics

Chapter 1. C++ Basics. Copyright 2010 Pearson Addison-Wesley. All rights reserved

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Strings and Streams. Professor Hugh C. Lauer CS-2303, System Programming Concepts

Chapter 2: Basic Elements of C++

What we will learn about this week:

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Your First C++ Program. September 1, 2010

Chapter 12. Streams and File I/O

Lab 6. Review of Variables, Formatting & Loops By: Dr. John Abraham, Professor, UTPA

Chapter 2. Outline. Simple C++ Programs

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

The sequence of steps to be performed in order to solve a problem by the computer is known as an algorithm.

Integer Data Types. Data Type. Data Types. int, short int, long int

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Lab # 02. Basic Elements of C++ _ Part1

Should you know scanf and printf?

Chapter 3: Input/Output

Sample Code: OUTPUT Daily Highs & Lows

Window s Visual Studio Output

Streams. Parsing Input Data. Associating a File Stream with a File. Conceptual Model of a Stream. Parsing. Parsing

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 2. C++ Basics

1/25/2018. ECE 220: Computer Systems & Programming. Write Output Using printf. Use Backslash to Include Special ASCII Characters

Streams - Object input and output in C++

Standard I/O in C and C++

Objectives. In this chapter, you will:

Programming with C++ as a Second Language

Program Organization and Comments

Sequential Program Execution

Chapter Overview. C++ Basics. Variables and Assignments. Variables and Assignments. Keywords. Identifiers. 2.1 Variables and Assignments

Introduction to Programming EC-105. Lecture 2

CSc 10200! Introduction to Computing. Lecture 4-5 Edgardo Molina Fall 2013 City College of New York

Arrays. Lecture 9 COP 3014 Fall October 16, 2017

Unit 3, Lesson 2 Data Types, Arithmetic,Variables, Input, Constants, & Library Functions. Mr. Dave Clausen La Cañada High School

C++ PROGRAMMING. For Industrial And Electrical Engineering Instructor: Ruba A. Salamh

CHAPTER 3 BASIC INSTRUCTION OF C++

Chapter 7: User-Defined Simple Data Types, Namespaces, and the string Type

1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A.

Streams and Basic File I/O Tools for Stream I/O Character I/O Inheritance

BITG 1113: Array (Part 2) LECTURE 9

Chapter 21 - C++ Stream Input/Output

Chapter 1 Introduction to Computers and C++ Programming

Page. No. 1/15 CS201 Introduction to Programmming Solved Subjective Questions From spring 2010 Final Term Papers By vuzs Team

We will exclusively use streams for input and output of data. Intro Programming in C++

Input and Output. Data Processing Course, I. Hrivnacova, IPN Orsay

Structured Programming Using C++ Lecture 2 : Introduction to the C++ Language. Dr. Amal Khalifa. Lecture Contents:

Streams. Rupesh Nasre.

Chapter 12. Streams and File I/O. Copyright 2010 Pearson Addison-Wesley. All rights reserved

CS 1044 Programming in C++ Test 1 READ THIS NOW! Failure to read and follow the instructions below may result in severe penalties.

Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples.

CSCE 206: Structured Programming in C++

CSCE 206: Structured Programming in C++

Characters, c-strings, and the string Class. CS 1: Problem Solving & Program Design Using C++

C++ As A "Better C" Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan.

Lecture 4 Tao Wang 1

Creating a C++ Program

Transcription:

String Variables and Output/Input Adding Strings and Literals to Your Programming Skills and output/input formatting

A group of characters put together to create text is called a string. Strings are one of the most useful kinds of data in a program Strings help programs communicate with the user and allow computers to process data other than numbers You have already used strings to ask for information from the user

Introduction to Strings and Literals Values that are keyed directly into the source code are often called hard-coded values Values or strings that are hard-coded into the source code are called literals. A hard-coded numeric value is called a numeric literal. A string of text that is hard coded is called a string literal

Introduction to Strings and Literals cont. A single character can also be hard coded A character literal appears in single quotes A string literal appears in double quotes You have already used them in cout statements

Examples of Literals X = 6.3; // 6.3 is a numeric literal cout << Hello ; // Hello is a string literal MyChar = A ; // a is a character literal Literals don t change when the program runs

Null Terminator Tells the compiler the string has ended An invisible character at the end of a string literal The character represent by the ASCII value zero. In C++ it is \0 together representing one character

M A R K G E N T R Y \0 Null Terminator

Character Arrays Used to store strings that change as the program runs. A group of variables of the same data type that appear together in the computer s memory A group of variables of the type char. Each char variable holds an ASCII value fo the character, and the null terminator. You can devote exactly the amount of memory you need to strings and easily manipulate individual characters in strings.

Declaring Character Arrays Characters K A L E Y \0 ASCII Value 75 65 76 69 89 0 Char student_name[21]; is and example of a declared character array This allows an the string variable student-name to contain 20 printable characters and the null terminator. ALWAYS save room for the null terminator when declaring character arrays that have different lengths.

Zip Codes and Telephone Numbers Phone numbers in character arrays can include parentheses and hyphens which are not allowed in numeric data types

Using the String Class To use the string class, you must include a header file in your source code In most cases it is #include apstring.h, #include apstring.cp or #include<string>

strcpy Function Pronounced string copy A sting literal can be stored in a character array or one character array can be copied to another Have to be sure the character array receiving the new data is long enough to house it

strcpy(student-name, Aliver Villarreal ); Used to store a name in the student-name array strcpy(destination-array, source-array); Used to copy the contents of one character array to another ** C++ does not prevent you from storing a string that is too long for the character array, therefore it spills over into other storage spaces and can crash the computer **

Formatting Output \n ; is called the new line character or the end-of-line character. Causes the cursor to return to the next line of the screen when the output is a single character much like the enter key on the keyboard Double quotes must be used if it is used in conjunction with other characters. Ex. cout << String\n ;

Formatting Output cont. \t generates a tab character to move the cursor to the next tab stop \\ prints a backslash (\) \ prints a single quote \ prints a double quote mark ( ) endl; use in place of the character \n For string literals, use << endl; following the quotations

Using setf And unsetf left left-justifies the output right right-justifies the output showpoint displays decimal point and trailing zeros for all floating-point numbers, even if the decimal paces are not needed uppercase displays the e om E-notation as E rather than e showpos displays a leading plus sign before positive values scientific displays floating-point numbers in scientific ( E ) notation fixed displays floating-point numbers in normal notation

Using setf And unsetf example cout.setf(ios::showpoint); Replace showpoint with the other commands. The statement under this statement is the cout with the variable that is to be displayed on the monitor To unset the command use cout.unsetf(ios::***); where *** represents the variable

I/O manipulators - setprecision In order to use the directive #include<iomanip.h> must be included Used in conjunction with the setf(ios::fixed); setprecision manipulator sets the number of decimal places to the number provided in the parentheses ex. cout << setprecision(2) << price << \n the amount in the variable price would show 2 decimal places

I/O manipulators - setw In order to use the directive #include<iomanip.h> must be included setw manipulators are used to change default field widths - ex. cout << setw (10) << I << setw(10) << j << setw(10) << k << endl;

Inputting Characters The get function allows a string containing spaces and almost any other character to be entered into a character array. The string input ends when the user presses Enter. You can specify the maximum number of characters in your statement that you want to allow.

Examples of get Ex. cin.get(student-name, 20); this statement allows for an input of 19 characters as the 20 th is the null terminator Ex. getline(cin, MyString); the MyString is changed to the name of the programmers string object

Flushing the Input Stream cin object is referred to as the input stream The character that was inputted stays in the stream Problem arises because the getline method is looking for any sequence of characters ending ina new line character If it is waiting in the input stream, that s what you get in your string To remedy the problem you must remove the extra characters from the input stream before the getline method is executed. This operation is called flushing the input stream.

Statement for Flushing Always use this statement after the get function, because the get function always leaves a new line character in the stream cin.ignore (80, \n ); This statement tells the program to ignore the next 80 characters in the stream and the \n tells the function to stop ignoring characters when it gets to a new line character. Most programmers use the number 80 to play it safe even though a smaller number might be able to be used.

Use Descriptive Prompts Output prompts that clearly explain the input the program is requesting. If prompting for a telephone number or some other formatted data, you may want to use the prompt to give and example. The more descriptive and clear your prompts, the more likely the user is to enter the information in the form your program is expecting.