<?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/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://enoriver.net</link>
	<description>computing for fun and profit</description>
	<lastBuildDate>Tue, 31 Jan 2012 20:03:36 +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>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>

