⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tut3-2.html

📁 a Complete C++ language tutorial on the cplusplus.com
💻 HTML
📖 第 1 页 / 共 2 页
字号:
  do {
    szOut[n] = szIn[n];
  } while (szIn[n++] != '\0');
}

int main ()
{
  char szMyName [20];
  setstring (szMyName,"J. Soulie");
  cout << szMyName;
  return 0;
}
</PRE></TT>
</TD><TD BGCOLOR="silver" WIDTH=50% VALIGN="top">
<B><TT>J. Soulie</TT></B>
</TD></TR></TABLE>
</CENTER>
<P>
Another frequently used method to assign values to an array is by directly using 
the input stream (<tt><b>cin</b></tt>). In this case the value of the string is assigned
by the user during program execution.
<p>
When <tt><b>cin</b></tt> is used with strings of characters it is usually used with its
<tt><b>getline</b></tt> method, that can be called following this prototype:
<blockquote><tt>
<b>cin.getline ( char </b><i>buffer</i><b>[], int </b><i>length</i><b>, char</b><i> delimiter</i><b> = ' \n');</b>
</tt></blockquote>
where <tt><b><i>buffer</i></b></tt> is the address of where to store the input
(like an array, for example), <tt><b><i>length</i></b></tt> is the maximum length of the
buffer (the size of the array) and <tt><b><i>delimiter</i></b></tt> is the character used
to determine the end of the user input, which by default - if we do not include that
parameter - will be the newline character
(<tt><b>'\n'</b></tt>).
<p>
The following example repeats whatever you type on
your keyboard. It is quite simple but serves as an example of how you can use
<tt><b>cin.getline</b></tt> with strings:

<P><CENTER>
<TABLE WIDTH=100% CELLPADDING=5 CELLSPACING=5><TR><TD BGCOLOR="#FFFFBF" WIDTH=50% VALIGN="top">
<TT><PRE><I>// cin with strings</I>
#include &lt;iostream.h&gt;

int main ()
{
  char mybuffer [100];
  cout &lt;&lt; "What's your name? ";
  cin.getline (mybuffer,100);
  cout &lt;&lt; "Hello " &lt;&lt; mybuffer &lt;&lt; ".\n";
  cout &lt;&lt; "Which is your favourite team? ";
  cin.getline (mybuffer,100);
  cout &lt;&lt; "I like " &lt;&lt; mybuffer &lt;&lt; " too.\n";
  return 0;
}
</PRE></TT>
</TD><TD BGCOLOR="silver" WIDTH=50% VALIGN="top">
<TT><B>What's your name? </B>Juan<BR>
<B>Hello Juan.<BR>
Which is your favourite team? </B>Inter Milan<BR>
<B>I like Inter Milan too.</B></TT>
</TD></TR></TABLE>
</CENTER>
<P>
Notice how in both calls to <tt><b>cin.getline</b></tt> we used the same string identifier
(<tt><b>mybuffer</b></tt>). What the program does in the second call is simply step on
the previous content of <tt><b>buffer</b></tt> with the new one that is introduced.
<p>
If you remember the section about communication through the console, you will remember
that we used the extraction operator (<tt><b>&gt;&gt;</b></tt>) to receive data directly
from the standard input.  This method can also be used instead
of <tt><b>cin.getline</b></tt> with strings of characters. For example, in our program,
when we requested an input from the user we could have written:
<blockquote><tt>cin &gt;&gt; mybuffer;</tt></blockquote>
this would work, but this method has the following limitations that
<TT><B>cin.getline</B></TT> has not:
<ul>
<li>It can only receive single words (no complete sentences) since this method uses as
a delimiter any occurrence of a blank character, including spaces, tabulators,
newlines and carriage returns.
<li>It is not allowed to specify a size for the buffer. That makes your program
unstable in case the user input is longer than the array that will host it.
</ul>
For these reasons it is recommended that whenever you require strings of characters
coming from <tt><b>cin</b></tt> you use <tt><b>cin.getline</b></tt> instead of
<tt><b>cin &gt;&gt;</b></tt>.

<P>
<H2>Converting strings to other types</H2>
Due to that a string may contain representations of other data types like numbers, it might
be useful to translate that content to a variable of a numeric type. For example, a string
may contain <TT><B>"1977"</B></TT>, but this is a sequence of 5 chars not so easily
convertable to a single integer data type. The <TT><B>cstdlib</B></TT>
(<TT><B>stdlib.h</B></TT>) library provides three useful functions for this purpose:
<UL>
<LI><B>atoi:</B> converts string to <TT><B>int</B></TT> type.
<LI><B>atol:</B> converts string to <TT><B>long</B></TT> type.
<LI><B>atof:</B> converts string to <TT><B>float</B></TT> type.
</UL>
All of these functions admit one parameter and return a value of the requested type
(<TT>int</TT>, <TT>long</TT> or <TT>float</TT>). These functions combined with
<TT><B>getline</B></TT> method of <TT><B>cin</B></TT> are a more reliable way to get
the user input when requesting a number than the classic <TT><B>cin&gt;&gt;</B></TT>
method:

<P><CENTER>
<TABLE WIDTH=100% CELLPADDING=5 CELLSPACING=5><TR><TD BGCOLOR="#FFFFBF" WIDTH=50% VALIGN="top">
<TT><PRE><I>// cin and ato* functions</I>
#include &lt;iostream.h&gt;
#include &lt;stdlib.h&gt;

int main ()
{
  char mybuffer [100];
  float price;
  int quantity;
  cout &lt;&lt; "Enter price: ";
  cin.getline (mybuffer,100);
  price = atof (mybuffer);
  cout &lt;&lt; "Enter quantity: ";
  cin.getline (mybuffer,100);
  quantity = atoi (mybuffer);
  cout &lt;&lt; "Total price: " &lt;&lt; price*quantity;
  return 0;
}
</PRE></TT>
</TD><TD BGCOLOR="silver" WIDTH=50% VALIGN="top">
<TT><B>Enter price: </B>2.75<BR>
<B>Enter quantity: </B>21<BR>
<B>Total price: 57.75</B></TT>
</TD></TR></TABLE>
</CENTER>

<P>
<H2>Functions to manipulate strings</H2>
The <B>cstring</B> library (<TT>string.h</TT>) defines many functions to
perform manipulation operations with C-like strings (like already explained strcpy).
Here you have a brief look at the most usual:
<DL>
<DT><B><A HREF="/ref/cstring/strcat.html">strcat</A>:</B> &nbsp; <FONT COLOR="#0000ff"><TT><B>char* strcat (char* </B><I>dest</I><B>, const char* </B><I>src</I><B>);</B></TT></FONT>
<DD>Appends <I>src</I> string at the end of <I>dest</I> string. Returns <I>dest</I>.

<P>
<DT><B><A HREF="/ref/cstring/strcmp.html">strcmp</A>:</B> &nbsp; <FONT COLOR="#0000ff"><TT><B>int strcmp (const char* </B><I>string1</I><B>, const char* </B><I>string2</I><B>);</B></TT></FONT>
<DD>Compares strings <I>string1</I> and <I>string2</I>. Returns <TT>0</TT> is both strings are equal.

<P>
<DT><B><A HREF="/ref/cstring/strcpy.html">strcpy</A>:</B> &nbsp; <FONT COLOR="#0000ff"><TT><B>char* strcpy (char* </B><I>dest</I><B>, const char* </B><I>src</I><B>);</B></TT></FONT>
<DD>Copies the content of <I>src</I> to <I>dest</I>. Returns <I>dest</I>.

<P>
<DT><B><A HREF="/ref/cstring/strlen.html">strlen</A>:</B> &nbsp; <FONT COLOR="#0000ff"><TT><B>size_t strlen (const char* </B><I>string</I><B>);</B></TT></FONT>
<DD>Returns the length of <I>string</I>.

</DL>

<P>
NOTE: <TT><B>char*</B></TT> is the same as <TT><B>char[]</B></TT>
<P>
Check the <A HREF="http://www.cplusplus.com/ref/">C++ Reference</A> for extended
information about these and other functions of this library.

<!--cuatut-->
<P>
<CENTER><TABLE WIDTH=100% CELLPADDING=0 CELLSPACING=0 BORDER=0>
 <TR><TD BGCOLOR="#0000FF"><IMG SRC="head0.gif" WIDTH=2 HEIGHT=2></TD></TR>
 <TR><TD ALIGN="right"><FONT FACE="arial,helvetica" SIZE=1>&copy; The C++ Resources Network, 2000-2003 - All rights reserved</FONT></TD></TR>
</TABLE></CENTER>
<P>
<CENTER>
<TABLE CELLPADDING=0 WIDTH=100%>
<TR><TD ALIGN="right" WIDTH=45%><A HREF="tut3-1.html">
 <IMG SRC="butnback.gif" ALIGN="right" BORDER=0>
 Previous:<BR><B>3-1. Arrays</B></A></TD>
<TD ALIGN="center" WIDTH=10%><A HREF="index.html">
 <IMG SRC="butnindx.gif" BORDER=0><BR>
 index</A></TD>
<TD ALIGN="left" WIDTH=45%><A HREF="tut3-3.html">
 <IMG SRC="butnnext.gif" ALIGN="left" BORDER=0>
 Next:<BR><B>3-3. Pointers</B></A>
</TD></TR></TABLE>
</CENTER>
<!--/cuatut-->

</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -