12.7.11

Learning C++ Part 1

Well ladies and gentlemen, let us begin our arduous and lengthy trek into the unfamiliar world of coding C++.  As Lao Tzu put it, "A journey of a thousand miles must begin with a single step."

While browsing the countless "How To's", I came across a book with a catchy cover and a title that was more or less what I was looking for- "A Beginner's Guide: C++ Without Fear" by Brian Overland.  In the book, Brian takes the approach of assuming the reader has never had any coding experience (me), that the concept of coding and programming is overwhelming (me), and that while you have fear of the unknown, you at least have the desire to press on and do what it takes to conquer C++.

I could tell from page 1 that I was going to enjoy this book.  It wasn't a raw, detached, unemotional barrage of terms and concepts.  Instead, it was as if Brian was there with you, rooting for you and doing his very best to break extremely difficult concepts down into tiny bite-sized pieces of digestible information.

Within minutes, I not only had a fair understanding of how programmers think, I was on my way to solving a real life problem.  The first example was how to convert a temperature from Celsius to Fahrenheit via C++ code.  Through this example, I learned several key concepts that quickly stuck with me.  I'll cover several key concepts as I go.

*Note: In order to quickly and coherently write the code of my choice, I will designate all code to be written in Green and all comments regarding the code in Red, with 2 slashes at the beginning.

Cout << "Please enter the Celsius Temperature \n" ;

//This code simply brings up a text box for the User, which reads "Please enter the Celsius Temperature".  After this, the "\n" tells the program to skip a line before continuing on. 

Next I learned about "If / Else" statements.  Quite simply, the code for this is as follows:

if (a > b)
     cout << "a is greater than b. \n" ;
else
     cout << "a is not greater than b. \n" ;

//This code is pretty straightforward.  If the value for "a" ends up being greater than b, the User will see "a is greater than b."  Note the use of semicolons to close all lines.


Next, I was taught about what is unique to C++, how to build programs with the use of Compile/Link, and where to find useful (and free) shareware that I should use to practice the concepts taught in the book.  Once this was installed and running, I was taught how to use the top piece of code (the Celsius prompt) in an actual code environment.  Again, Brian took his time to really explain what is going on.  He helped subdue the natural instinct I was feeling, which was "What is going on?!"  At any rate, after a couple test problems, I was almost halfway through the first Chapter.

Along the way, I learned several more useful pieces of information.  Using the "cin" command to store numbers to variables, running equations, and learning the difference between "int", "double", and "long double" all came together to form my first actual program:


#include <iostream>
using namespace std;


int main() {
        double ctemp, ftemp;
    
        cout << "Please input the Celsius temp and press Enter: ";
        cin >> ctemp;
        ftemp = (ctemp * 1.8) + 32;
        cout << "Fahrenheit temp is : " << ftemp;
        cout << " \n" ;
        
                
  system("PAUSE");
  return 0;
}

//As you can probably deduce for yourself, this program starts by asking you to input the Celsius that you desire to have converted to Fahrenheit.  Once you input a number and hit <enter>, it then runs a calculation where it multiplies the input number by 1.8, then adds 32.  At this point, the User will see "Fahrenheit temp is:" and then the result.  So, if the number ends up being 60, the User will see "Fahrenheit temp is: 60."




While this took well over an hour to learn the required knowledge and create the above program, I felt an immense sense of satisfaction watching the code compile with no errors, then seamlessly run as designed.  This triggered a sort of high which would keep me craving more as I continued on through the remainder of chapter 1.
I felt like this.

As I closed the final pages of chapter 1, I couldn't help but feel a sort of thrill run through me.  I was off to a good start.  Who knew?... maybe I would go on to invent the next Harvard Connection  The Facebook  Facebook.


0 comments:

Post a Comment