forked from uva-cs/pdr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmdlineparams.cpp.html
36 lines (34 loc) · 3.59 KB
/
cmdlineparams.cpp.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="GENERATOR" content="GNU source-highlight
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite">
<title>cmdlineparams.cpp</title>
</head>
<body style="background-color:white">
<pre><i><span style="color:#9A1900">// handling command-line parameters</span></i>
<b><span style="color:#000080">#include</span></b> <span style="color:#FF0000"><iostream></span>
<b><span style="color:#0000FF">using</span></b> <b><span style="color:#0000FF">namespace</span></b> std<span style="color:#990000">;</span>
<span style="color:#009900">int</span> <b><span style="color:#000000">main</span></b> <span style="color:#990000">(</span><span style="color:#009900">int</span> argc<span style="color:#990000">,</span> <span style="color:#009900">char</span><span style="color:#990000">*</span> argv<span style="color:#990000">[])</span> <span style="color:#FF0000">{</span>
<i><span style="color:#9A1900">// the command-line parameters are stored as C-style strings in the</span></i>
<i><span style="color:#9A1900">// argv[] array. argc contains 1 more than the number of command</span></i>
<i><span style="color:#9A1900">// line parmaeters specified.</span></i>
<i><span style="color:#9A1900">// the 0th command line parameter is always the program name.</span></i>
<i><span style="color:#9A1900">// we can print a C-style string directly through cout:</span></i>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"This program is called '"</span> <span style="color:#990000"><<</span> argv<span style="color:#990000">[</span><span style="color:#993399">0</span><span style="color:#990000">]</span> <span style="color:#990000"><<</span> <span style="color:#FF0000">"'"</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"The following are the command line parameters you specified: "</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<i><span style="color:#9A1900">// this for loop starts at 1 to avoid printing the name of the program</span></i>
<b><span style="color:#0000FF">for</span></b> <span style="color:#990000">(</span> <span style="color:#009900">int</span> i <span style="color:#990000">=</span> <span style="color:#993399">1</span><span style="color:#990000">;</span> i <span style="color:#990000"><</span> argc<span style="color:#990000">;</span> i<span style="color:#990000">++</span> <span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<i><span style="color:#9A1900">// we can convert the C-style strings into C++-style strings:</span></i>
<span style="color:#008080">string</span> <b><span style="color:#000000">s</span></b><span style="color:#990000">(</span>argv<span style="color:#990000">[</span>i<span style="color:#990000">]);</span>
<i><span style="color:#9A1900">// and then print them out:</span></i>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"</span><span style="color:#CC33CC">\t</span><span style="color:#FF0000">"</span> <span style="color:#990000"><<</span> s <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
<b><span style="color:#0000FF">return</span></b> <span style="color:#993399">0</span><span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
</pre>
</body>
</html>