<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The Stata Things &#187; C++</title>
	<atom:link href="http://enoriver.net/index.php/category/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://enoriver.net</link>
	<description>computing for fun and profit</description>
	<lastBuildDate>Wed, 08 Feb 2012 18:09:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Indenting your code &#8212; convenient tab to space conversion</title>
		<link>http://enoriver.net/index.php/2009/02/28/indenting-your-code-convenient-tab-to-space-conversion/</link>
		<comments>http://enoriver.net/index.php/2009/02/28/indenting-your-code-convenient-tab-to-space-conversion/#comments</comments>
		<pubDate>Sun, 01 Mar 2009 02:52:01 +0000</pubDate>
		<dc:creator>Gabi Huiber</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Stata]]></category>
		<category><![CDATA[Notepad ++]]></category>

		<guid isPermaLink="false">http://enoriver.net/?p=587</guid>
		<description><![CDATA[OK, so UltraEdit is not that expensive and I have yet to see an unfavorable opinion of it. But Notepad++ works fine for me, and as of today it works even better. Like most everybody, I indent my code. I use tabs for that. Tabs are convenient, but they are set to different widths for [...]]]></description>
			<content:encoded><![CDATA[<p>OK, so UltraEdit is not that expensive and I have yet to see an unfavorable opinion of it. But Notepad++ works fine for me, and as of today it works even better.</p>
<p>Like most everybody, I indent my code. I use tabs for that. Tabs are convenient, but they are set to different widths for different systems by default, and that can be annoying. For sets of nested curly brackets three or four levels deep, which between loops, if/else conditions, and function definition syntax can easily happen, wide tabs can make your lines run over the width of the screen.</p>
<p>Notepad++ can fix that. In the "Settings" menu there's a "Preferences" item, and in it there's an "Edit Components" tab. In it there's a tab box, which gives you exactly the two options you need to solve this problem once and for all: the first is the number of spaces equivalent to a tab's width. The default is 4, which is also the preferred width of at least one widely used coding standard -- the Zend Framework, for PHP. The other option is a cool little check-box that reads "Replace by space". </p>
<p>You check that box, and your friends who like indented code but hate tabs will love you.</p>
]]></content:encoded>
			<wfw:commentRss>http://enoriver.net/index.php/2009/02/28/indenting-your-code-convenient-tab-to-space-conversion/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>The class string vs. C-style strings</title>
		<link>http://enoriver.net/index.php/2009/02/07/the-class-string-vs-c-style-strings/</link>
		<comments>http://enoriver.net/index.php/2009/02/07/the-class-string-vs-c-style-strings/#comments</comments>
		<pubDate>Sat, 07 Feb 2009 04:34:02 +0000</pubDate>
		<dc:creator>Gabi Huiber</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[class string]]></category>
		<category><![CDATA[structs]]></category>

		<guid isPermaLink="false">http://enoriver.net/?p=543</guid>
		<description><![CDATA[My previous C++ post illustrated how structs worked. The same example -- reading student names and grades from a .csv file into an array of Student structs -- can be used to illustrate the differences between C-style strings -- null-terminated arrays of characters -- and the class string. The advantage of using the class string, [...]]]></description>
			<content:encoded><![CDATA[<p>My previous C++ post illustrated how <code>struct</code>s worked. The same example -- reading student names and grades from a .csv file into an array of Student structs -- can be used to illustrate the differences between C-style strings -- null-terminated arrays of characters -- and the class <code>string</code>.</p>
<p>The advantage of using the class <code>string</code>, so far, seems to be that it saves the work of array length accounting. The global constant NAMELEN is gone. Here's the new version of the code in the previous post:<br />
<code><br />
// An example of using structs: reading data from a list of student <br />
// names and grades, saved as a .csv file, into an array. In this <br />
// example the file is saved as .csv, but the delimiter between <br />
// name and grade is the tab character, '\t'. Also, the first row <br />
// of the .csv file has column names -- "name" and "grade" -- so<br />
// I want to skip it.<br />
//-------------------------------------------------------------------<br />
</code><code><br />
#include &lt;iostream&gt;<br />
#include &lt;fstream&gt;<br />
#include &lt;string&gt;<br />
using namespace std;<br />
</code><code><br />
const int LISTMAX=1000;    // maximum number of students<br />
</code><code><br />
struct Student {<br />
  string name;<br />
  float grade;<br />
};<br />
</code><code><br />
void readList(string infilename);<br />
// Arguments:<br />
//   file name, passed as a class string.<br />
// Side effects:<br />
//   open file stream, function call of fillUp.<br />
</code><code><br />
void fillUp(ifstream&amp; inList, Student wholeClass[],<br />
int LISTMAX, int&amp; stCount);<br />
// Preconditions: global constant LISTMAX is the dimension of<br />
//   the wholeClass[] array of Student structs.<br />
// Postconditions:<br />
//   (1) grades[0] through grades[stCount-1]  filled with positive<br />
//   real numbers read from input file stream open by readList.<br />
//   (2) names[0] through names[stCount-1] are class string objects.<br />
//-------------------------------------------------------------------<br />
</code><code><br />
int main()<br />
{<br />
  string infilename="example_list.csv";<br />
  readList(infilename);<br />
  return 0;<br />
}<br />
//-------------------------------------------------------------------<br />
</code><code><br />
// open input, file stream call fillStudent to fill array named<br />
// wholeClass[LISTMAX] with Student structs<br />
void readList(string infilename)<br />
{<br />
  ifstream inList(infilename.c_str( ));<br />
  if (inList.fail()) {<br />
    cout &lt;&lt; "Input file opening failed.\n";<br />
    exit(1);<br />
  }<br />
  else {<br />
    Student wholeClass[LISTMAX]; // initialize array of structs<br />
    int stCount=0;  // number of valid elements starts at zero<br />
    fillUp(inList, wholeClass, LISTMAX, stCount);<br />
    cout &lt;&lt; "\nGrade Roll Report" &lt;&lt; endl;<br />
    cout &lt;&lt; "There were " &lt;&lt; stCount &lt;&lt; " students." &lt;&lt; endl;<br />
  }<br />
  inList.close();<br />
}<br />
</code><code><br />
// fill wholeClass[LISTMAX] using data from infilename.<br />
void fillUp(ifstream&amp; inList, Student wholeClass[],<br />
int LISTMAX, int&amp; stCount)<br />
{<br />
  string dummy;               // container for garbage.<br />
  getline(inList,dummy,'\n'); // toss the entire first row.<br />
  for(int i=0; i&lt;LISTMAX; i++) {<br />
    while (stCount&lt;LISTMAX &amp;&amp;<br />
           getline(inList, wholeClass[i].name,'\t')) {<br />
      if(inList &gt;&gt; wholeClass[i].grade) {<br />
        getline(inList,dummy,'\n');<br />
        cout &lt;&lt; "Name: " &lt;&lt; wholeClass[i].name<br />
             &lt;&lt; " Grade: "&lt;&lt; wholeClass[i].grade &lt;&lt; endl;<br />
      }<br />
      stCount++;<br />
    }<br />
  }<br />
}<br />
//-------------------------------------------------------------------<br />
</code><br />
So I'm back in school, continuing the course I started last fall. We're using the same textbook, and the quotes at the beginning of the sections are only getting better. Here's the one on testing and debugging your functions: "I beheld the wretch -- the miserable monster whom I had created." Can you tell it's from <em>Frankenstein</em>?</p>
]]></content:encoded>
			<wfw:commentRss>http://enoriver.net/index.php/2009/02/07/the-class-string-vs-c-style-strings/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Structs</title>
		<link>http://enoriver.net/index.php/2008/12/14/structs/</link>
		<comments>http://enoriver.net/index.php/2008/12/14/structs/#comments</comments>
		<pubDate>Sun, 14 Dec 2008 18:59:00 +0000</pubDate>
		<dc:creator>Gabi Huiber</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[structs]]></category>

		<guid isPermaLink="false">http://enoriver.net/?p=397</guid>
		<description><![CDATA[School's out, and structs were the last thing I learned about in this C++ class I took at NC State. This semester was really about the C in C++. The object-oriented part comes in the spring. But by the time you get around to structs, you will have learned about file streaming, arrays, global constants, [...]]]></description>
			<content:encoded><![CDATA[<p>School's out, and <code>structs</code> were the last thing I learned about in this C++ class I took at NC State. This semester was really about the C in C++. The object-oriented part comes in the spring. But by the time you get around to structs, you will have learned about file streaming, arrays, global constants, loops, all sorts of things. So, here's an example of reading mixed data -- strings of characters (like student names) and numbers (like student grades) -- from a text file:<br />
<code><br />
// An example of using structs: reading data from a list of student<br />
// names and grades, saved as a .csv file, into an array. In this<br />
// example the file is saved as .csv, but the delimiter between<br />
// name and grade is the tab character, '\t'. Also, the first row<br />
// of the .csv file has variable names -- "name" and "grade" --<br />
// so it must be skipped.<br />
//-------------------------------------------------------------------<br />
</code><code><br />
#include &lt;iostream&gt;<br />
#include &lt;fstream&gt;<br />
using namespace std;<br />
</code><code><br />
const int NAMELEN=120;     // longest name<br />
const int LISTMAX=1000;    // maximum number of students<br />
</code><code><br />
struct Student {<br />
  char name[NAMELEN];<br />
  float grade;<br />
};<br />
</code><code><br />
void readList(char infilename[]);<br />
// Arguments:<br />
//    file name, passed as an array of characters.<br />
// Side effects:<br />
//    open file stream, function call of fillUp.<br />
</code><code><br />
void fillUp(ifstream&amp; inList, Student wholeClass[],<br />
                        int LISTMAX, int&amp; stCount);<br />
// Preconditions: global constants LISTMAX and NAMELEN are the<br />
//   dimensions of the wholeClass[] and name[] arrays respectively.<br />
// Postconditions:<br />
//   (1) grades[0] through grades[stCount-1]  filled with positive<br />
//       real numbers read from input file stream open by readList.<br />
//   (2) names[0] through names[stCount-1] filled with<br />
//       array-based lists of characters up to NAMELEN long.<br />
//-------------------------------------------------------------------<br />
</code><code><br />
int main()<br />
{<br />
  char infilename[]="example_list.csv";<br />
  readList(infilename);<br />
  return 0;<br />
}<br />
//-------------------------------------------------------------------<br />
</code><code><br />
// open input, file stream call fillStudent to fill array named<br />
// wholeClass[LISTMAX] with Student structs<br />
void readList(char infilename[])<br />
{<br />
  ifstream inList(infilename);   // input file stream<br />
  if (inList.fail()) {<br />
    cout &lt;&lt; "Input file opening failed.\n";<br />
    exit(1);<br />
  }<br />
  else {<br />
    Student wholeClass[LISTMAX]; // initialize array of structs<br />
    int stCount=0;  // number of valid elements starts at zero<br />
    fillUp(inList, wholeClass, LISTMAX, stCount);<br />
    cout &lt;&lt; "\nGrade Roll Report" &lt;&lt; endl;<br />
    cout &lt;&lt; "There were " &lt;&lt; stCount &lt;&lt; " students." &lt;&lt; endl;<br />
  }<br />
  inList.close();<br />
}<br />
</code><code><br />
// fill wholeClass[LISTMAX] using data from INFILENAME.<br />
void fillUp(ifstream&amp; inList, Student wholeClass[],<br />
                        int LISTMAX, int&amp; stCount)<br />
{<br />
  char dummy[NAMELEN];                // container for garbage.<br />
  inList.getline(dummy,NAMELEN,'\n'); // toss the entire first row.<br />
  for(int i=0; i&lt;LISTMAX; i++) {<br />
    while (stCount&lt;LISTMAX &amp;&amp;<br />
           inList.getline(wholeClass[i].name,NAMELEN,'\t')) {<br />
      if(inList &gt;&gt; wholeClass[i].grade) {<br />
        inList.getline(dummy,NAMELEN,'\n');<br />
        cout &lt;&lt; "Name: " &lt;&lt; wholeClass[i].name<br />
             &lt;&lt; " Grade: "&lt;&lt; wholeClass[i].grade &lt;&lt; endl;<br />
      }<br />
      stCount++;<br />
    }<br />
  }<br />
}<br />
//-------------------------------------------------------------------<br />
</code></p>
<p>OK, so maybe the code above is a little over-commented for your taste, but they were very clear about how they wanted your programs in this course. There were points taken off for not commenting variables when you declared them, and the punishment for not commenting function prototypes was just a little short of a public flagellation.</p>
<p>I like this way of doing business. I learned my programming in the haphazard way that all social scientists do -- you write code in your statistical programming environment of choice, see to it that it does the job, and that's that. This, of course, bit me back numerous times over the years, so I did eventually come to appreciate the true cost of sloppiness. In computer science you don't get a chance at that sort of revelation, because bad practice is nipped in the bud. The textbook quoted Oscar Wilde before the first chapter was over: "In matters of grave importance,  style, not sincerity, is the vital thing."</p>
]]></content:encoded>
			<wfw:commentRss>http://enoriver.net/index.php/2008/12/14/structs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Debugging C++ code (UNIX)</title>
		<link>http://enoriver.net/index.php/2008/10/03/debugging-c-code-unix/</link>
		<comments>http://enoriver.net/index.php/2008/10/03/debugging-c-code-unix/#comments</comments>
		<pubDate>Sat, 04 Oct 2008 03:16:15 +0000</pubDate>
		<dc:creator>Gabi Huiber</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[GNU debugger]]></category>

		<guid isPermaLink="false">http://enoriver.net/?p=189</guid>
		<description><![CDATA[This assumes you're inside a UNIX terminal window (e.g., PuTTY session to remote UNIX box). Compile a .cpp source file in the GNU debugger: g++ -g -Wall -o mysourcefile.out mysourcefile.cpp Before starting gdb, make sure you're in the same directory as the executable just compiled above. Then call gdb: add GNU Then start the gdb [...]]]></description>
			<content:encoded><![CDATA[<p>This assumes you're inside a UNIX terminal window (e.g., PuTTY session to remote UNIX box).</p>
<p>Compile a .cpp source file in the GNU debugger:</p>
<p><code>g++ -g -Wall -o mysourcefile.out mysourcefile.cpp</code></p>
<p>Before starting gdb, make sure you're in the same directory as the executable just compiled above. Then call gdb:</p>
<p><code>add GNU</code></p>
<p>Then start the gdb debugger:</p>
<p><code>gdb mysourcefile.out</code></p>
<p>Things you can do here:</p>
<p>1) Set a breakpoint at a given function (e.g., main):</p>
<p><code>break main</code></p>
<p>2) Set a breakpoint at a given line (e.g., line 12):</p>
<p><code>break mysourcefile.cpp:12</code></p>
<p>3) Set a watch point at a given expression:</p>
<p><code>watch x&gt;y</code></p>
<p>4) Find the type of that expression:</p>
<p><code>whatis x&gt;y</code></p>
<p>5) OK, that was trivial, clearly it's boolean. More usefully, you can find its value so far (true or false in the GNU C++ compiler, 1 or 0 in others, I guess) with</p>
<p><code>print x&gt;y</code></p>
<p>6) Step into the program (execute a certain number of lines after the debugger stopped at your breakpoint):</p>
<p><code>step &lt;number of lines&gt;</code></p>
<p>If you set a break point, <code>run</code> will execute the code up to then, at which point you can choose <code>next</code> to move on to the next line, or <code>continue</code>, for the thing to run until it exits normally or an error is found.</p>
]]></content:encoded>
			<wfw:commentRss>http://enoriver.net/index.php/2008/10/03/debugging-c-code-unix/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

