<HTML>
<HEAD>
<TITLE>vcl06.htm </TITLE>
<LINK REL="ToC" HREF="index.htm">
<LINK REL="Index" HREF="htindex.htm">
<LINK REL="Next" HREF="vclp03.htm">
<LINK REL="Previous" HREF="vcl05.htm"></HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080">
<A NAME="I0"></A><P ALIGN=CENTER>
<A HREF="vcl05.htm" TARGET="_self"><IMG SRC="blanprev.gif" WIDTH = 37 HEIGHT = 37 BORDER = 0 ALT="Previous Page"></A>
<A HREF="index.htm" TARGET="_self"><IMG SRC="blantoc.gif" WIDTH = 37 HEIGHT = 37 BORDER = 0 ALT="TOC"></A>
<A HREF="vclp03.htm" TARGET="_self"><IMG SRC="blannext.gif" WIDTH = 37 HEIGHT = 37 BORDER = 0 ALT="Next Page"></A>
<HR ALIGN=CENTER>
<P>
<UL>
<UL>
<UL>
<LI>
<A HREF="#E68E42" >What You'll Learn</A>
<LI>
<A HREF="#E68E43" >Character Literals and Variables</A>
<LI>
<A HREF="#E68E44" >String Literals</A>
<UL>
<LI>
<A HREF="#E69E47" >String-Literal Endings</A>
<LI>
<A HREF="#E69E48" >String Lengths</A></UL>
<LI>
<A HREF="#E68E45" >Special Characters: Escape Sequences</A>
<LI>
<A HREF="#E68E46" >Integers and Characters</A>
<LI>
<A HREF="#E68E47" >Storing Strings in Arrays</A>
<UL>
<LI>
<A HREF="#E69E49" >Arrays Versus Strings</A>
<LI>
<A HREF="#E69E50" >Getting Strings from the User</A></UL>
<LI>
<A HREF="#E68E48" >Comparing cout and cin</A>
<UL>
<LI>
<A HREF="#E69E51" >Output Options</A>
<LI>
<A HREF="#E69E52" >Input Options</A>
<LI>
<A HREF="#E69E53" >Getting Lines of Data</A></UL>
<LI>
<A HREF="#E68E49" >Homework</A>
<UL>
<LI>
<A HREF="#E69E54" >General Knowledge</A>
<LI>
<A HREF="#E69E55" >What's the Output?</A>
<LI>
<A HREF="#E69E56" >Find the Bug</A>
<LI>
<A HREF="#E69E57" >Write Code That. . .</A>
<LI>
<A HREF="#E69E58" >Extra Credit</A></UL></UL></UL></UL>
<HR ALIGN=CENTER>
<A NAME="E66E16"></A>
<H1 ALIGN=CENTER>
<CENTER>
<FONT SIZE=6 COLOR="#0000A0"><B>-</B><B> 6 </B><B>-</B>
<BR><B>String Data and I/O</B></FONT></CENTER></H1>
<BR>
<UL>
<UL>
<P>character literals
<BR>
</UL></UL>
<UL>
<UL>
<P>delimiter
<BR>
</UL></UL>
<UL>
<UL>
<P>null zero
<BR>
</UL></UL>
<BLOCKQUOTE>
<BLOCKQUOTE>
<P>string literals
<BR>
</BLOCKQUOTE></BLOCKQUOTE>
<BR>
<A NAME="E68E42"></A>
<H3 ALIGN=CENTER>
<CENTER>
<FONT SIZE=5 COLOR="#0000A0"><B>What You'll Learn</B></FONT></CENTER></H3>
<BR>
<UL>
<LI>Character literals and variables
<BR>
<BR>
<LI>String literals
<BR>
<BR>
<LI>Special characters: escape sequences
<BR>
<BR>
<LI>Integers and characters
<BR>
<BR>
<LI>Storing strings in arrays
<BR>
<BR>
<LI>Comparing cout and cin
<BR>
<BR>
</UL>
<P>Visual C++ differs from many other programming languages in that there is no such thing as a character string variable. There are character variables, but they hold only a single character of data. There are character string literals, but no character
string variables. A string is simply a list of characters such as a word, phrase, sentence, or any set of characters strung together. If you have never programmed before, you will not miss string variables. If you have programmed in other languages, you
might question Visual C++'s usefulness because it does not support string variables.
<BR>
<P>The designers of the C++ language decided against using string variables, but C++ offers alternatives to them that are almost as easy to use and much more efficient. After all, Visual C++ must offer a way for you to store people's names and addresses.
This unit shows you how to designate string literals and how to store them in memory.
<BR>
<P>Performing input and output (often referred to as just I/O) is extremely easy with Visual C++. You need a way to get data and results of calculations to the screen and to get input from the user at the keyboard. This book has already used cout and cin
for simple input and output.
<BR>
<P>After you learn about storing character and string data, you will have mastered all the fundamental data types in Visual C++. You will then need some way to output that data. This unit reviews cout and cin, and then explains how to make them work
exactly the way you want.
<BR>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE>Lesson 2's second unit gave a brief overview of character and string data as well as Visual C++'s I/O capabilities. This unit explains all the details that were skipped then. This unit also begins showing you full-length programs again now that
you'll finally understand what all the statements do.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<BR>
<A NAME="E68E43"></A>
<H3 ALIGN=CENTER>
<CENTER>
<FONT SIZE=5 COLOR="#0000A0"><B>Character Literals and Variables</B></FONT></CENTER></H3>
<BR>
<P><B><I><IMG SRC="concept.gif"></I></B>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE>Character literals always appear inside single quotation marks. Define char variables for character data.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<P>All character literals must appear between two single quotation marks. All of the following are character literals:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">'A' 'x' ' ' '*' '&' '1'</FONT></PRE>
<P>As you can see, a character is any character—whether a letter, number, space, or special character—enclosed in single quotes. Never put single quotation marks around something other than a single character of data.
<BR>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE>Remember that C++ is extremely fussy about the characters you type to give it instructions. ' and " have two very different meanings and cannot be used interchangeably.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<P>In the previous unit, you learned how to define a character variable with the char keyword. The following statement defines a character variable:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">char MyGrade; // Defines a character variable</FONT></PRE>
<P>Your program can store a character inside MyGrade using the assignment operator, =. The following assignment statement stores an A in MyGrade:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">MyGrade = 'A'; // Store character data in the character variable</FONT></PRE>
<P>Later in the program, another assignment might store a different value in MyGrade, either by assigning another character literal to the variable or by assigning another variable to MyGrade, such as this:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">MyGrade = oldGrade; // Replace the contents of MyGrade</FONT></PRE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE>
<FONT COLOR="#FF0000">If you know a character variable's initial value at the time that you define the variable, you can assign the variable a value at the same time you define the variable, like this:</FONT></NOTE>
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">char MyGrade = 'A'; // Define and initialize</FONT></PRE>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<P><IMG SRC="stop.gif">Listing 6.1 contains the beginnings of a Visual C++ program that defines and initializes three character variables.
<BR>
<P><B><I><IMG SRC="review.gif"></I></B>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE>Define character variables with char and specify character literals by enclosing them inside single quotation marks.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<NOTE ALIGN=CENTER>
<CENTER><B>Input Listing 6.1. Defining and storing values in three </B><B>character variables.</B></CENTER></NOTE>
<PRE>
<FONT COLOR="#000080">1: void main()
2: {
3: char first, middle, last;
4: first = 'G';
5: middle = 'M';
6: last = 'P';
7: // Rest of program would follow</FONT></PRE>
<P>Output
<P>There is no output from Listing 6.1 because the program is not complete.
<BR>
<P>Analysis
<P>definition
<P>A <I>delimiter</I> is a character used to signal or enclose data.
<P>The code declares three variables, and then assigns three initials
没有合适的资源?快使用搜索试试~ 我知道了~
Sam.s.Teach.Yourself.Visual.C++.In.12.Easy.Lesson_TEACH_sam teac
共43个文件
htm:43个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 85 浏览量
2022-09-24
01:11:48
上传
评论
收藏 380KB ZIP 举报
温馨提示
Sam s Teach Yourself Visual C++ In 12 Easy Lessons
资源推荐
资源详情
资源评论
收起资源包目录
Sam.s.Teach.Yourself.Visual.C++.In.12.Easy.Lesson.zip (43个子文件)
3_Sam's Teach Yourself Visual C++ In 12 Easy Lessons
vcl16.htm 57KB
vcl14.htm 49KB
vclp02.htm 5KB
vcl10.htm 47KB
vclp07.htm 11KB
vcl21.htm 46KB
htindex.htm 150B
vcl01.htm 23KB
vcl13.htm 63KB
vcl12.htm 39KB
vclp08.htm 18KB
vcl23.htm 55KB
vcl11.htm 38KB
vcl03.htm 40KB
vclxd.htm 4KB
vcl04.htm 34KB
vcl06.htm 69KB
vclp11.htm 24KB
vcl07.htm 44KB
vcl22.htm 36KB
vclxb.htm 2KB
vclfm.htm 22KB
vclp01.htm 7KB
vclp10.htm 23KB
vcl08.htm 50KB
vclp06.htm 7KB
vclxc.htm 9KB
vclp04.htm 8KB
index.htm 4KB
vcl24.htm 49KB
vcl02.htm 26KB
vcl09.htm 52KB
vcl17.htm 45KB
vcl18.htm 53KB
vcl05.htm 47KB
vclp03.htm 6KB
vcl19.htm 39KB
vclp12.htm 14KB
vcl15.htm 44KB
vcl20.htm 37KB
vclp05.htm 7KB
vclxa.htm 12KB
vclp09.htm 16KB
共 43 条
- 1
资源评论
小贝德罗
- 粉丝: 70
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功