14.7.11

Learning C++ Part 2

With head held high, I continued on to Chapter 2.  While reading the first paragraph, I came across something that boosted my morale even higher!- I was told that by the end of the chapter, I would have all the tools needed to make a basic game!  Now that's pretty cool.

Initially, Brian starts out easy by describing the differences between int and double.  He then brings up a hugely important topic- if and if-else.  The point Brian drove home was the structure, which I'll show below.

if (condition)
    statement 1
else
    statement 2


Armed with this knowledge, I set out to work on my first exercise.  The goal was to be able to type in a number (once prompted, of course), and then have the program tell me if it was odd or even. Here's what I did.


#include <iostream>
#include <stdlib.h>


using namespace std;


int main(int argc, char *argv[])
{
 int n, remainder;
 // Get Number from Keyboard
 cout << "Please enter a number and hit ENTER \n";
 cin >> n ;

 //Get remainder after dividing by 2

 remainder = n % 2 ;

 //If remainder is 0, input was even
 // If not, it was odd

 if (remainder == 0)
  cout << "Number is even!  Congrats, face. \n" ;
 else 
  cout << "Oh man... number is odd, homie. Lame. \n" ;
  
    
  system("PAUSE");
  return 0;
}


Booyah.  Ego slightly boosted.  I begin to think "Man, this coding stuff is cake!"  But then I look down and realize I'm on page 40 of 550... so I decide to shut up and push on.

The next big topic covered involved loops brought about via the while command.  Again, the structure was the important point.

while (condition) {
    statement
}

And here was my first example using that command.


#include <iostream>
#include <stdlib.h>


using namespace std;


int main()
{
int i , n ;
cin >> n;
i = 1;


while (i <= n) {
    cout << i << " " ;
    i = 1 + i;
  }
    
  system("PAUSE");
  return 0;
}


In the above code, if you typed in the number 10 when prompted, the program would show you "1 2 3 4 5 6 7 8 9 10".


Brian then goes on to show a few techniques to optimize the code, and I do a few more exercise problems.  After these minor points, it's time for the next major command: var++ .  This code passes the integer along to the rest of the program, then adds 1 to its value.

At this point, we're closing in on the end of the chapter.  The last true learning point is Boolean (Short-Circuit) Logic.  These include && (AND) , || (OR) , and ! (NOT).

After this, there was one more giant program (which gave me fits) where you can input any number, and it will test to see if it is prime or not.  While it did use all of my learned knowledge up to that point, it was extremely frustrating to get working due to an error while using sqrt(var) with int instead of double.  Took hours to fix.  Not wanting to revisit that, so let's push on.

Finally, I reached the last exercise: Creating a Game!  I was excited (and a bit overwhelmed).  But with the help of the text, I was easily guided through it in a way that actually taught me the why instead of just expecting me to simply memorize it.  The final code, and all its glory, are below.


#include <iostream>
#include <cmath>


using namespace std;


int main()

  int n2;
  int i2;
    
  cout << "Let's play a game! \n" << " \n" << "Here's how to play. \n";
  cout << "The object is to be the person to get to 0 or less, while only \n";
  cout << "subtracting by 1 or 2 from a starting number, which YOU will pick! \n\n";
  cout << "Go ahead and pick the starting number \n" ;
  cin >> n2;
  
  while (true) {
  
  if (n2 % 3 == 2) {
    n2 = n2 - 2 ; cout << "\n I subtracted 2. \n "; 
    }
  else {
    n2 = n2 - 1;
    cout << "\n I subtracted 1. \n" ; 
    }
  cout << "\n New total is " << n2 << endl;
  if (n2 == 0) {
    cout << "\n\n I win, sucka! \n"; 
    break; 
    }
    
  cout << "Your move, punk.  Enter either 1 or 2. \n" ;
  cin >> i2;  
  while (i2 < 1 || i2 > 2) {
    cout << "Input must be 1 or 2, my friend who is about to lose. \n ";  cout << "Please Re-enter: \n\n" ; 
    cin >> i2;
    }
  
  n2 = n2 - i2;
  cout << "\n New total is " << n2 << endl;
  if (n2 == 0 ) {
    cout << "You win! \n" ;
    break;
    }
   }   
  system("PAUSE");
  return 0;
}





Me, after writing & troubleshooting my "game". 
After successfully completing this assignment (which ended Chapter 2), I couldn't help but feel like I was Neo from the Matrix.  Not only was it satisfying to really understand what I was doing, but I was enjoying myself at the same time.

3 comments:

Okay this was absolutely adorable. :3 I love watching people learn how to code. Reminds me of my first year university when we learned Java, hehe.

Thanks... not that "adorable" was my goal, but I realize it's probably like watching a todler try to walk. I'll get there, though.

Haha, yeah it kind of is. But, it's awesome that you seem to be really enjoying it, rather than cursing up and down and saying that coding stupid, which is the most common reaction. :P

Post a Comment