<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2enclosuresfull.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:media="http://search.yahoo.com/mrss/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0"><channel><title>Coding Mix</title><link>http://www.codingmix.com/search/label/Cplusplus</link><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/codingmix/Cplusplus" /><description></description><language>en</language><managingEditor>noreply@blogger.com (Csabi)</managingEditor><lastBuildDate>Thu, 23 Feb 2012 19:15:09 PST</lastBuildDate><generator>Blogger http://www.blogger.com</generator><openSearch:totalResults xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/">16</openSearch:totalResults><openSearch:startIndex xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/">1</openSearch:startIndex><openSearch:itemsPerPage xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/">25</openSearch:itemsPerPage><feedburner:info uri="codingmix/cplusplus" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:emailServiceId>codingmix/Cplusplus</feedburner:emailServiceId><feedburner:feedburnerHostname>http://feedburner.google.com</feedburner:feedburnerHostname><item><title>My first 2D game in C++ using Direct3D</title><link>http://feedproxy.google.com/~r/codingmix/Cplusplus/~3/nzddpsqhEUU/2d-game-directx-snake-cplusplus.html</link><category>Cplusplus projects</category><category>Cplusplus</category><author>noreply@blogger.com (Csabi)</author><pubDate>Fri, 10 Dec 2010 12:54:38 PST</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-5682776130825686229.post-5494779758440019457</guid><description>This is my first 2D game. It`s written in C++ using DirectX9 (Direct3D and DirectInput). This is a very simple Snake game. At the beginning the snake is only 1 square long and when it "eats" the target it`s length is encreased by 1 square. The snake can have a maximum of 20 squares long (each square is 20px), after it reaches this maximum length the snake is moving faster and faster. If the head of the snake hit`s it`s  tail the game is over:&lt;div id="full-post"&gt;&lt;object width="540" height="328"&gt;&lt;param name="movie" value="http://www.youtube.com/v/K8_UpmEfEnU?fs=1&amp;amp;hl=en_US&amp;amp;rel=0&amp;amp;color1=0x234900&amp;amp;color2=0x4e9e00"&gt;&lt;/param&gt;&lt;param name="allowFullScreen" value="true"&gt;&lt;/param&gt;&lt;param name="allowscriptaccess" value="always"&gt;&lt;/param&gt;&lt;embed src="http://www.youtube.com/v/K8_UpmEfEnU?fs=1&amp;amp;hl=en_US&amp;amp;rel=0&amp;amp;color1=0x234900&amp;amp;color2=0x4e9e00" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="540" height="328"&gt;&lt;/embed&gt;&lt;/object&gt;And here is the source-code:&lt;div class="code"&gt;#include &amp;lt;Windows.h&amp;gt;&lt;br /&gt;#include &amp;lt;WindowsX.h&amp;gt;&lt;br /&gt;#include &amp;lt;d3d9.h&amp;gt;&lt;br /&gt;#include &amp;lt;d3dx9.h&amp;gt;&lt;br /&gt;#include &amp;lt;dinput.h&amp;gt;&lt;br /&gt;#include &amp;lt;ctime&amp;gt;&lt;br /&gt;&lt;br /&gt;#pragma comment(lib,&amp;quot;d3d9.lib&amp;quot;);&lt;br /&gt;#pragma comment(lib,&amp;quot;d3dx9.lib&amp;quot;);&lt;br /&gt;#pragma comment(lib,&amp;quot;dinput8.lib&amp;quot;);&lt;br /&gt;#pragma comment(lib,&amp;quot;dxguid.lib&amp;quot;);&lt;br /&gt;&lt;br /&gt;LPDIRECT3D9 d3d;&lt;br /&gt;LPDIRECT3DDEVICE9 d3ddev;&lt;br /&gt;LPDIRECT3DVERTEXBUFFER9 buffer=NULL;&lt;br /&gt;LPDIRECTINPUT8 dinput;&lt;br /&gt;LPDIRECTINPUTDEVICE8 keyboard;&lt;br /&gt;BYTE buttons[256];&lt;br /&gt;ID3DXFont *m_font;&lt;br /&gt;&lt;br /&gt;LRESULT CALLBACK WindowProc(HWND hWnd,&lt;br /&gt;                                                             UINT message,&lt;br /&gt;                                                             WPARAM wParam,&lt;br /&gt;                                                             LPARAM lParam);&lt;br /&gt;void initialized3d(HWND hWnd);&lt;br /&gt;void renderframe(void);&lt;br /&gt;void cleand3d(void);&lt;br /&gt;void graphics(void);&lt;br /&gt;void initializedinput(HINSTANCE hInstance,&lt;br /&gt;                             HWND hWnd);&lt;br /&gt;void getkey(void);&lt;br /&gt;void cleandinput(void);&lt;br /&gt;&lt;br /&gt;struct VERTEX {&lt;br /&gt;   FLOAT X, Y, Z, RHW; &lt;br /&gt;   DWORD COLOR;&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;#define FVF (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)&lt;br /&gt;&lt;br /&gt;int WINAPI WinMain(HINSTANCE hInstance, &lt;br /&gt;                                   HINSTANCE hPrevInstance, &lt;br /&gt;                                   LPSTR lpCmdLine,&lt;br /&gt;                                   int nCmdShow){&lt;br /&gt;   WNDCLASSEX windowclass;&lt;br /&gt;   ZeroMemory(&amp;amp;windowclass,sizeof(WNDCLASSEX));&lt;br /&gt;&lt;br /&gt;   windowclass.cbSize = sizeof(WNDCLASSEX);&lt;br /&gt;   windowclass.style = CS_HREDRAW | CS_VREDRAW;&lt;br /&gt;   windowclass.lpfnWndProc = WindowProc;&lt;br /&gt;   windowclass.hInstance = hInstance;&lt;br /&gt;   windowclass.hCursor = LoadCursor(NULL,IDC_ARROW);&lt;br /&gt;   windowclass.hbrBackground = (HBRUSH)COLOR_WINDOW;&lt;br /&gt;   windowclass.lpszClassName = L&amp;quot;WindowClass&amp;quot;;&lt;br /&gt;&lt;br /&gt;   RegisterClassEx(&amp;amp;windowclass);&lt;br /&gt;   HWND hWnd = CreateWindowEx(NULL,&lt;br /&gt;                                                          L&amp;quot;WindowClass&amp;quot;,&lt;br /&gt;                                                          L&amp;quot;Snake - www.codingmix.com&amp;quot;,&lt;br /&gt;                                                          WS_OVERLAPPEDWINDOW,&lt;br /&gt;                                                          600,&lt;br /&gt;                                                          200,&lt;br /&gt;                                                          636,&lt;br /&gt;                                                          478,&lt;br /&gt;                                                          NULL,&lt;br /&gt;                                                          NULL,&lt;br /&gt;                                                          hInstance,&lt;br /&gt;                                                          NULL);&lt;br /&gt;   ShowWindow(hWnd,&lt;br /&gt;                          nCmdShow);&lt;br /&gt;   initialized3d(hWnd);&lt;br /&gt;   initializedinput(hInstance,&lt;br /&gt;                         hWnd);&lt;br /&gt;   MSG message;&lt;br /&gt;   while(TRUE){&lt;br /&gt;      while(PeekMessage(&amp;amp;message,&lt;br /&gt;                                      NULL,&lt;br /&gt;                                      0,&lt;br /&gt;                                      0,&lt;br /&gt;                                      PM_REMOVE)){&lt;br /&gt;         TranslateMessage(&amp;amp;message);&lt;br /&gt;         DispatchMessage(&amp;amp;message);&lt;br /&gt;      }&lt;br /&gt;      if(message.message == WM_QUIT){&lt;br /&gt;         break;&lt;br /&gt;      }&lt;br /&gt;      getkey();&lt;br /&gt;      graphics();&lt;br /&gt;      renderframe();&lt;br /&gt;   }  &lt;br /&gt;   cleand3d();&lt;br /&gt;   cleandinput();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void initialized3d(HWND hWnd){&lt;br /&gt;   d3d = Direct3DCreate9(D3D_SDK_VERSION);&lt;br /&gt;   D3DPRESENT_PARAMETERS d3dpp;&lt;br /&gt;   ZeroMemory(&amp;amp;d3dpp,sizeof(d3dpp));&lt;br /&gt;&lt;br /&gt;   d3dpp.Windowed = TRUE;&lt;br /&gt;   d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;&lt;br /&gt;   d3dpp.hDeviceWindow = hWnd;&lt;br /&gt;&lt;br /&gt;   d3d-&amp;gt;CreateDevice(D3DADAPTER_DEFAULT,&lt;br /&gt;                                   D3DDEVTYPE_HAL,&lt;br /&gt;                                   hWnd,&lt;br /&gt;                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING,&lt;br /&gt;                                   &amp;amp;d3dpp,&lt;br /&gt;                                   &amp;amp;d3ddev);&lt;br /&gt;   D3DXCreateFont(d3ddev, &lt;br /&gt;                                50, &lt;br /&gt;                                0, &lt;br /&gt;                                FW_BOLD, &lt;br /&gt;                                0, &lt;br /&gt;                                FALSE, &lt;br /&gt;                                DEFAULT_CHARSET, &lt;br /&gt;                                OUT_DEFAULT_PRECIS, &lt;br /&gt;                                DEFAULT_QUALITY, &lt;br /&gt;                                DEFAULT_PITCH | FF_DONTCARE, &lt;br /&gt;                                TEXT(&amp;quot;Arial&amp;quot;), &lt;br /&gt;                                &amp;amp;m_font );&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void initializedinput(HINSTANCE hInstance,&lt;br /&gt;                              HWND hWnd){&lt;br /&gt;   DirectInput8Create(hInstance,&lt;br /&gt;                                 DIRECTINPUT_VERSION,&lt;br /&gt;                                 IID_IDirectInput8,&lt;br /&gt;                                 (void**)&amp;amp;dinput,&lt;br /&gt;                                 NULL);&lt;br /&gt;   dinput-&amp;gt;CreateDevice(GUID_SysKeyboard,&lt;br /&gt;                                      &amp;amp;keyboard,&lt;br /&gt;                                      NULL);&lt;br /&gt;   keyboard-&amp;gt;SetDataFormat(&amp;amp;c_dfDIKeyboard);&lt;br /&gt;   keyboard-&amp;gt;SetCooperativeLevel(hWnd,&lt;br /&gt;                                                      DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int verticesnr = 132;&lt;br /&gt;int trianglesnr = 44;&lt;br /&gt;bool left = false;&lt;br /&gt;bool right = false;&lt;br /&gt;bool up = true;&lt;br /&gt;bool down = false;&lt;br /&gt;float posy = 220.0f;&lt;br /&gt;float posx = 280.0f;&lt;br /&gt;bool target = false;&lt;br /&gt;float random1;&lt;br /&gt;float random2;&lt;br /&gt;float newpos[][2] = {&lt;br /&gt;   {-20.0f, -20.0f},&lt;br /&gt;   {-20.0f, -20.0f},&lt;br /&gt;   {-20.0f, -20.0f},&lt;br /&gt;   {-20.0f, -20.0f},&lt;br /&gt;   {-20.0f, -20.0f},&lt;br /&gt;   {-20.0f, -20.0f},&lt;br /&gt;   {-20.0f, -20.0f},&lt;br /&gt;   {-20.0f, -20.0f},&lt;br /&gt;   {-20.0f, -20.0f},&lt;br /&gt;   {-20.0f, -20.0f},&lt;br /&gt;   {-20.0f, -20.0f},&lt;br /&gt;   {-20.0f, -20.0f},&lt;br /&gt;   {-20.0f, -20.0f},&lt;br /&gt;   {-20.0f, -20.0f},&lt;br /&gt;   {-20.0f, -20.0f},&lt;br /&gt;   {-20.0f, -20.0f},&lt;br /&gt;   {-20.0f, -20.0f},&lt;br /&gt;   {-20.0f, -20.0f},&lt;br /&gt;   {-20.0f, -20.0f},&lt;br /&gt;   {-20.0f, -20.0f},&lt;br /&gt;};&lt;br /&gt;int squares = 0;&lt;br /&gt;bool gameover = false;&lt;br /&gt;int col1 = 0;&lt;br /&gt;int col2 = 255;&lt;br /&gt;int currentframe = 0;&lt;br /&gt;int speed = 30;&lt;br /&gt;void graphics(void){&lt;br /&gt;   static int framenr = 0;&lt;br /&gt;   framenr++;&lt;br /&gt;   if(framenr % speed == 0 &amp;amp;&amp;amp; !gameover){&lt;br /&gt;      if(squares &amp;gt; 0){&lt;br /&gt;         for(int i=squares-1;i&amp;gt;0;i--){&lt;br /&gt;            newpos[i][0] = newpos[i-1][0];&lt;br /&gt;            newpos[i][1] = newpos[i-1][1];&lt;br /&gt;         } &lt;br /&gt;         newpos[0][0] = posx;&lt;br /&gt;         newpos[0][1] = posy;&lt;br /&gt;      }&lt;br /&gt;      if(up){&lt;br /&gt;         posy -= 20.0f;&lt;br /&gt;      }&lt;br /&gt;      else if(down){&lt;br /&gt;         posy += 20.0f;&lt;br /&gt;      }&lt;br /&gt;      else if(left){&lt;br /&gt;         posx -= 20.0f;&lt;br /&gt;      }&lt;br /&gt;      else{&lt;br /&gt;         posx += 20.0f;&lt;br /&gt;      }&lt;br /&gt;      for(int i=0;i&amp;lt;=squares;i++){&lt;br /&gt;         for(int y=0;y&amp;lt;=squares;y++){&lt;br /&gt;            if(newpos[i][0] == newpos[y][0] &amp;amp;&amp;amp; newpos[i][1] == newpos[y][1] &amp;amp;&amp;amp; i != y){&lt;br /&gt;               gameover = true;&lt;br /&gt;            }&lt;br /&gt;         }&lt;br /&gt;         if(posx == newpos[i][0] &amp;amp;&amp;amp; posy == newpos[i][1]){&lt;br /&gt;            gameover = true;&lt;br /&gt;         }&lt;br /&gt;      }&lt;br /&gt;      if(posx == random1 &amp;amp;&amp;amp; posy == random2){&lt;br /&gt;         target = false;&lt;br /&gt;         if(squares &amp;gt; 19 &amp;amp;&amp;amp; squares &amp;gt; 0){&lt;br /&gt;            speed--;&lt;br /&gt;         }&lt;br /&gt;         else{&lt;br /&gt;            if(squares == 0){&lt;br /&gt;               newpos[0][0] = posx;&lt;br /&gt;               newpos[0][1] = posy;&lt;br /&gt;            }&lt;br /&gt;            else{&lt;br /&gt;               newpos[squares][0] = newpos[squares-1][0];&lt;br /&gt;               newpos[squares][1] = newpos[squares-1][1];&lt;br /&gt;            }&lt;br /&gt;            squares++;&lt;br /&gt;         }&lt;br /&gt;      }&lt;br /&gt;      for(int i=0;i&amp;lt;squares;i++){&lt;br /&gt;         if(newpos[i][0] &amp;gt; 600){&lt;br /&gt;            newpos[i][0]-=620;&lt;br /&gt;         }&lt;br /&gt;         else if(newpos[i][0] &amp;lt; 0){&lt;br /&gt;            newpos[i][0]+=620;&lt;br /&gt;         }&lt;br /&gt;         else if(newpos[i][1] &amp;gt; 420){&lt;br /&gt;            newpos[i][1]-=440;&lt;br /&gt;         }&lt;br /&gt;         else if(newpos[i][1] &amp;lt; 0){&lt;br /&gt;            newpos[i][1]+=440;&lt;br /&gt;         }&lt;br /&gt;      }&lt;br /&gt;      if(posx &amp;gt; 600){&lt;br /&gt;         posx-=620;&lt;br /&gt;      }&lt;br /&gt;      else if(posx &amp;lt; 0){&lt;br /&gt;         posx+=620;&lt;br /&gt;      }&lt;br /&gt;      else if(posy &amp;gt; 420){&lt;br /&gt;         posy-=440;&lt;br /&gt;      }&lt;br /&gt;      else if(posy &amp;lt; 0){&lt;br /&gt;         posy+=440;&lt;br /&gt;      }&lt;br /&gt;      currentframe = 0;&lt;br /&gt;   }&lt;br /&gt;   else if(framenr % 20 == 0 &amp;amp;&amp;amp; gameover){&lt;br /&gt;      if(col1 == 255){&lt;br /&gt;         col1 = 0;&lt;br /&gt;         col2 = 255;&lt;br /&gt;      }&lt;br /&gt;      else{&lt;br /&gt;         col1 = 255;&lt;br /&gt;         col2 = 0;&lt;br /&gt;      }&lt;br /&gt;   }&lt;br /&gt;   if(buttons[DIK_LEFT] &amp;amp; 0x80 &amp;amp;&amp;amp; right == false &amp;amp;&amp;amp; currentframe == 0){&lt;br /&gt;      left = true;&lt;br /&gt;      up = false;&lt;br /&gt;      down = false;&lt;br /&gt;      currentframe = 1;&lt;br /&gt;   }&lt;br /&gt;   else if(buttons[DIK_RIGHT] &amp;amp; 0x80 &amp;amp;&amp;amp; left == false &amp;amp;&amp;amp; currentframe == 0){&lt;br /&gt;      right = true;&lt;br /&gt;      up = false;&lt;br /&gt;      down = false;&lt;br /&gt;      currentframe = 1;&lt;br /&gt;   }&lt;br /&gt;   else if(buttons[DIK_UP] &amp;amp; 0x80 &amp;amp;&amp;amp; down == false &amp;amp;&amp;amp; currentframe == 0){&lt;br /&gt;      up = true;&lt;br /&gt;      left = false;&lt;br /&gt;      right = false;&lt;br /&gt;      currentframe = 1;&lt;br /&gt;   }&lt;br /&gt;   else if(buttons[DIK_DOWN] &amp;amp; 0x80 &amp;amp;&amp;amp; up == false &amp;amp;&amp;amp; currentframe == 0){&lt;br /&gt;      down = true;&lt;br /&gt;      left = false;&lt;br /&gt;      right = false;&lt;br /&gt;      currentframe = 1;&lt;br /&gt;   }&lt;br /&gt;   if(!target){&lt;br /&gt;      srand((unsigned)time(0));&lt;br /&gt;      random1 = (rand() % 30) * 20.0f; &lt;br /&gt;      random2 = (rand() % 22) * 20.0f;&lt;br /&gt;      target = true;&lt;br /&gt;   }&lt;br /&gt;   VERTEX vertices[] = {&lt;br /&gt;      //Target&lt;br /&gt;      {random1 + 10.0f, random2, 1.0f, 1.0f, D3DCOLOR_XRGB(255, 0, 0),},&lt;br /&gt;      {random1 + 20.0f, random2 + 10.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(255, 0, 0),},&lt;br /&gt;      {random1, random2 + 10.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(255, 0, 0),},&lt;br /&gt;      {random1, random2 + 10.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(255, 0, 0),},&lt;br /&gt;      {random1 + 20.0f, random2 + 10.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(255, 0, 0),},&lt;br /&gt;      {random1 + 10.0f, random2 + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(255, 0, 0),},&lt;br /&gt;      //Head&lt;br /&gt;      {posx , posy, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {posx + 20.0f, posy, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {posx, posy + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {posx, posy + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {posx + 20.0f, posy, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {posx + 20.0f, posy + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      //Square 1&lt;br /&gt;      {newpos[0][0] , newpos[0][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[0][0] + 20.0f, newpos[0][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[0][0], newpos[0][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[0][0], newpos[0][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[0][0] + 20.0f, newpos[0][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[0][0] + 20.0f, newpos[0][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      //Square 2&lt;br /&gt;      {newpos[1][0] , newpos[1][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[1][0] + 20.0f, newpos[1][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[1][0], newpos[1][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[1][0], newpos[1][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[1][0] + 20.0f, newpos[1][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[1][0] + 20.0f, newpos[1][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      //Square 3&lt;br /&gt;      {newpos[2][0] , newpos[2][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[2][0] + 20.0f, newpos[2][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[2][0], newpos[2][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[2][0], newpos[2][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[2][0] + 20.0f, newpos[2][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[2][0] + 20.0f, newpos[2][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      //Square 4&lt;br /&gt;      {newpos[3][0] , newpos[3][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[3][0] + 20.0f, newpos[3][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[3][0], newpos[3][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[3][0], newpos[3][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[3][0] + 20.0f, newpos[3][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[3][0] + 20.0f, newpos[3][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      //Square 5&lt;br /&gt;      {newpos[4][0] , newpos[4][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[4][0] + 20.0f, newpos[4][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[4][0], newpos[4][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[4][0], newpos[4][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[4][0] + 20.0f, newpos[4][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[4][0] + 20.0f, newpos[4][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      //Square 6&lt;br /&gt;      {newpos[5][0] , newpos[5][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[5][0] + 20.0f, newpos[5][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[5][0], newpos[5][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[5][0], newpos[5][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[5][0] + 20.0f, newpos[5][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[5][0] + 20.0f, newpos[5][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      //Square 7&lt;br /&gt;      {newpos[6][0] , newpos[6][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[6][0] + 20.0f, newpos[6][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[6][0], newpos[6][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[6][0], newpos[6][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[6][0] + 20.0f, newpos[6][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[6][0] + 20.0f, newpos[6][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      //Square 8&lt;br /&gt;      {newpos[7][0] , newpos[7][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[7][0] + 20.0f, newpos[7][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[7][0], newpos[7][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[7][0], newpos[7][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[7][0] + 20.0f, newpos[7][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[7][0] + 20.0f, newpos[7][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      //Square 9&lt;br /&gt;      {newpos[8][0] , newpos[8][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[8][0] + 20.0f, newpos[8][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[8][0], newpos[8][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[8][0], newpos[8][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[8][0] + 20.0f, newpos[8][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[8][0] + 20.0f, newpos[8][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      //Square 10&lt;br /&gt;      {newpos[9][0] , newpos[9][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[9][0] + 20.0f, newpos[9][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[9][0], newpos[9][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[9][0], newpos[9][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[9][0] + 20.0f, newpos[9][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[9][0] + 20.0f, newpos[9][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      //Square 11&lt;br /&gt;      {newpos[10][0] , newpos[10][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[10][0] + 20.0f, newpos[10][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[10][0], newpos[10][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[10][0], newpos[10][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[10][0] + 20.0f, newpos[10][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[10][0] + 20.0f, newpos[10][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      //Square 12&lt;br /&gt;      {newpos[11][0] , newpos[11][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[11][0] + 20.0f, newpos[11][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[11][0], newpos[11][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[11][0], newpos[11][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[11][0] + 20.0f, newpos[11][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[11][0] + 20.0f, newpos[11][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      //Square 13&lt;br /&gt;      {newpos[12][0] , newpos[12][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[12][0] + 20.0f, newpos[12][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[12][0], newpos[12][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[12][0], newpos[12][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[12][0] + 20.0f, newpos[12][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[12][0] + 20.0f, newpos[12][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      //Square 14&lt;br /&gt;      {newpos[13][0] , newpos[13][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[13][0] + 20.0f, newpos[13][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[13][0], newpos[13][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[13][0], newpos[13][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[13][0] + 20.0f, newpos[13][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[13][0] + 20.0f, newpos[13][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      //Square 15&lt;br /&gt;      {newpos[14][0] , newpos[14][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[14][0] + 20.0f, newpos[14][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[14][0], newpos[14][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[14][0], newpos[14][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[14][0] + 20.0f, newpos[14][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[14][0] + 20.0f, newpos[14][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      //Square 16&lt;br /&gt;      {newpos[15][0] , newpos[15][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[15][0] + 20.0f, newpos[15][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[15][0], newpos[15][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[15][0], newpos[15][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[15][0] + 20.0f, newpos[15][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[15][0] + 20.0f, newpos[15][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      //Square 17&lt;br /&gt;      {newpos[16][0] , newpos[16][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[16][0] + 20.0f, newpos[16][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[16][0], newpos[16][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[16][0], newpos[16][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[16][0] + 20.0f, newpos[16][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[16][0] + 20.0f, newpos[16][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      //Square 18&lt;br /&gt;      {newpos[17][0] , newpos[17][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[17][0] + 20.0f, newpos[17][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[17][0], newpos[17][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[17][0], newpos[17][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[17][0] + 20.0f, newpos[17][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[17][0] + 20.0f, newpos[17][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      //Square 19&lt;br /&gt;      {newpos[18][0] , newpos[18][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[18][0] + 20.0f, newpos[18][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[18][0], newpos[18][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[18][0], newpos[18][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[18][0] + 20.0f, newpos[18][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[18][0] + 20.0f, newpos[18][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      //Square 20&lt;br /&gt;      {newpos[19][0] , newpos[19][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[19][0] + 20.0f, newpos[19][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[19][0], newpos[19][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[19][0], newpos[19][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[19][0] + 20.0f, newpos[19][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[19][0] + 20.0f, newpos[19][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;   };&lt;br /&gt;   d3ddev-&amp;gt;CreateVertexBuffer(verticesnr*sizeof(VERTEX),&lt;br /&gt;                                                 0,&lt;br /&gt;                                                 FVF,&lt;br /&gt;                                                 D3DPOOL_MANAGED,&lt;br /&gt;                                                 &amp;amp;buffer,&lt;br /&gt;                                                 NULL);&lt;br /&gt;   VOID* pVoid;&lt;br /&gt;   buffer-&amp;gt;Lock(0,&lt;br /&gt;                         0,&lt;br /&gt;                         (void**)&amp;amp;pVoid,&lt;br /&gt;                         0);&lt;br /&gt;   memcpy(pVoid,&lt;br /&gt;                 vertices,&lt;br /&gt;                 sizeof(vertices));&lt;br /&gt;   buffer-&amp;gt;Unlock();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void getkey(void){&lt;br /&gt;   keyboard-&amp;gt;Acquire();&lt;br /&gt;   keyboard-&amp;gt;GetDeviceState(256,&lt;br /&gt;                                              (LPVOID)buttons);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void renderframe(void){&lt;br /&gt;   d3ddev-&amp;gt;Clear(0,&lt;br /&gt;                            NULL,&lt;br /&gt;                            D3DCLEAR_TARGET,&lt;br /&gt;                            D3DCOLOR_XRGB(255,222,173),&lt;br /&gt;                            1.0f,&lt;br /&gt;                            0);&lt;br /&gt;   d3ddev-&amp;gt;BeginScene();&lt;br /&gt;&lt;br /&gt;   D3DCOLOR fontColor = D3DCOLOR_ARGB(255,255,0,0); &lt;br /&gt;   RECT rct;&lt;br /&gt;   rct.left=200;&lt;br /&gt;   rct.right=480;&lt;br /&gt;   rct.top=150;&lt;br /&gt;   rct.bottom=rct.top+50;&lt;br /&gt;   if(gameover){&lt;br /&gt;      m_font-&amp;gt;DrawText(NULL,&lt;br /&gt;                                     (LPCWSTR)L&amp;quot;Game over&amp;quot;,&lt;br /&gt;                                     -1,&lt;br /&gt;                                     &amp;amp;rct,&lt;br /&gt;                                     0,&lt;br /&gt;                                     fontColor);&lt;br /&gt;   }&lt;br /&gt;   d3ddev-&amp;gt;SetFVF(FVF);&lt;br /&gt;   d3ddev-&amp;gt;SetStreamSource(0,&lt;br /&gt;                                              buffer,&lt;br /&gt;                                              0,&lt;br /&gt;                                              sizeof(VERTEX));&lt;br /&gt;   d3ddev-&amp;gt;DrawPrimitive(D3DPT_TRIANGLELIST,&lt;br /&gt;                                         0,&lt;br /&gt;                                         trianglesnr);&lt;br /&gt;   d3ddev-&amp;gt;EndScene();&lt;br /&gt;   d3ddev-&amp;gt;Present(NULL,&lt;br /&gt;                               NULL,&lt;br /&gt;                               NULL,&lt;br /&gt;                               NULL);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void cleand3d(void){&lt;br /&gt;   buffer-&amp;gt;Release();&lt;br /&gt;   d3ddev-&amp;gt;Release();&lt;br /&gt;   d3d-&amp;gt;Release();&lt;br /&gt;   m_font-&amp;gt;Release();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void cleandinput(void){&lt;br /&gt;   keyboard-&amp;gt;Unacquire(); &lt;br /&gt;   dinput-&amp;gt;Release(); &lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;LRESULT CALLBACK WindowProc(HWND hWnd,&lt;br /&gt;                                                             UINT message,&lt;br /&gt;                                                             WPARAM wParam,&lt;br /&gt;                                                             LPARAM lParam){&lt;br /&gt;   if(message == WM_DESTROY){&lt;br /&gt;      PostQuitMessage(0);&lt;br /&gt;      return 0;&lt;br /&gt;   }&lt;br /&gt;   return DefWindowProc(hWnd,&lt;br /&gt;                                        message,&lt;br /&gt;                                        wParam,&lt;br /&gt;                                        lParam);&lt;br /&gt;}&lt;/div&gt;You can ask anything about how it works but I`m just a beginner in DirectX programming...&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5682776130825686229-5494779758440019457?l=www.codingmix.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/hZnUqGvdaWHLuyDKJ2SxDsmpwE8/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/hZnUqGvdaWHLuyDKJ2SxDsmpwE8/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/hZnUqGvdaWHLuyDKJ2SxDsmpwE8/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/hZnUqGvdaWHLuyDKJ2SxDsmpwE8/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/codingmix/Cplusplus/~4/nzddpsqhEUU" height="1" width="1"/&gt;</description><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">12</thr:total><enclosure url="http://www.youtube.com/v/K8_UpmEfEnU?fs=1&amp;amp;hl=en_US&amp;amp;rel=0&amp;amp;color1=0x234900&amp;amp;color2=0x4e9e00" length="1077" type="application/x-shockwave-flash" /><media:content url="http://www.youtube.com/v/K8_UpmEfEnU?fs=1&amp;amp;hl=en_US&amp;amp;rel=0&amp;amp;color1=0x234900&amp;amp;color2=0x4e9e00" fileSize="1077" type="application/x-shockwave-flash" /><feedburner:origLink>http://www.codingmix.com/2010/12/2d-game-directx-snake-cplusplus.html</feedburner:origLink></item><item><title>Addition with extremely huge numbers in C++</title><link>http://feedproxy.google.com/~r/codingmix/Cplusplus/~3/oqQeMw4Op6M/addition-with-extremely-huge-numbers-in.html</link><category>Cplusplus projects</category><category>Cplusplus</category><author>noreply@blogger.com (Csabi)</author><pubDate>Fri, 03 Dec 2010 11:40:56 PST</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-5682776130825686229.post-7400027607781102421</guid><description>In C++ you the data type with the largest range is the &lt;i&gt;long long&lt;/i&gt;, with a maximum limit of ~19 digits. This means that you can`t make additions with numbers that result a larger number. I know this is a crazy thing, why would you need to calculate with such huge numbers ? I think never..., but the method is very useful and ingenious. &lt;div id="full-post"&gt;Here is my code:&lt;div class="code"&gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;#include &amp;lt;string&amp;gt;&lt;br /&gt;&lt;br /&gt;using namespace std;&lt;br /&gt;&lt;br /&gt;int main(){&lt;br /&gt;   string nr; &lt;br /&gt;   bool validnr = false;&lt;br /&gt;   while(!validnr){&lt;br /&gt;      cout &amp;lt;&amp;lt; &amp;quot;Enter the first number: &amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;      cin &amp;gt;&amp;gt; nr;&lt;br /&gt;      validnr = true;&lt;br /&gt;      &lt;span style="color:green;"&gt;//Check the number for invalid characters&lt;/span&gt;&lt;br /&gt;      for(int i=0;i&amp;lt;nr.length();i++){&lt;br /&gt;         if((int) nr[i] &amp;lt; 48 || (int) nr[i] &amp;gt; 57){&lt;br /&gt;            &lt;span style="color:green;"&gt;//48 is the ASCII code of number 0&lt;/span&gt;&lt;br /&gt;            &lt;span style="color:green;"&gt;//57 is the ASCII code of number 9&lt;/span&gt;&lt;br /&gt;            validnr = false; &lt;br /&gt;         }&lt;br /&gt;      }&lt;br /&gt;      if(!validnr){&lt;br /&gt;         cout &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; &amp;quot;Please enter a valid number!&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;      }&lt;br /&gt;   }&lt;br /&gt;   string nr2;&lt;br /&gt;   validnr = false;&lt;br /&gt;   &lt;span style="color:green;"&gt;//Do the same for the second number&lt;/span&gt;&lt;br /&gt;   while(!validnr){&lt;br /&gt;      cout &amp;lt;&amp;lt; &amp;quot;Enter the second number: &amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;      cin &amp;gt;&amp;gt; nr2;&lt;br /&gt;      validnr = true;&lt;br /&gt;      for(int i=0;i&amp;lt;nr2.length();i++){&lt;br /&gt;         if((int) nr2[i] &amp;lt; 48 || (int) nr2[i] &amp;gt; 57){&lt;br /&gt;            validnr = false; &lt;br /&gt;         }&lt;br /&gt;      }&lt;br /&gt;      if(!validnr){&lt;br /&gt;         cout &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; &amp;quot;Please enter a valid number!&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;      }&lt;br /&gt;   }&lt;br /&gt;   int digitsnr;&lt;br /&gt;   &lt;span style="color:green;"&gt;//Make the two digits to have the same size&lt;/span&gt;&lt;br /&gt;   &lt;span style="color:green;"&gt;//by adding 0`s in front of it&lt;/span&gt;&lt;br /&gt;   &lt;span style="color:green;"&gt;//This is important because we can`t add a digit with nothing&lt;/span&gt;&lt;br /&gt;   if(nr.length() &amp;gt; nr2.length()){&lt;br /&gt;      digitsnr = nr.length() - 1;&lt;br /&gt;      while(nr.length() != nr2.length()){&lt;br /&gt;         nr2 = &amp;quot;0&amp;quot; + nr2;&lt;br /&gt;      }&lt;br /&gt;   }&lt;br /&gt;   else{&lt;br /&gt;      digitsnr = nr2.length() - 1;&lt;br /&gt;      while(nr.length() != nr2.length()){&lt;br /&gt;         nr = &amp;quot;0&amp;quot; + nr;&lt;br /&gt;      }&lt;br /&gt;   }&lt;br /&gt;   string result;&lt;br /&gt;   char digit;&lt;br /&gt;   int remainder = 0; &lt;span style="color:green;"&gt;//If the result of two digits is more than 10&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:green;"&gt;//we need to add 1 to the following digit(e.g: 5 + 7 = 12, 12 is made from 2 digits&lt;/span&gt; &lt;br /&gt;   for(int i=digitsnr;i&amp;gt;=0;i--){ &lt;span style="color:green;"&gt;//Loop through each digit&lt;/span&gt;&lt;br /&gt;      if(remainder + nr[i] + nr2[i] - 96 &amp;gt; 9){&lt;br /&gt;         digit = remainder + nr[i] + nr2[i] - 48 - 10; &lt;span style="color:green;"&gt;//We are working with ASCII codes&lt;/span&gt;&lt;br /&gt;         result = digit + result;&lt;br /&gt;         remainder = 1; &lt;span style="color:green;"&gt;//We have to increase the following digit&lt;/span&gt;&lt;br /&gt;      }&lt;br /&gt;      else{&lt;br /&gt;         digit = remainder + nr[i] + nr2[i] - 48;&lt;br /&gt;         remainder = 0;&lt;br /&gt;         result = digit + result;&lt;br /&gt;      }&lt;br /&gt;   }&lt;br /&gt;   if(remainder){&lt;br /&gt;      result = &amp;quot;1&amp;quot; + result; &lt;br /&gt;     &lt;span style="color:green;"&gt;//Add the last remaining if required&lt;/span&gt;&lt;br /&gt;   }&lt;br /&gt;   cout &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; &amp;quot;Result = &amp;quot; &amp;lt;&amp;lt; result &amp;lt;&amp;lt; endl;&lt;br /&gt;   system(&amp;quot;pause&amp;quot;);&lt;br /&gt;}&lt;/div&gt;The numbers are stored in strings, each character of the strings is checked to avoid wrong characters. Each character is checked using it`s ASCII code, (for example 0 has the 47 code number, 1 has the 49 code). After the numbers are validated the two numbers are added digit by digit starting from the last number:&lt;div class="code"&gt;123 + 56 = &lt;br /&gt;9&lt;br /&gt;79&lt;br /&gt;179&lt;/div&gt;Is that simple, but if something it`s not clear please don`t hesitate, ASK ME!&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5682776130825686229-7400027607781102421?l=www.codingmix.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/jY9W9vOB8FaoH8O6qQfrGbFDl-Y/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/jY9W9vOB8FaoH8O6qQfrGbFDl-Y/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/jY9W9vOB8FaoH8O6qQfrGbFDl-Y/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/jY9W9vOB8FaoH8O6qQfrGbFDl-Y/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/codingmix/Cplusplus/~4/oqQeMw4Op6M" height="1" width="1"/&gt;</description><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">7</thr:total><feedburner:origLink>http://www.codingmix.com/2010/12/addition-with-extremely-huge-numbers-in.html</feedburner:origLink></item><item><title>A very simple C++ Tic Tac Toe game (v2)</title><link>http://feedproxy.google.com/~r/codingmix/Cplusplus/~3/19jp-lgHtyc/very-simple-cplusplus-tic-tac-toe-game.html</link><category>Cplusplus projects</category><category>Cplusplus</category><author>noreply@blogger.com (Csabi)</author><pubDate>Fri, 01 Oct 2010 13:45:13 PDT</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-5682776130825686229.post-1501933207832143526</guid><description>This is my second C++ Tic Tac Toe game. This one has a shorter source-code because instead of using a variable for each square I`we used a two-dimensional array. With arrays is more simple to check if a square is available or to check for an eventual winner. &lt;div id="full-post"&gt;The rest of the game is almost the same as the &lt;a href="http://www.codingmix.com/2010/07/simples-cplusplus-tic-tac-toe-game-ever_24.html"&gt;first version&lt;/a&gt;, the only difference is that I`we used &lt;i&gt;char&lt;/i&gt;s instead of &lt;i&gt;int&lt;/i&gt;s to show the state of the squares.&lt;br /&gt;&lt;br /&gt;Required knowledge: &lt;a href="http://www.codingmix.com/2010/06/cplusplus-lesson-4-variables.html"&gt;variables&lt;/a&gt;, &lt;a href="http://www.codingmix.com/2010/06/cplusplus-lesson-5-inputoutput.html"&gt;input/output&lt;/a&gt;, &lt;a href="http://www.codingmix.com/2010/06/cplusplus-lesson-6-ifelseelse-if_18.html"&gt;if/else/else if statements&lt;/a&gt;, &lt;a href="http://www.codingmix.com/2010/07/cplusplus-lesson-8-while-loop-and-for.html"&gt;loop&lt;/a&gt;, &lt;a href="http://www.codingmix.com/2010/09/cplusplus-lesson-9-arrays.html"&gt;arrays&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;Here is my code:&lt;div class="code"&gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;&lt;br /&gt;using namespace std;&lt;br /&gt;&lt;br /&gt;int main(){&lt;br /&gt;   &lt;span style="color:green;"&gt;//This array of &lt;i&gt;char&lt;/i&gt;s represents the game board, and it holds the content of each square (x,o or empty)&lt;/span&gt;&lt;br /&gt;   char square[3][3] = {{' ',' ',' '},{' ',' ',' '},{' ',' ',' '}};&lt;br /&gt;   &lt;span style="color:green;"&gt;//The current player (x or o)&lt;/span&gt;&lt;br /&gt;   char turn = 'X';&lt;br /&gt;   &lt;span style="color:green;"&gt;//These variables will hold the number of the row and column selected by the players.&lt;/span&gt;&lt;br /&gt;   int row;&lt;br /&gt;   int column;&lt;br /&gt;   &lt;span style="color:green;"&gt;//The winner (x, o, t(if it`s a tie) or empty)&lt;/span&gt;&lt;br /&gt;   char winner = ' ';&lt;br /&gt;   &lt;span style="color:green;"&gt;//Required variables to check if a move is valid or not&lt;/span&gt;&lt;br /&gt;   bool validmove;&lt;br /&gt;   bool validrow;&lt;br /&gt;   bool validcolumn;&lt;br /&gt;   &lt;span style="color:green;"&gt;//The game loop`s again and again until the game is over&lt;/span&gt;&lt;br /&gt;   while(winner == ' '){&lt;br /&gt;      &lt;span style="color:green;"&gt;//Draw the board&lt;/span&gt;&lt;br /&gt;      cout &amp;lt;&amp;lt; &amp;quot; 1 2 3&amp;quot; &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; &amp;quot; +---+---+---+&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;      for(int i=1;i&amp;lt;4;i++){&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot; &amp;quot; &amp;lt;&amp;lt; i &amp;lt;&amp;lt; &amp;quot; | &amp;quot; &amp;lt;&amp;lt; square[i-1][0] &amp;lt;&amp;lt; &amp;quot; | &amp;quot; &amp;lt;&amp;lt; square[i-1][1] &amp;lt;&amp;lt; &amp;quot; | &amp;quot; &amp;lt;&amp;lt; square[i-1][2] &amp;lt;&amp;lt; &amp;quot; |&amp;quot; &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; &amp;quot; +---+---+---+&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;      }&lt;br /&gt;      &lt;span style="color:green;"&gt;//Check for an eventual winner of for a tie&lt;/span&gt;&lt;br /&gt;      for(int i=0;i&amp;lt;3;i++){&lt;br /&gt;         if(square[i][0] == square[i][1] &amp;amp;&amp;amp; square[i][1] == square[i][2] &amp;amp;&amp;amp; square[i][0] != ' '){&lt;br /&gt;            winner = square[i][0];&lt;br /&gt;         }&lt;br /&gt;      }&lt;br /&gt;      for(int i=0;i&amp;lt;3;i++){&lt;br /&gt;         if(square[0][i] == square[1][i] &amp;amp;&amp;amp; square[1][i] == square[2][i] &amp;amp;&amp;amp; square[0][i] != ' '){&lt;br /&gt;            winner = square[0][i];&lt;br /&gt;         }&lt;br /&gt;      }&lt;br /&gt;      if(square[0][0] == square[1][1] &amp;amp;&amp;amp; square[1][1] == square[2][2] &amp;amp;&amp;amp; square[0][0] != ' '){&lt;br /&gt;         winner = square[0][0];&lt;br /&gt;      }&lt;br /&gt;      if(square[0][2] == square[1][1] &amp;amp;&amp;amp; square[1][1] == square[2][0] &amp;amp;&amp;amp; square[0][2] != ' '){&lt;br /&gt;         winner = square[0][2];&lt;br /&gt;      }&lt;br /&gt;      if(square[0][0] == square[0][1] &amp;amp;&amp;amp; square[0][1] == square[0][2] &amp;amp;&amp;amp; square[0][2] == square[1][0] &amp;amp;&amp;amp; square[1][0] == square[1][1] &amp;amp;&amp;amp; square[1][1] == square[1][2] &amp;amp;&amp;amp; square[1][2] == square[2][0] &amp;amp;&amp;amp; square[2][0] == square[2][1] &amp;amp;&amp;amp; square[2][1] == square[2][2] &amp;amp;&amp;amp; square[0][0] != ' '){&lt;br /&gt;         winner = 't';&lt;br /&gt;      }&lt;br /&gt;      &lt;span style="color:green;"&gt;//If somebody won&lt;/span&gt;&lt;br /&gt;      if(winner == 'X' || winner == 'O'){&lt;br /&gt;         &lt;span style="color:green;"&gt;//Display congratulations message&lt;/span&gt;&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot;Congratulations! Player &amp;quot;;&lt;br /&gt;         if(winner == 'X'){&lt;br /&gt;            cout &amp;lt;&amp;lt; 1;&lt;br /&gt;         }&lt;br /&gt;         else{&lt;br /&gt;            cout &amp;lt;&amp;lt; 2;&lt;br /&gt;         }&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot; is the winner!&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;         break;&lt;br /&gt;      }&lt;br /&gt;      else if(winner == 't'){&lt;br /&gt;         &lt;span style="color:green;"&gt;//Display a message if it`s tie&lt;/span&gt;&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot;Tie!&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;         break;&lt;br /&gt;      }&lt;br /&gt;      &lt;span style="color:green;"&gt;//If the game is not yet over show who`s the next player to move&lt;/span&gt;&lt;br /&gt;      cout &amp;lt;&amp;lt; &amp;quot;Player &amp;quot;;&lt;br /&gt;      if(turn == 'X'){&lt;br /&gt;         cout &amp;lt;&amp;lt; 1;&lt;br /&gt;      }&lt;br /&gt;      else{&lt;br /&gt;         cout &amp;lt;&amp;lt; 2;&lt;br /&gt;      } &lt;br /&gt;      cout &amp;lt;&amp;lt; &amp;quot;'s turn:&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;      &lt;span style="color:green;"&gt;//Loop until the player selects a valid square&lt;/span&gt;&lt;br /&gt;      validmove = false;&lt;br /&gt;      while(!validmove){&lt;br /&gt;         validrow = false;&lt;br /&gt;         &lt;span style="color:green;"&gt;//Loop until the player selects a valid row&lt;/span&gt;&lt;br /&gt;         while(!validrow){&lt;br /&gt;            cout &amp;lt;&amp;lt; &amp;quot;Row: &amp;quot;;&lt;br /&gt;            cin &amp;gt;&amp;gt; row;&lt;br /&gt;            if(row == 1 || row == 2 || row == 3){&lt;br /&gt;               validrow = true;&lt;br /&gt;            }&lt;br /&gt;            else{&lt;br /&gt;               cout &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; &amp;quot;Invalid row!&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;            }&lt;br /&gt;         }&lt;br /&gt;         validcolumn = false;&lt;br /&gt;         &lt;span style="color:green;"&gt;//Loop until the player selects a valid column&lt;/span&gt;&lt;br /&gt;         while(!validcolumn){&lt;br /&gt;            cout &amp;lt;&amp;lt; &amp;quot;Column: &amp;quot;;&lt;br /&gt;            cin &amp;gt;&amp;gt; column;&lt;br /&gt;            if(column == 1 || column == 2 || column == 3){&lt;br /&gt;               validcolumn = true;&lt;br /&gt;            }&lt;br /&gt;            else{&lt;br /&gt;               cout &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; &amp;quot;Invalid column!&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;            }&lt;br /&gt;         }&lt;br /&gt;         &lt;span style="color:green;"&gt;//Change the turn to the next player&lt;/span&gt;&lt;br /&gt;         if(square[row-1][column-1] == ' '){&lt;br /&gt;            square[row-1][column-1] = turn;&lt;br /&gt;            validmove = true;&lt;br /&gt;            if(turn == 'X'){&lt;br /&gt;               turn = 'O';&lt;br /&gt;            }&lt;br /&gt;            else{&lt;br /&gt;               turn = 'X';&lt;br /&gt;            }&lt;br /&gt;         }&lt;br /&gt;         &lt;span style="color:green;"&gt;//If the selected square is occupied display a message and loop again&lt;/span&gt;&lt;br /&gt;         else{&lt;br /&gt;            cout &amp;lt;&amp;lt; &amp;quot;The selected square is occupied!&amp;quot; &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; &amp;quot;Select again:&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;         }&lt;br /&gt;      }&lt;br /&gt;   }&lt;br /&gt;   system(&amp;quot;pause&amp;quot;);&lt;br /&gt;}&lt;/div&gt;If something it`s not clear or you have any question just ask!&lt;br /&gt;If this is too advanced for you feel free to read my lessons or view the previous, simpler version of the game here: &lt;a href="http://www.codingmix.com/2010/07/simples-cplusplus-tic-tac-toe-game-ever_24.html"&gt;The simplest C++ Tic Tac Toe game ever&lt;/a&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5682776130825686229-1501933207832143526?l=www.codingmix.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/8q_2FgIqBSesRcExPKZNJG4At4c/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/8q_2FgIqBSesRcExPKZNJG4At4c/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/8q_2FgIqBSesRcExPKZNJG4At4c/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/8q_2FgIqBSesRcExPKZNJG4At4c/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/codingmix/Cplusplus/~4/19jp-lgHtyc" height="1" width="1"/&gt;</description><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">3</thr:total><feedburner:origLink>http://www.codingmix.com/2010/10/very-simple-cplusplus-tic-tac-toe-game.html</feedburner:origLink></item><item><title>C++ Console Calculator v3</title><link>http://feedproxy.google.com/~r/codingmix/Cplusplus/~3/k6ppczSGwP8/cplusplus-console-calculator-v3.html</link><category>Cplusplus projects</category><category>Cplusplus</category><author>noreply@blogger.com (Csabi)</author><pubDate>Sat, 16 Oct 2010 07:13:34 PDT</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-5682776130825686229.post-232818944587829691</guid><description>This is a tutorial about creating the third version of my C++ Calculator (console application). Here are the first and second versions: &lt;a href="http://www.codingmix.com/2010/06/cplusplus-console-calculator-v1.html"&gt;Calculator v1&lt;/a&gt; and &lt;a href="http://www.codingmix.com/2010/06/cplusplus-console-calculator-v2.html"&gt;Calculator v2&lt;/a&gt;. The previous versions of this applications are closed after displaying a warning message when an invalid operation is selected. In this version this was improved: the programs shows an error message and it asks again and again until a valid operation is selected. This version is also a bit shorter because in some places I`we used arrays and loops.&lt;div id="full-post"&gt;&lt;br /&gt;Required knowledge: &lt;a href="http://www.codingmix.com/2010/06/cplusplus-lesson-4-variables.html"&gt;variables&lt;/a&gt;, &lt;a href="http://www.codingmix.com/2010/06/cplusplus-lesson-5-inputoutput.html"&gt;input/output&lt;/a&gt;, &lt;a href="http://www.codingmix.com/2010/06/cplusplus-lesson-6-ifelseelse-if_18.html"&gt;if/else/else if statements&lt;/a&gt;, &lt;a href="http://www.codingmix.com/2010/06/cplusplus-lesson-7-switchcase-statement.html"&gt;switch/case statements&lt;/a&gt;, &lt;a href="http://www.codingmix.com/2010/07/cplusplus-lesson-8-while-loop-and-for.html"&gt;loop&lt;/a&gt;, &lt;a href="http://www.codingmix.com/2010/09/cplusplus-lesson-9-arrays.html"&gt;arrays&lt;/a&gt;.&lt;br /&gt;Here is my code: &lt;div class="code"&gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;&lt;br /&gt;using namespace std;&lt;br /&gt;&lt;br /&gt;int main(){&lt;br /&gt;   cout &amp;lt;&amp;lt; &amp;quot;Enter the first number:&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;   int numone;&lt;br /&gt;   cin &amp;gt;&amp;gt; numone;&lt;br /&gt;   bool validselection = false;&lt;br /&gt;   char operation[] = {'*','/','+','-'};&lt;br /&gt;   int selectedoperation;&lt;br /&gt;   while(!validselection){&lt;br /&gt;      cout &amp;lt;&amp;lt; &amp;quot;Select an operation:&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;      for(int i=0;i&amp;lt;4;i++){&lt;br /&gt;         cout &amp;lt;&amp;lt; i+1 &amp;lt;&amp;lt; &amp;quot;. &amp;quot; &amp;lt;&amp;lt; operation[i] &amp;lt;&amp;lt; endl;&lt;br /&gt;      }&lt;br /&gt;      cin &amp;gt;&amp;gt; selectedoperation;&lt;br /&gt;      if(selectedoperation == 1 || selectedoperation == 2 || selectedoperation == 3 || selectedoperation == 4){&lt;br /&gt;         validselection = true;&lt;br /&gt;      }&lt;br /&gt;      else{&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot;Invalid selection!&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;      }&lt;br /&gt;   }&lt;br /&gt;   cout &amp;lt;&amp;lt; &amp;quot;Enter the second number:&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;   int numtwo;&lt;br /&gt;   cin &amp;gt;&amp;gt; numtwo;&lt;br /&gt;   float result;&lt;br /&gt;   bool dividebyzero = false;&lt;br /&gt;   switch(selectedoperation){&lt;br /&gt;   case 1:&lt;br /&gt;      result = numone * numtwo;&lt;br /&gt;      break;&lt;br /&gt;   case 2:&lt;br /&gt;      if(numtwo == 0){&lt;br /&gt;         dividebyzero = true;&lt;br /&gt;      }&lt;br /&gt;      else{&lt;br /&gt;         result = (float)numone / numtwo;&lt;br /&gt;      }&lt;br /&gt;      break;&lt;br /&gt;   case 3:&lt;br /&gt;      result = numone + numtwo;&lt;br /&gt;      break;&lt;br /&gt;   case 4:&lt;br /&gt;      result = numone - numtwo;&lt;br /&gt;      break;&lt;br /&gt;   }&lt;br /&gt;   if(!dividebyzero){&lt;br /&gt;      cout &amp;lt;&amp;lt; numone &amp;lt;&amp;lt; &amp;quot; &amp;quot; &amp;lt;&amp;lt; operation[selectedoperation - 1] &amp;lt;&amp;lt; &amp;quot; &amp;quot; &amp;lt;&amp;lt; numtwo &amp;lt;&amp;lt; &amp;quot; = &amp;quot; &amp;lt;&amp;lt; result &amp;lt;&amp;lt; endl;&lt;br /&gt;   }&lt;br /&gt;   else{&lt;br /&gt;      cout &amp;lt;&amp;lt; &amp;quot;Cannot divide by zero!&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;   }&lt;br /&gt;   system(&amp;quot;pause&amp;quot;);&lt;br /&gt;}&lt;/div&gt;This works almost the same way as the &lt;a href="http://www.codingmix.com/2010/06/cplusplus-console-calculator-v2.html"&gt;Calculator v2&lt;/a&gt;, the difference is that this version uses a loop and an array to display the available operations instead of multiple &lt;i&gt;cout&lt;/i&gt;`s and this whole part of code where the operation is asked is inside a loop and it loops until a valid operation is selected.&lt;br /&gt;This was the second version, but there will be more advanced ones, too.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5682776130825686229-232818944587829691?l=www.codingmix.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/VmbcWNaUj9DNP9CZV-Oj2c3tXF8/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/VmbcWNaUj9DNP9CZV-Oj2c3tXF8/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/VmbcWNaUj9DNP9CZV-Oj2c3tXF8/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/VmbcWNaUj9DNP9CZV-Oj2c3tXF8/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/codingmix/Cplusplus/~4/k6ppczSGwP8" height="1" width="1"/&gt;</description><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total><feedburner:origLink>http://www.codingmix.com/2010/09/cplusplus-console-calculator-v3.html</feedburner:origLink></item><item><title>C++ Lesson 9 - Arrays</title><link>http://feedproxy.google.com/~r/codingmix/Cplusplus/~3/RqoY7CwOovA/cplusplus-lesson-9-arrays.html</link><category>Cplusplus lessons</category><category>Cplusplus</category><author>noreply@blogger.com (Csabi)</author><pubDate>Fri, 03 Sep 2010 08:46:32 PDT</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-5682776130825686229.post-4106824301066022382</guid><description>Imagine a running competition with 5 runners. We want to store the seconds needed to finish the race for each runner. We would need 5 variables, one for each runner:&lt;div id="full-post"&gt;&lt;div class="code"&gt;int runner1 = 200;&lt;br /&gt;int runner2 = 105;&lt;br /&gt;int runner3 = 340;&lt;br /&gt;int runner4 = 243;&lt;br /&gt;int runner5 = 345;&lt;/div&gt;The disadvantage of the above code is that we can`t loop through the results. In this situation the best method is to use an &lt;b&gt;array&lt;/b&gt;.&lt;br /&gt;An array would look in the following way:&lt;div class="code"&gt;runner[0] = 200;&lt;br /&gt;runner[1] = 105;&lt;br /&gt;runner[2] = 340;&lt;br /&gt;runner[3] = 243;&lt;br /&gt;runner[4] = 345;&lt;/div&gt;&lt;br /&gt;Those numbers in the brackets are the &lt;b&gt;index&lt;/b&gt; number, and these numbers are showing the position of a given value in the array. &lt;br /&gt;&lt;b&gt;Initializing arrays&lt;/b&gt;&lt;br /&gt;An array can be initialized in three different ways:&lt;br /&gt;&lt;br /&gt;&lt;b&gt;1.&lt;/b&gt; Syntax:&lt;div class="code"&gt;type identifier[max_elements_number];&lt;/div&gt;Example: &lt;div class="code"&gt;int runner[5];&lt;br /&gt;runner[0] = 200;&lt;br /&gt;runner[1] = 105;&lt;br /&gt;runner[2] = 340;&lt;br /&gt;runner[3] = 243;&lt;br /&gt;runner[4] = 345&lt;/div&gt; &lt;br /&gt;&lt;b&gt;2.&lt;/b&gt; Syntax:&lt;div class="code"&gt;type identifier[max_elements_number] = {element_1,element_2,element_max_elements_numbe};&lt;/div&gt;Example:&lt;div class="code"&gt;int runner[5] = {200,105,340,243,345};&lt;/div&gt;The number of elements can be fewer than the max_elements_number, but not greater.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;3.&lt;/b&gt; Syntax:&lt;div class="code"&gt;type identifier[] = {element_1,element_2,element_n};&lt;/div&gt;Example: &lt;div class="code"&gt;int runner[] = {200,105,340,243,345};&lt;/div&gt;The maximum number of elements is generated automatically based on the added elements number.&lt;br /&gt;&lt;br /&gt;The &lt;i&gt;runner&lt;/i&gt; array look in the following way for the computer:&lt;div class="code"&gt;&lt;div style="float:left;"&gt;runner = &lt;/div&gt;&lt;table style="float:left;" width="200" border="1"&gt;&lt;tr&gt;&lt;td&gt;200&lt;/td&gt;&lt;td&gt;105&lt;/td&gt;&lt;td&gt;340&lt;/td&gt;&lt;td&gt;243&lt;/td&gt;&lt;td&gt;345&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;div style="float:left;width:520px;"&gt;                 (0)     (1)     (2)     (3)     (4)&lt;/div&gt;&lt;/div&gt;This is an &lt;b&gt;one-dimensional array&lt;/b&gt;, because it has only one index number. So, yes an array can have more than one index. Now you probably ask, why to use more than one indexes ?&lt;br /&gt;&lt;br /&gt;Well, let`s continue the idea of a running competition. Now imagine that you need to store the timings of the last 3 competitions for our 5 runners. The new array would look like this: &lt;div class="code"&gt;&lt;div style="float:left;"&gt;runner = &lt;/div&gt;&lt;table style="float:left;" width="200" border="1"&gt;&lt;tr&gt;&lt;td&gt;200&lt;/td&gt;&lt;td&gt;105&lt;/td&gt;&lt;td&gt;340&lt;/td&gt;&lt;td&gt;243&lt;/td&gt;&lt;td&gt;345&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;223&lt;/td&gt;&lt;td&gt;324&lt;/td&gt;&lt;td&gt;323&lt;/td&gt;&lt;td&gt;454&lt;/td&gt;&lt;td&gt;123&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;245&lt;/td&gt;&lt;td&gt;203&lt;/td&gt;&lt;td&gt;150&lt;/td&gt;&lt;td&gt;340&lt;/td&gt;&lt;td&gt;311&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;div style="float:left;line-height:27px;"&gt;(0)&lt;br /&gt;(1)&lt;br /&gt;(2)&lt;/div&gt;&lt;div style="float:left;width:520px;"&gt;                 (0)     (1)     (2)     (3)     (4)&lt;/div&gt;&lt;/div&gt;So we have 5 runners and a column for each of them. We have 3 competitions and a row for each of them. &lt;br /&gt;&lt;br /&gt;&lt;b&gt;Getting a value from this table&lt;/b&gt; &lt;br /&gt;Let`s say we need the timing of runner 2`s from the competition 1: &lt;div class="code"&gt;cout &lt;&lt; runner[2][1]; //323&lt;/div&gt; &lt;br /&gt;&lt;b&gt;Initializing a multi-dimensional array&lt;/b&gt;&lt;br /&gt;Because this is very similar to the one-dimensional arrays I will show you just some example:&lt;div class="code"&gt;int runner[5][3];&lt;br /&gt;&lt;br /&gt;or&lt;br /&gt;&lt;br /&gt;int runner[5][3] = {{200,223,245},{105,324,203},{340,323,150},{243,454,340},{345,123,311}};&lt;br /&gt;&lt;br /&gt;or&lt;br /&gt;&lt;br /&gt;int runner[][3] = {{200,223,245},{105,324,203},{340,323,150},{243,454,340},{345,123,311}};&lt;br /&gt;&lt;b&gt;Note&lt;/b&gt;: With multi-dimensional arrays only the first index can be leaved blank!&lt;/div&gt;&lt;br /&gt;The max_elements_number needs to be a &lt;b&gt;constant&lt;/b&gt; value, this can`t be changed during the program runs. The first element of an array is always the 0th element.&lt;br /&gt;&lt;br /&gt;Here is an example of how useful can arrays be: &lt;div class="code"&gt;#include &lt;iostream&gt;&lt;br /&gt;&lt;br /&gt;using namespace std;&lt;br /&gt;&lt;br /&gt;int main(){&lt;br /&gt;   int number[11];&lt;br /&gt;   for(int i=0;i&lt;=10;i++){&lt;br /&gt;      number[i] = i;&lt;br /&gt;   }&lt;br /&gt;   for(int i=0;i&lt;=10;i++){&lt;br /&gt;      cout &lt;&lt; number[i] &lt;&lt; endl;&lt;br /&gt;   }&lt;br /&gt;   system("pause");&lt;br /&gt;}&lt;/div&gt;The above program simply loads an array with numbers from 0 to 10 (11 elements) and then writes out the numbers. Using simple variables this would not be possible, we would need 11 variables and many lines of code for the same result.&lt;br /&gt;&lt;br /&gt;Next lesson: Coming soon&lt;br /&gt;Previous lesson: &lt;a href="http://www.codingmix.com/2010/07/cplusplus-lesson-8-while-loop-and-for.html"&gt;C++ Lesson 8 - while loop and for loop&lt;/a&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5682776130825686229-4106824301066022382?l=www.codingmix.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/5hv5sL52hCj6uA6z7aaE8dlfHxY/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/5hv5sL52hCj6uA6z7aaE8dlfHxY/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/5hv5sL52hCj6uA6z7aaE8dlfHxY/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/5hv5sL52hCj6uA6z7aaE8dlfHxY/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/codingmix/Cplusplus/~4/RqoY7CwOovA" height="1" width="1"/&gt;</description><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total><feedburner:origLink>http://www.codingmix.com/2010/09/cplusplus-lesson-9-arrays.html</feedburner:origLink></item><item><title>The simplest C++ Tic Tac Toe game ever</title><link>http://feedproxy.google.com/~r/codingmix/Cplusplus/~3/JAtW6iOuxJQ/simples-cplusplus-tic-tac-toe-game-ever_24.html</link><category>Cplusplus projects</category><category>Cplusplus</category><author>noreply@blogger.com (Csabi)</author><pubDate>Sat, 02 Oct 2010 03:56:13 PDT</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-5682776130825686229.post-2061702966661096784</guid><description>This is the simplest C++ Tic Tac Toe game ever, it`s made using only the most basic statements: &lt;a href="http://www.codingmix.com/2010/06/cplusplus-lesson-5-inputoutput.html"&gt;cout&lt;/a&gt;, &lt;a href="http://www.codingmix.com/2010/06/cplusplus-lesson-5-inputoutput.html"&gt;cin&lt;/a&gt;, &lt;a href="http://www.codingmix.com/2010/06/cplusplus-lesson-4-variables.html"&gt;variables&lt;/a&gt;, &lt;a href="http://www.codingmix.com/2010/06/cplusplus-lesson-6-ifelseelse-if_18.html"&gt;if/else/else if&lt;/a&gt;, &lt;a href="http://www.codingmix.com/2010/06/cplusplus-lesson-7-switchcase-statement.html"&gt;switch/case&lt;/a&gt; and &lt;a href="http://www.codingmix.com/2010/07/cplusplus-lesson-8-while-loop-and-for.html"&gt;loops&lt;/a&gt;&lt;div id="full-post"&gt;&lt;br /&gt;The game uses 9 variables, one for each square of the Tic Tac Toe game board, each of them can have 3 different value:&lt;div class="code"&gt;0 - free square&lt;br /&gt;1 - occupied by an x&lt;br /&gt;2 - occupied by a 0&lt;/div&gt;The board is redrawn after each move in a loop. The whole action of the game happens in this loop. First the board is checked for an eventual winner or for a tie. If the game is not over yet than it`s time to move. First the move is checked, if it`s not a valid move a message is displayed and the player needs to pick an another square, if the move is valid than the loop runs again, but this time is the other player`s turn.&lt;div class="code"&gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;&lt;br /&gt;using namespace std;&lt;br /&gt;&lt;br /&gt;int main(){&lt;br /&gt;   &lt;span style="color:green;"&gt;//These variables are holding the value of each square (0 = free, 1 = x, 2 = o)&lt;/span&gt;&lt;br /&gt;   int one = 0;&lt;br /&gt;   int two = 0;&lt;br /&gt;   int three = 0;&lt;br /&gt;   int four = 0;&lt;br /&gt;   int five = 0;&lt;br /&gt;   int six = 0;&lt;br /&gt;   int seven = 0;&lt;br /&gt;   int eight = 0;&lt;br /&gt;   int nine = 0;&lt;br /&gt;   &lt;span style="color:green;"&gt;//This holds the number of the current player (1 = player1(x), 2 = player2(o)&lt;/span&gt;&lt;br /&gt;   int turn = 1;&lt;br /&gt;   &lt;span style="color:green;"&gt;//These variables are holding the row and column number inputted by a player&lt;/span&gt;&lt;br /&gt;   int row;&lt;br /&gt;   int column;&lt;br /&gt;   &lt;span style="color:green;"&gt;//These variables will be used later to check if a move is valid or not&lt;/span&gt;&lt;br /&gt;   bool validmove;&lt;br /&gt;   bool validrow;&lt;br /&gt;   bool validcolumn;&lt;br /&gt;   &lt;span style="color:green;"&gt;//The bellow variable holds the number of the winner, when the value is not 0 means the game is over&lt;/span&gt;&lt;br /&gt;   int winner = 0;&lt;br /&gt;   &lt;span style="color:green;"&gt;//The game loops again and again until somebody wins or it`s tie (winner = 3)&lt;/span&gt;&lt;br /&gt;   while(winner == 0){&lt;br /&gt;      &lt;span style="color:green;"&gt;//Draw the board from simple +,- and | characters&lt;/span&gt;&lt;br /&gt;      cout &amp;lt;&amp;lt; &amp;quot;1 2 3&amp;quot; &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; &amp;quot;+---+---+---+&amp;quot; &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; &amp;quot;1 | &amp;quot;;&lt;br /&gt;      if(one == 0){&lt;br /&gt;         &lt;span style="color:green;"&gt;//For each square the variable of that square is checked, and based on it`s value a character is written (0 - nothing, 1 = x, 2 = o)&lt;/span&gt;&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot;|&amp;quot;;&lt;br /&gt;      }&lt;br /&gt;      else if(one == 1){&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot;x |&amp;quot;;&lt;br /&gt;      }&lt;br /&gt;      else{&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot;o |&amp;quot;;&lt;br /&gt;      }&lt;br /&gt;      if(two == 0){&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot; |&amp;quot;;&lt;br /&gt;      }&lt;br /&gt;      else if(two == 1){&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot; x |&amp;quot;;&lt;br /&gt;      }&lt;br /&gt;      else{&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot; o |&amp;quot;;&lt;br /&gt;      }&lt;br /&gt;      if(three == 0){&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot; |&amp;quot;;&lt;br /&gt;      }&lt;br /&gt;      else if(three == 1){&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot; x |&amp;quot;;&lt;br /&gt;      }&lt;br /&gt;      else{&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot; o |&amp;quot;;&lt;br /&gt;      }&lt;br /&gt;      cout &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; &amp;quot;+---+---+---+&amp;quot; &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; &amp;quot;2 | &amp;quot;;&lt;br /&gt;      if(four == 0){&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot;|&amp;quot;;&lt;br /&gt;      }&lt;br /&gt;      else if(four == 1){&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot;x |&amp;quot;;&lt;br /&gt;      }&lt;br /&gt;      else{&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot;o |&amp;quot;;&lt;br /&gt;      }&lt;br /&gt;      if(five == 0){&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot; |&amp;quot;;&lt;br /&gt;      }&lt;br /&gt;      else if(five == 1){&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot; x |&amp;quot;;&lt;br /&gt;      }&lt;br /&gt;      else{&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot; o |&amp;quot;;&lt;br /&gt;      }&lt;br /&gt;      if(six == 0){&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot; |&amp;quot;;&lt;br /&gt;      }&lt;br /&gt;      else if(six == 1){&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot; x |&amp;quot;;&lt;br /&gt;      }&lt;br /&gt;      else{&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot; o |&amp;quot;;&lt;br /&gt;      }&lt;br /&gt;      cout &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; &amp;quot;+---+---+---+&amp;quot; &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; &amp;quot;3 | &amp;quot;;&lt;br /&gt;      if(seven == 0){&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot;|&amp;quot;;&lt;br /&gt;      }&lt;br /&gt;      else if(seven == 1){&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot;x |&amp;quot;;&lt;br /&gt;      }&lt;br /&gt;      else{&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot;o |&amp;quot;;&lt;br /&gt;      }&lt;br /&gt;      if(eight == 0){&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot; |&amp;quot;;&lt;br /&gt;      }&lt;br /&gt;      else if(eight == 1){&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot; x |&amp;quot;;&lt;br /&gt;      }&lt;br /&gt;      else{&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot; o |&amp;quot;;&lt;br /&gt;      }&lt;br /&gt;      if(nine == 0){&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot; |&amp;quot;;&lt;br /&gt;      }&lt;br /&gt;      else if(nine == 1){&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot; x |&amp;quot;;&lt;br /&gt;      }&lt;br /&gt;      else{&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot; o |&amp;quot;;&lt;br /&gt;      }&lt;br /&gt;      cout &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; &amp;quot;+---+---+---+&amp;quot; &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; endl;&lt;br /&gt;      &lt;span style="color:green;"&gt;//The following several lines will check if the game is over or not&lt;/span&gt;&lt;br /&gt;      &lt;span style="color:green;"&gt;//Check`s the first line of the board for a winner combination (xxx or ooo)&lt;/span&gt;&lt;br /&gt;      if(one == two &amp;amp;&amp;amp; two == three &amp;amp;&amp;amp; one != 0){&lt;br /&gt;         &lt;span style="color:green;"&gt;//If the three squares have the same value and they are not free, then the winner is the player with the number from one of those squares&lt;/span&gt;&lt;br /&gt;         winner = one;&lt;br /&gt;      }&lt;br /&gt;      &lt;span style="color:green;"&gt;//Checks the second line&lt;/span&gt;&lt;br /&gt;      if(four == five &amp;amp;&amp;amp; five == six &amp;amp;&amp;amp; four != 0){&lt;br /&gt;         winner = four;&lt;br /&gt;      }&lt;br /&gt;      &lt;span style="color:green;"&gt;//Checks the third line&lt;/span&gt;&lt;br /&gt;      if(seven == eight &amp;amp;&amp;amp; eight == nine &amp;amp;&amp;amp; seven != 0){&lt;br /&gt;         winner = seven;&lt;br /&gt;      }&lt;br /&gt;      &lt;span style="color:green;"&gt;//Checks the first column&lt;/span&gt;&lt;br /&gt;      if(one == four &amp;amp;&amp;amp; four == seven &amp;amp;&amp;amp; one != 0){&lt;br /&gt;         winner = one;&lt;br /&gt;      }&lt;br /&gt;      &lt;span style="color:green;"&gt;//Checks the second column&lt;/span&gt;&lt;br /&gt;      if(two == five &amp;amp;&amp;amp; five == eight &amp;amp;&amp;amp; two != 0){&lt;br /&gt;         winner = two;&lt;br /&gt;      }&lt;br /&gt;      &lt;span style="color:green;"&gt;//Checks the third column&lt;/span&gt;&lt;br /&gt;      if(three == six &amp;amp;&amp;amp; six == nine &amp;amp;&amp;amp; three != 0){&lt;br /&gt;         winner = three;&lt;br /&gt;      }&lt;br /&gt;      &lt;span style="color:green;"&gt;//Checks the diagonal from top left to bottom right&lt;/span&gt;&lt;br /&gt;      if(one == five &amp;amp;&amp;amp; five == nine &amp;amp;&amp;amp; one != 0){&lt;br /&gt;         winner = one;&lt;br /&gt;      }&lt;br /&gt;      &lt;span style="color:green;"&gt;//Checks the diagonal from bottom left to top right&lt;/span&gt;&lt;br /&gt;      if(three == five &amp;amp;&amp;amp; five == seven &amp;amp;&amp;amp; three != 0){&lt;br /&gt;         winner = three;&lt;br /&gt;      }&lt;br /&gt;      &lt;span style="color:green;"&gt;//If nobody wins this turn it checks to see if the board is full or not&lt;/span&gt;&lt;br /&gt;      if(one != 0 &amp;amp;&amp;amp; two != 0 &amp;amp;&amp;amp; three != 0 &amp;amp;&amp;amp; four != 0 &amp;amp;&amp;amp; five != 0 &amp;amp;&amp;amp; six != 0 &amp;amp;&amp;amp; seven != 0 &amp;amp;&amp;amp; eight != 0 &amp;amp;&amp;amp; nine != 0 &amp;amp;&amp;amp; winner == 0){&lt;br /&gt;         &lt;span style="color:green;"&gt;//If the table is full, then it`s tie&lt;/span&gt;&lt;br /&gt;         winner = 3;&lt;br /&gt;      }&lt;br /&gt;      &lt;span style="color:green;"&gt;//If somebody won or it`s a tie shows a message and break out from the loop - the game is over&lt;/span&gt;&lt;br /&gt;      if(winner == 1 || winner == 2){&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot;Congratulations! Player &amp;quot; &amp;lt;&amp;lt; winner &amp;lt;&amp;lt; &amp;quot; is the winner!&amp;quot;;&lt;br /&gt;         break;&lt;br /&gt;      }&lt;br /&gt;      else if(winner == 3){&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot;Tie!&amp;quot;;&lt;br /&gt;         break;&lt;br /&gt;      }&lt;br /&gt;      &lt;span style="color:green;"&gt;If the game is nor over yet than it writes a "Player x`s turn:" - where x is the number of the player (1 or 2)&lt;/span&gt;&lt;br /&gt;      cout &amp;lt;&amp;lt; &amp;quot;Player &amp;quot; &amp;lt;&amp;lt; turn &amp;lt;&amp;lt; &amp;quot;'s turn:&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;      validmove = false;&lt;br /&gt;      &lt;span style="color:green;"&gt;//Loops until the player makes a valid move&lt;/span&gt;&lt;br /&gt;      while(!validmove){&lt;br /&gt;         validrow = false;&lt;br /&gt;         &lt;span style="color:green;"&gt;//Loops until the player chooses a valid row&lt;/span&gt;&lt;br /&gt;         while(!validrow){&lt;br /&gt;            cout &amp;lt;&amp;lt; &amp;quot;Row: &amp;quot;;&lt;br /&gt;            cin &amp;gt;&amp;gt; row;&lt;br /&gt;            if(row == 1 || row == 2 || row == 3){&lt;br /&gt;               validrow = true;&lt;br /&gt;            }&lt;br /&gt;            else{&lt;br /&gt;               cout &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; &amp;quot;Invalid row!&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;            }&lt;br /&gt;         }&lt;br /&gt;         validcolumn = false;&lt;br /&gt;         &lt;span style="color:green;"&gt;//Loops until the player chooses a valid column&lt;/span&gt;&lt;br /&gt;         while(!validcolumn){&lt;br /&gt;            cout &amp;lt;&amp;lt; &amp;quot;Column: &amp;quot;;&lt;br /&gt;            cin &amp;gt;&amp;gt; column;&lt;br /&gt;            if(column == 1 || column == 2 || column == 3){&lt;br /&gt;               validcolumn = true;&lt;br /&gt;            }&lt;br /&gt;            else{&lt;br /&gt;               cout &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; &amp;quot;Invalid column!&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;            }&lt;br /&gt;         }&lt;br /&gt;         &lt;span style="color:green;"&gt;//After the user selects a valid row and column number the changes are applied to the game board and the variable of the selected square if it`s not occupied and it changes the value of the &lt;i&gt;validmove&lt;/i&gt; variable from &lt;i&gt;false&lt;/i&gt; to &lt;i&gt;true&lt;/i&gt;&lt;/span&gt;&lt;br /&gt;         switch(row){&lt;br /&gt;         case 1:&lt;br /&gt;            if(column == 1){&lt;br /&gt;               if(one == 0){&lt;br /&gt;                  one = turn;&lt;br /&gt;                  validmove = true;&lt;br /&gt;               }&lt;br /&gt;            }&lt;br /&gt;            else if(column == 2){&lt;br /&gt;               if(two == 0){&lt;br /&gt;                  two = turn;&lt;br /&gt;                  validmove = true;&lt;br /&gt;               }&lt;br /&gt;            }&lt;br /&gt;            else{&lt;br /&gt;               if(three == 0){&lt;br /&gt;                  three = turn;&lt;br /&gt;                  validmove = true;&lt;br /&gt;               }&lt;br /&gt;            }&lt;br /&gt;            break;&lt;br /&gt;         case 2:&lt;br /&gt;            if(column == 1){&lt;br /&gt;               if(four == 0){&lt;br /&gt;                  four = turn;&lt;br /&gt;                  validmove = true;&lt;br /&gt;               }&lt;br /&gt;            }&lt;br /&gt;            else if(column == 2){&lt;br /&gt;               if(five == 0){&lt;br /&gt;                  five = turn;&lt;br /&gt;                  validmove = true;&lt;br /&gt;               }&lt;br /&gt;            }&lt;br /&gt;            else{&lt;br /&gt;               if(six == 0){&lt;br /&gt;                  six = turn;&lt;br /&gt;                  validmove = true;&lt;br /&gt;               }&lt;br /&gt;            }&lt;br /&gt;            break;&lt;br /&gt;         case 3:&lt;br /&gt;            if(column == 1){&lt;br /&gt;               if(seven == 0){&lt;br /&gt;                  seven = turn;&lt;br /&gt;                  validmove = true;&lt;br /&gt;               }&lt;br /&gt;            }&lt;br /&gt;            else if(column == 2){&lt;br /&gt;               if(eight == 0){&lt;br /&gt;                  eight = turn;&lt;br /&gt;                  validmove = true;&lt;br /&gt;               }&lt;br /&gt;            }&lt;br /&gt;            else{&lt;br /&gt;               if(nine == 0){&lt;br /&gt;                  nine = turn;&lt;br /&gt;                  validmove = true;&lt;br /&gt;               }&lt;br /&gt;            }&lt;br /&gt;         }&lt;br /&gt;         &lt;span style="color:green;"&gt;//If &lt;i&gt;validmove&lt;/i&gt; is still &lt;i&gt;false&lt;/i&gt;, then the selected square is occupied and a message is displayed&lt;/span&gt;&lt;br /&gt;         if(!validmove){&lt;br /&gt;            cout &amp;lt;&amp;lt; &amp;quot;The sellected square is occupied!&amp;quot; &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; &amp;quot;Select again:&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;         }&lt;br /&gt;         else{&lt;br /&gt;            &lt;span style="color:green;"&gt;//Else if it was a valid move, then it`s the other player`s turn&lt;/span&gt;&lt;br /&gt;            if(turn == 1){&lt;br /&gt;               turn = 2;&lt;br /&gt;            }&lt;br /&gt;            else{&lt;br /&gt;               turn = 1;&lt;br /&gt;            }&lt;br /&gt;         }&lt;br /&gt;         cout &amp;lt;&amp;lt; endl;&lt;br /&gt;      }&lt;br /&gt;   &lt;span style="color:green;"&gt;//The loop start`s over&lt;/span&gt;&lt;br /&gt;   }   &lt;br /&gt;   system(&amp;quot;pause&amp;quot;);&lt;br /&gt;}&lt;/div&gt;The second version of this Tic Tac Toe game is ready with minor changes in the code, using a wee bit harder but more effective techniques: &lt;a href="http://www.codingmix.com/2010/10/very-simple-cplusplus-tic-tac-toe-game.html"&gt;Tic Tac Toe v2&lt;/a&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5682776130825686229-2061702966661096784?l=www.codingmix.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/Y7-NkCMPs-EHKL3TlAYe7uy2hNw/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/Y7-NkCMPs-EHKL3TlAYe7uy2hNw/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/Y7-NkCMPs-EHKL3TlAYe7uy2hNw/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/Y7-NkCMPs-EHKL3TlAYe7uy2hNw/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/codingmix/Cplusplus/~4/JAtW6iOuxJQ" height="1" width="1"/&gt;</description><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">3</thr:total><feedburner:origLink>http://www.codingmix.com/2010/07/simples-cplusplus-tic-tac-toe-game-ever_24.html</feedburner:origLink></item><item><title>C++ Lesson 8 - while loop and for loop</title><link>http://feedproxy.google.com/~r/codingmix/Cplusplus/~3/2aKlCjilNfI/cplusplus-lesson-8-while-loop-and-for.html</link><category>Cplusplus lessons</category><category>Cplusplus</category><author>noreply@blogger.com (Csabi)</author><pubDate>Fri, 03 Sep 2010 08:47:18 PDT</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-5682776130825686229.post-3225762893827518628</guid><description>Loops are used to execute a block of code multiple times, so you don`t have to write the same code again and again. Imagine how much time it would take to write a program what writes out the numbers from 1 to 1000 without using loops. In C++ are 3 types of loops:&lt;div id="full-post"&gt;&lt;br /&gt;&lt;b&gt;1. while&lt;/b&gt;&lt;br /&gt;The while loop executes a block of statements while a condition is true. The basic syntax of a while loop is:&lt;div class="code"&gt;while( condition ){&lt;br /&gt;   //code to execute while condition is true&lt;br /&gt;}&lt;/div&gt;Here is an example about how to use it. The following program will write out the numbers from 1 to 100: &lt;div class="code"&gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;&lt;br /&gt;using namespace std;&lt;br /&gt;&lt;br /&gt;int main(){&lt;br /&gt;   int i = 0;&lt;br /&gt;   while(i &amp;lt; 100){&lt;br /&gt;      cout &amp;lt;&amp;lt; ++i &amp;lt;&amp;lt; endl;&lt;br /&gt;   }&lt;br /&gt;   system(&amp;quot;pause&amp;quot;);&lt;br /&gt;}&lt;/div&gt;Before the while loop starts &lt;i&gt;i&lt;/i&gt; is declared with the value 0, but the first number will be 1, because the ++ is in front of &lt;i&gt;i&lt;/i&gt; (&lt;a href="http://www.codingmix.com/2010/06/cplusplus-lesson-4-variables.html"&gt;Lesson 4&lt;/a&gt;)&lt;br /&gt;&lt;br /&gt;&lt;b&gt;2. for&lt;/b&gt;&lt;br /&gt;The for loop is useful when you know how many times you need to repeat a block of code, because the for loop repeats the code for a specific number of times. The basic syntax is:&lt;div class="code"&gt;for(integer = starting value; integer &lt;= ending value; integer++){&lt;br /&gt;   //code to execute&lt;br /&gt;}&lt;/div&gt;We can use any variable name for the integer, but the default is &lt;i&gt;i&lt;/i&gt; or &lt;i&gt;y&lt;/i&gt;.&lt;br /&gt;Here is an example of using the for loop:&lt;br /&gt;Write out the numbers from 1 to 100:&lt;div class="code"&gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;&lt;br /&gt;using namespace std;&lt;br /&gt;&lt;br /&gt;int main(){&lt;br /&gt;   for(int i=1;i&amp;lt;=100;i++){&lt;br /&gt;      cout &amp;lt;&amp;lt; i &amp;lt;&amp;lt; endl;&lt;br /&gt;   }&lt;br /&gt;   system(&amp;quot;pause&amp;quot;);&lt;br /&gt;}&lt;/div&gt;&lt;br /&gt;For the starting and ending value we can use variables, too. For example look at the bellow program which shows all the numbers between two numbers:&lt;div class="code"&gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;&lt;br /&gt;using namespace std;&lt;br /&gt;&lt;br /&gt;int main(){&lt;br /&gt;   cout &amp;lt;&amp;lt; &amp;quot;Enter the starting number: &amp;quot;;&lt;br /&gt;   int startingnum;&lt;br /&gt;   cin &amp;gt;&amp;gt; startingnum;&lt;br /&gt;   cout &amp;lt;&amp;lt; &amp;quot;Enter the ending number: &amp;quot;;&lt;br /&gt;   int endingnum;&lt;br /&gt;   cin &amp;gt;&amp;gt; endingnum;&lt;br /&gt;   for(startingnum;startingnum &amp;lt;= endingnum;startingnum++){&lt;br /&gt;      cout &amp;lt;&amp;lt; startingnum &amp;lt;&amp;lt; endl;&lt;br /&gt;   }&lt;br /&gt;   system(&amp;quot;pause&amp;quot;);&lt;br /&gt;}&lt;/div&gt;&lt;br /&gt;The counting expression can be anything, it can increment or decrement, or any mathematical expression, here are some examples:&lt;div class="code"&gt;for(int i=10;i&gt;0;i--)&lt;br /&gt;or&lt;br /&gt;for(int i=1;i&lt;=101;i+=10)&lt;br /&gt;or&lt;br /&gt;for(int i=1;i&lt;=101;i*=2)&lt;br /&gt;and so on....&lt;/div&gt;&lt;br /&gt;&lt;b&gt;3. do while&lt;/b&gt;&lt;br /&gt;The do while loop it`s similar to the while loop, the only difference is that the do while loop first executes the code and just after checks the condition, so the code inside the do while loop is executed at least once. &lt;br /&gt;&lt;br /&gt;The basic syntax is: &lt;div class="code"&gt;do{&lt;br /&gt;   //code to execute&lt;br /&gt;}while(condition);&lt;/div&gt;Here is and example of using the do while loop:&lt;div class="code"&gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;&lt;br /&gt;using namespace std;&lt;br /&gt;&lt;br /&gt;int main(){&lt;br /&gt;   int i = 0;&lt;br /&gt;   do{&lt;br /&gt;      cout &amp;lt;&amp;lt; ++i &amp;lt;&amp;lt; endl;&lt;br /&gt;   }while(i &amp;lt; 100);&lt;br /&gt;   system(&amp;quot;pause&amp;quot;);&lt;br /&gt;}&lt;/div&gt;The above program shows the numbers from 1 to 100. &lt;br /&gt;Next lesson: &lt;a href="http://www.codingmix.com/2010/09/cplusplus-lesson-9-arrays.html"&gt;C++ Lesson 9 - Arrays&lt;/a&gt;&lt;br /&gt;Previous lesson: &lt;a href="http://www.codingmix.com/2010/06/cplusplus-lesson-7-switchcase-statement.html"&gt;C++ Lesson 7 - switch/case statement&lt;/a&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5682776130825686229-3225762893827518628?l=www.codingmix.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/QOw2XjxxIRpodxDWXfHlian3LOA/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/QOw2XjxxIRpodxDWXfHlian3LOA/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/QOw2XjxxIRpodxDWXfHlian3LOA/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/QOw2XjxxIRpodxDWXfHlian3LOA/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/codingmix/Cplusplus/~4/2aKlCjilNfI" height="1" width="1"/&gt;</description><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">4</thr:total><feedburner:origLink>http://www.codingmix.com/2010/07/cplusplus-lesson-8-while-loop-and-for.html</feedburner:origLink></item><item><title>C++ Console Calculator v2</title><link>http://feedproxy.google.com/~r/codingmix/Cplusplus/~3/BYJtJ-cY0wc/cplusplus-console-calculator-v2.html</link><category>Cplusplus projects</category><category>Cplusplus</category><author>noreply@blogger.com (Csabi)</author><pubDate>Sat, 02 Oct 2010 03:50:06 PDT</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-5682776130825686229.post-8527173715013262902</guid><description>This is a tutorial about creating the second version of my C++ Calculator (console application). Here is the first version: &lt;a href="http://www.codingmix.com/2010/06/cplusplus-console-calculator-v1.html"&gt;Calculator v1&lt;/a&gt;. The only difference between the first and second versions is that this one uses switch/case statement in some places instead of multiple if else statements.&lt;div id="full-post"&gt;&lt;br /&gt;Required knowledge: &lt;a href="http://www.codingmix.com/2010/06/cplusplus-lesson-4-variables.html"&gt;variables&lt;/a&gt;, &lt;a href="http://www.codingmix.com/2010/06/cplusplus-lesson-5-inputoutput.html"&gt;input/output&lt;/a&gt;, &lt;a href="http://www.codingmix.com/2010/06/cplusplus-lesson-6-ifelseelse-if_18.html"&gt;if/else/else if statements&lt;/a&gt;, &lt;a href="http://www.codingmix.com/2010/06/cplusplus-lesson-7-switchcase-statement.html"&gt;switch/case statements&lt;/a&gt;.&lt;br /&gt;Here is my code: &lt;div class="code"&gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;&lt;br /&gt;using namespace std;&lt;br /&gt;&lt;br /&gt;int main(){&lt;br /&gt;   cout &amp;lt;&amp;lt; &amp;quot;Enter the first number:&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;   int numone;&lt;br /&gt;   cin &amp;gt;&amp;gt; numone;&lt;br /&gt;   cout &amp;lt;&amp;lt; &amp;quot;Select an operation:&amp;quot; &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; &amp;quot;1. *&amp;quot; &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; &amp;quot;2. /&amp;quot; &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; &amp;quot;3. +&amp;quot; &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; &amp;quot;4. -&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;   int operation;&lt;br /&gt;   cin &amp;gt;&amp;gt; operation;&lt;br /&gt;   if(operation == 1 || operation == 2 || operation == 3 || operation == 4){&lt;br /&gt;      cout &amp;lt;&amp;lt; &amp;quot;Enter the second number:&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;      int numtwo;&lt;br /&gt;      cin &amp;gt;&amp;gt; numtwo;&lt;br /&gt;      float result;&lt;br /&gt;      bool dividebyzero = false;&lt;br /&gt;      switch(operation){&lt;br /&gt;      case 1:&lt;br /&gt;         result = numone * numtwo;&lt;br /&gt;         break;&lt;br /&gt;      case 2:&lt;br /&gt;         if(numtwo == 0){&lt;br /&gt;            dividebyzero = true;&lt;br /&gt;         }&lt;br /&gt;         else{&lt;br /&gt;            result = (float) numone / numtwo;&lt;br /&gt;         }&lt;br /&gt;         break;&lt;br /&gt;      case 3:&lt;br /&gt;         result = numone + numtwo;&lt;br /&gt;         break;&lt;br /&gt;      case 4:&lt;br /&gt;         result = numone - numtwo;&lt;br /&gt;         break;&lt;br /&gt;      }&lt;br /&gt;      if(!dividebyzero){&lt;br /&gt;         cout &amp;lt;&amp;lt; numone;&lt;br /&gt;         switch(operation){&lt;br /&gt;         case 1:&lt;br /&gt;            cout &amp;lt;&amp;lt; &amp;quot; * &amp;quot;;&lt;br /&gt;            break;&lt;br /&gt;         case 2:&lt;br /&gt;            cout &amp;lt;&amp;lt; &amp;quot; / &amp;quot;;&lt;br /&gt;            break;&lt;br /&gt;         case 3:&lt;br /&gt;            cout &amp;lt;&amp;lt; &amp;quot; + &amp;quot;;&lt;br /&gt;            break;&lt;br /&gt;         case 4:&lt;br /&gt;            cout &amp;lt;&amp;lt; &amp;quot; - &amp;quot;;&lt;br /&gt;         }&lt;br /&gt;         cout &amp;lt;&amp;lt; numtwo &amp;lt;&amp;lt; &amp;quot; = &amp;quot; &amp;lt;&amp;lt; result &amp;lt;&amp;lt; endl;&lt;br /&gt;      }&lt;br /&gt;      else{&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot;Cannot divide by zero!&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;      }&lt;br /&gt;   }&lt;br /&gt;   else{&lt;br /&gt;      cout &amp;lt;&amp;lt; &amp;quot;Invalid selection!&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;   }&lt;br /&gt;   system(&amp;quot;pause&amp;quot;);&lt;br /&gt;}&lt;/div&gt;After the program reads the first number it check if the selection is valid. If not it will display and error message. If will ask for a second number and based on the operation value (using switch/case statement) it will make the mathematical operation. If the selected operation is 2 (division), the program will check the value of the second number. If the second number is 0 it will set the dividebezero boolean to true. If dividebyzero is true at the end, the program will show and error message instead of the result.&lt;br /&gt;&lt;br /&gt;This was the second version,here is the third one: &lt;a href="http://www.codingmix.com/2010/09/cplusplus-console-calculator-v3.html"&gt;Calculator v3&lt;/a&gt;. Here is a link to the first version: &lt;a href="http://www.codingmix.com/2010/06/cplusplus-console-calculator-v1.html"&gt;Calculator v1&lt;/a&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5682776130825686229-8527173715013262902?l=www.codingmix.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/7rfxw5V4WzkkhuK9fT6DheGGQ7I/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/7rfxw5V4WzkkhuK9fT6DheGGQ7I/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/7rfxw5V4WzkkhuK9fT6DheGGQ7I/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/7rfxw5V4WzkkhuK9fT6DheGGQ7I/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/codingmix/Cplusplus/~4/BYJtJ-cY0wc" height="1" width="1"/&gt;</description><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total><feedburner:origLink>http://www.codingmix.com/2010/06/cplusplus-console-calculator-v2.html</feedburner:origLink></item><item><title>C++ Lesson 7 - switch/case statement</title><link>http://feedproxy.google.com/~r/codingmix/Cplusplus/~3/Dbu0gE-abm4/cplusplus-lesson-7-switchcase-statement.html</link><category>Cplusplus lessons</category><category>Cplusplus</category><author>noreply@blogger.com (Csabi)</author><pubDate>Wed, 01 Sep 2010 07:07:14 PDT</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-5682776130825686229.post-4551401238672530239</guid><description>The switch/case statement usually it`s used to replace multiple if/else statements.&lt;br /&gt;The general syntax is:&lt;div id="full-post"&gt;&lt;div class="code"&gt;switch (variable){&lt;br /&gt;   case possible-value: &lt;br /&gt;      //code to execute&lt;br /&gt;      break;&lt;br /&gt;   default:&lt;br /&gt;      //code to execute&lt;br /&gt;}&lt;/div&gt;Now, let`s see it working: Let`s say that we want to write a program that will ask for a number between 1 and 5, and it will write out the number in text format (1 = one, 2 = two, 3 = three ....).&lt;br /&gt;Using the if/else if statements it will look like in the bellow example:&lt;div class="code"&gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;&lt;br /&gt;using namespace std;&lt;br /&gt;&lt;br /&gt;int main(){&lt;br /&gt;   cout &amp;lt;&amp;lt; &amp;quot;Enter a number between 1 and 5!&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;   int number;&lt;br /&gt;   cin &amp;gt;&amp;gt; number;&lt;br /&gt;   if(number == 1){&lt;br /&gt;      cout &amp;lt;&amp;lt; &amp;quot;one&amp;quot;;&lt;br /&gt;   }&lt;br /&gt;   else if(number == 2){&lt;br /&gt;      cout &amp;lt;&amp;lt; &amp;quot;two&amp;quot;;&lt;br /&gt;   }&lt;br /&gt;   else if(number == 3){&lt;br /&gt;      cout &amp;lt;&amp;lt; &amp;quot;three&amp;quot;;&lt;br /&gt;   }&lt;br /&gt;   else if(number == 4){&lt;br /&gt;      cout &amp;lt;&amp;lt; &amp;quot;four&amp;quot;;&lt;br /&gt;   }&lt;br /&gt;   else if(number == 5){&lt;br /&gt;      cout &amp;lt;&amp;lt; &amp;quot;five&amp;quot;;&lt;br /&gt;   }&lt;br /&gt;   else{&lt;br /&gt;      cout &amp;lt;&amp;lt; number &amp;lt;&amp;lt; &amp;quot; is not between 1 and 5!&amp;quot;;&lt;br /&gt;   }&lt;br /&gt;   cout &amp;lt;&amp;lt; endl;&lt;br /&gt;   system(&amp;quot;pause&amp;quot;);&lt;br /&gt;}&lt;/div&gt;And using the switch/case statement:&lt;div class="code"&gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;&lt;br /&gt;using namespace std;&lt;br /&gt;&lt;br /&gt;int main(){&lt;br /&gt;   cout &amp;lt;&amp;lt; &amp;quot;Enter a number between 1 and 5!&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;   int number;&lt;br /&gt;   cin &amp;gt;&amp;gt; number;&lt;br /&gt;   switch(number){&lt;br /&gt;   case 1: //if (number == 1)&lt;br /&gt;      cout &amp;lt;&amp;lt; &amp;quot;one&amp;quot;;&lt;br /&gt;      break;&lt;br /&gt;   case 2: //else if(number == 2) and so on... &lt;br /&gt;      cout &amp;lt;&amp;lt; &amp;quot;two&amp;quot;;&lt;br /&gt;      break;&lt;br /&gt;   case 3: &lt;br /&gt;      cout &amp;lt;&amp;lt; &amp;quot;three&amp;quot;;&lt;br /&gt;      break;&lt;br /&gt;   case 4: &lt;br /&gt;      cout &amp;lt;&amp;lt; &amp;quot;four&amp;quot;;&lt;br /&gt;      break;&lt;br /&gt;   case 5: &lt;br /&gt;      cout &amp;lt;&amp;lt; &amp;quot;five&amp;quot;;&lt;br /&gt;      break;&lt;br /&gt;   default: //if number is NOT 1,2,3,4 or 5&lt;br /&gt;      cout &amp;lt;&amp;lt; number &amp;lt;&amp;lt; &amp;quot; is not between 1 and 5!&amp;quot;;&lt;br /&gt;   }&lt;br /&gt;   cout &amp;lt;&amp;lt; endl;&lt;br /&gt;   system(&amp;quot;pause&amp;quot;);&lt;br /&gt;}&lt;/div&gt;&lt;br /&gt;&lt;b&gt;break;&lt;/b&gt;&lt;br /&gt;The break is used to break out from the switch/case statement. Without break the program will execute all the code after the selected one. For example, if we don`t use breaks in the above code, and the user inputs 3, the program will show:&lt;div class="code"&gt;threefourfive3 is not between 1 and 5!&lt;/div&gt;instead of &lt;div class="code"&gt;three&lt;/div&gt;Write the above code in your compiler and try it out to see it with your own eyes.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;default:&lt;/b&gt;&lt;br /&gt;The default case is optional, this is executed when none of the previous cases are executed or when you forget the break;.&lt;br /&gt;&lt;br /&gt;Note: The cases (including the default case) are followed by a colon, not semicolon!&lt;br /&gt;Note: &lt;b&gt;This is very important!&lt;/b&gt; A variable can`t be used as possible value of a case statement! Example of how NOT to do: &lt;div class="code"&gt;int myvariable = 1;&lt;br /&gt;switch (x){&lt;br /&gt;case myvariable: //this is NOT valid&lt;br /&gt;   //code to execute&lt;br /&gt;}&lt;/div&gt;&lt;br /&gt;&lt;b&gt;The same result for multiple values&lt;/b&gt;&lt;br /&gt;If you want to execute the same code in more cases, you don`t have to write that code multiple times, just write it like in the bellow example:&lt;div class="code"&gt;switch (myvariable){&lt;br /&gt;case 1:&lt;br /&gt;case 2:&lt;br /&gt;case 3:&lt;br /&gt;   cout &amp;lt;&amp;lt; &amp;quot;myvariable is 1,2 or 3&amp;quot;;&lt;br /&gt;}&lt;/div&gt;&lt;br /&gt;Recommended project: Write a calculator program, first ask for a number, then let the user to select from a list of mathematic operations (*,/,+,-), than ask for a second number and than write out the result using switch/case statement when possible. Pay attention to every possibility, you don`t want to have a program that crashes all the time.&lt;br /&gt;&lt;br /&gt;Here is a link to a tutorial about how to make it, but before reading it try to solve it! Coming soon.&lt;br /&gt;&lt;br /&gt;Next lesson: &lt;a href="http://www.codingmix.com/2010/07/cplusplus-lesson-8-while-loop-and-for.html"&gt;C++ Lesson 8 - while loop and for loop&lt;/a&gt;&lt;br /&gt;Previous lesson: &lt;a href="http://www.codingmix.com/2010/06/cplusplus-lesson-6-ifelseelse-if_18.html"&gt;C++ Lesson 6 - if/else/else if statements&lt;/a&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5682776130825686229-4551401238672530239?l=www.codingmix.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/ttZCv6Y5geN4S4e_5ZHIGaTP63M/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/ttZCv6Y5geN4S4e_5ZHIGaTP63M/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/ttZCv6Y5geN4S4e_5ZHIGaTP63M/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/ttZCv6Y5geN4S4e_5ZHIGaTP63M/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/codingmix/Cplusplus/~4/Dbu0gE-abm4" height="1" width="1"/&gt;</description><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">6</thr:total><feedburner:origLink>http://www.codingmix.com/2010/06/cplusplus-lesson-7-switchcase-statement.html</feedburner:origLink></item><item><title>C++ Console Calculator v1</title><link>http://feedproxy.google.com/~r/codingmix/Cplusplus/~3/9Ts5nCTeLwE/cplusplus-console-calculator-v1.html</link><category>Cplusplus projects</category><category>Cplusplus</category><author>noreply@blogger.com (Csabi)</author><pubDate>Thu, 02 Sep 2010 11:02:07 PDT</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-5682776130825686229.post-1419408886631161130</guid><description>This is a tutorial about how to make a C++ console Calculator (version 1) which first asks for a number, than shows a list of mathematical operations, than asks for an another number, than writes out the result. Here is an image of how it will look:&lt;div id="full-post"&gt;&lt;a href="http://3.bp.blogspot.com/_Ok7mVUxPcsk/TByRI5zpt_I/AAAAAAAAAPs/u8ndtCz_Kio/s1600/Calculator+v1.jpg"&gt;&lt;img style="display:block; margin:5px auto 5px; text-align:center;width: 545px; height: 275px;" src="http://3.bp.blogspot.com/_Ok7mVUxPcsk/TByRI5zpt_I/AAAAAAAAAPs/u8ndtCz_Kio/s1600/Calculator+v1.jpg" border="0" alt=""id="BLOGGER_PHOTO_ID_5484418028372867058" /&gt;&lt;/a&gt;Required knowledge: &lt;a href="http://www.codingmix.com/2010/06/cplusplus-lesson-4-variables.html"&gt;variables&lt;/a&gt;, &lt;a href="http://www.codingmix.com/2010/06/cplusplus-lesson-5-inputoutput.html"&gt;input/output&lt;/a&gt;, &lt;a href="http://www.codingmix.com/2010/06/cplusplus-lesson-6-ifelseelse-if_18.html"&gt;if/else/else if statements&lt;/a&gt;.&lt;br /&gt;Here is my code:&lt;div class="code"&gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;&lt;br /&gt;using namespace std;&lt;br /&gt;&lt;br /&gt;int main(){&lt;br /&gt;   cout &amp;lt;&amp;lt; &amp;quot;Enter the first number:&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;   int numone;&lt;br /&gt;   cin &amp;gt;&amp;gt; numone;&lt;br /&gt;   cout &amp;lt;&amp;lt; &amp;quot;Select an operation:&amp;quot; &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; &amp;quot;1. *&amp;quot; &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; &amp;quot;2. /&amp;quot; &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; &amp;quot;3. +&amp;quot; &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; &amp;quot;4. -&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;   int operation;&lt;br /&gt;   cin &amp;gt;&amp;gt; operation;&lt;br /&gt;   if(operation == 1 || operation == 2 || operation == 3 || operation == 4){&lt;br /&gt;      cout &amp;lt;&amp;lt; &amp;quot;Enter the second number:&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;      int numtwo;&lt;br /&gt;      cin &amp;gt;&amp;gt; numtwo;&lt;br /&gt;      float result;&lt;br /&gt;      bool dividebyzero = false;&lt;br /&gt;      if(operation == 1){&lt;br /&gt;         result = numone * numtwo;&lt;br /&gt;      }&lt;br /&gt;      else if(operation == 2){&lt;br /&gt;         if(numtwo != 0){&lt;br /&gt;            result = (float) numone / numtwo;&lt;br /&gt;         }&lt;br /&gt;         else{&lt;br /&gt;            dividebyzero = true;&lt;br /&gt;         }&lt;br /&gt;      }&lt;br /&gt;      else if(operation == 3){&lt;br /&gt;         result = numone + numtwo;&lt;br /&gt;      }&lt;br /&gt;      else{&lt;br /&gt;         result = numone - numtwo;&lt;br /&gt;      }&lt;br /&gt;      if(!dividebyzero){&lt;br /&gt;         cout &amp;lt;&amp;lt; numone;&lt;br /&gt;         if(operation == 1){&lt;br /&gt;            cout &amp;lt;&amp;lt; &amp;quot; * &amp;quot;;&lt;br /&gt;         }&lt;br /&gt;         else if(operation == 2){&lt;br /&gt;            cout &amp;lt;&amp;lt; &amp;quot; / &amp;quot;;&lt;br /&gt;         }&lt;br /&gt;         else if(operation == 3){&lt;br /&gt;            cout &amp;lt;&amp;lt; &amp;quot; + &amp;quot;;&lt;br /&gt;         }&lt;br /&gt;         else{&lt;br /&gt;            cout &amp;lt;&amp;lt; &amp;quot; - &amp;quot;;&lt;br /&gt;         }&lt;br /&gt;         cout &amp;lt;&amp;lt; numtwo &amp;lt;&amp;lt; &amp;quot; = &amp;quot; &amp;lt;&amp;lt; result &amp;lt;&amp;lt; endl;&lt;br /&gt;      }&lt;br /&gt;      else{&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot;Cannot divide by zero!&amp;quot;;&lt;br /&gt;      }&lt;br /&gt;   }&lt;br /&gt;   else{&lt;br /&gt;      cout &amp;lt;&amp;lt; &amp;quot;Invalid selection!&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;   }&lt;br /&gt;   system(&amp;quot;pause&amp;quot;);&lt;br /&gt;}&lt;/div&gt;After the program reads the first number and the operation number it checks if the operator value is valid (1,2,3 or 4), if it`s an invalid number selection (else than 1,2,3,4) it will display an error message. If the operation number is valid then the program asks for the second number, and calculates the result based on the operation. If the selected operation is 2 (division) than it checks the second numbers value. If the second number is 0 than it sets the dividebyzero boolean to true. Finally the program checks for the value of dividebyzero. If it`s true than it shows and error message, else it writes the first number, than the operation (based on the operation number) , than the second number , than an equal sign (=) and the result.&lt;br /&gt;&lt;br /&gt;This is a very simple program, it uses only the most basic statements of C++. The second version is ready, check it out: &lt;a href="http://www.codingmix.com/2010/06/cplusplus-console-calculator-v2.html"&gt;Calculator v2&lt;/a&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5682776130825686229-1419408886631161130?l=www.codingmix.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/YgM_HOCQ5UQKgyIAeev5ol27Mzc/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/YgM_HOCQ5UQKgyIAeev5ol27Mzc/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/YgM_HOCQ5UQKgyIAeev5ol27Mzc/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/YgM_HOCQ5UQKgyIAeev5ol27Mzc/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/codingmix/Cplusplus/~4/9Ts5nCTeLwE" height="1" width="1"/&gt;</description><media:thumbnail url="http://3.bp.blogspot.com/_Ok7mVUxPcsk/TByRI5zpt_I/AAAAAAAAAPs/u8ndtCz_Kio/s72-c/Calculator+v1.jpg" height="72" width="72" /><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">2</thr:total><feedburner:origLink>http://www.codingmix.com/2010/06/cplusplus-console-calculator-v1.html</feedburner:origLink></item><item><title>C++ Lesson 6 - if/else/else if statements</title><link>http://feedproxy.google.com/~r/codingmix/Cplusplus/~3/n-XghPRr_tk/cplusplus-lesson-6-ifelseelse-if_18.html</link><category>Cplusplus lessons</category><category>Cplusplus</category><author>noreply@blogger.com (Csabi)</author><pubDate>Wed, 01 Sep 2010 07:08:57 PDT</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-5682776130825686229.post-3820134802452056242</guid><description>The &lt;b&gt;if, else&lt;/b&gt; and &lt;b&gt;else if&lt;/b&gt; statements are used to decide what code to execute based on different conditions. The program makes decisions based on whether a given condition is true or false. Condition can be a boolean or the comparison of two values. The C++ comparison operators are:&lt;div id="full-post"&gt;&lt;table style="width:545px;"&gt;&lt;tr style="background:#33bb33; color:white;"&gt;&lt;td&gt;Operator&lt;/td&gt;&lt;td&gt;Description&lt;/td&gt;&lt;td&gt;Example (&lt;b&gt;x = 2&lt;/b&gt;)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;==&lt;/td&gt;&lt;td&gt;Equal to&lt;/td&gt;&lt;td&gt;x == 3 is false&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;!=&lt;/td&gt;&lt;td&gt;Not equal to&lt;/td&gt;&lt;td&gt;x != 3 is true&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&amp;gt;&lt;/td&gt;&lt;td&gt;Greater than&lt;/td&gt;&lt;td&gt;x &amp;gt; 2 is false&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&amp;lt;&lt;/td&gt;&lt;td&gt;Less than&lt;/td&gt;&lt;td&gt;x &amp;lt; 2 is true&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&amp;gt;=&lt;/td&gt;&lt;td&gt;Greater than or equal to&lt;/td&gt;&lt;td&gt;x &amp;gt;= 2 is true&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&amp;lt;=&lt;/td&gt;&lt;td&gt;Less than or equal to&lt;/td&gt;&lt;td&gt;x &amp;lt;= 2 is true&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;br /&gt;&lt;b&gt;if&lt;/b&gt;&lt;br /&gt;General syntax: &lt;div class="code"&gt;if( condition ) {&lt;br /&gt;   //code to execute if condition is true&lt;br /&gt;}&lt;/div&gt;Here is an example: &lt;div class="code"&gt;int x=5;&lt;br /&gt;if(x == 5){&lt;br /&gt;   cout &amp;lt;&amp;lt; "x is equal to 5"; //this message will be shown only if x = 5&lt;br /&gt;}&lt;/div&gt;&lt;br /&gt;&lt;b&gt;else&lt;/b&gt;&lt;br /&gt;General syntax: &lt;div class="code"&gt;if( condition ) {&lt;br /&gt;   //code to execute if condition is true&lt;br /&gt;}&lt;br /&gt;else{&lt;br /&gt;   //code to execute if condition is false&lt;br /&gt;}&lt;/div&gt;Here is an example:&lt;div class="code"&gt;int x;&lt;br /&gt;cin &amp;gt;&amp;gt; x;&lt;br /&gt;if(x &lt; 5){&lt;br /&gt;   cout &amp;lt;&amp;lt; "x is less than 5";&lt;br /&gt;}&lt;br /&gt;else{&lt;br /&gt;   cout &amp;lt;&amp;lt; "x is greater or equal to 5";&lt;br /&gt;}&lt;/div&gt;&lt;br /&gt;&lt;b&gt;else if&lt;/b&gt;&lt;br /&gt;This is used when we want to check more then two possibilities. General syntax:&lt;div class="code"&gt;if( condition1 ){&lt;br /&gt;   //code to execute if condition1 is true&lt;br /&gt;}&lt;br /&gt;else if( condition2 ){&lt;br /&gt;   //code to execute if condition1 is false and condition2 is true&lt;br /&gt;}&lt;/div&gt;Here is an example:&lt;div class="code"&gt;int x;&lt;br /&gt;cin &amp;gt;&amp;gt; x;&lt;br /&gt;if(x &lt; 5){&lt;br /&gt;   cout &amp;lt;&amp;lt; "x is less than 5";&lt;br /&gt;}&lt;br /&gt;else if(x &gt; 5){&lt;br /&gt;   cout &amp;lt;&amp;lt; "x is greater than 5";&lt;br /&gt;}&lt;br /&gt;else{&lt;br /&gt;   cout &amp;lt;&amp;lt; "x is equal to 5";&lt;br /&gt;}&lt;/div&gt;&lt;br /&gt;&lt;b&gt;Using a boolean&lt;/b&gt;&lt;br /&gt;Here is an example: &lt;div class="code"&gt;bool x = true;&lt;br /&gt;if(x){&lt;br /&gt;   cout &amp;lt;&amp;lt; "x is true";&lt;br /&gt;}&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;b&gt;Using logical operators&lt;/b&gt;&lt;br /&gt;The logical operators in C++ are:&lt;div class="code"&gt;!        inverse&lt;br /&gt;&amp;&amp;    and&lt;br /&gt;||        or&lt;/div&gt;&lt;br /&gt;!&lt;br /&gt;The ! logical operator inverts the value of a boolean operation. Example:&lt;div class="code"&gt;bool x = false;&lt;br /&gt;if(!x){ //this checks if the inverted value of x is true or not&lt;br /&gt;   cout &amp;lt;&amp;lt; "x is false";  //!x is true and x is false&lt;br /&gt;}&lt;/div&gt;&lt;br /&gt;&amp;&amp;&lt;br /&gt;This logical operator is used when we want to execute a code only if more than one conditions are true. Example:&lt;div class="code"&gt;bool x = true;&lt;br /&gt;bool y = true;&lt;br /&gt;if(x &amp;&amp; y){&lt;br /&gt;   cout &amp;lt;&amp;lt; "both of them are true";&lt;br /&gt;}&lt;br /&gt;y = false;&lt;br /&gt;if(x &amp;&amp; y){&lt;br /&gt;   //this code &lt;b&gt;won`t&lt;/b&gt; be executed because not all the condition are true&lt;br /&gt;}&lt;/div&gt;&lt;br /&gt;||&lt;br /&gt;This logical operator is used when we want to execute a code when at least one of the conditions is true. Examples:&lt;div class="code"&gt;bool x = true;&lt;br /&gt;bool y = true;&lt;br /&gt;if(x || y){&lt;br /&gt;   //code will be executed because all the conditions are true&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;x = false;&lt;br /&gt;if(x || y){&lt;br /&gt;   //code will be executed because at least one of the conditions are true&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;y = false;&lt;br /&gt;if(x || y){&lt;br /&gt;   //code won`t be executed because none of the conditions are true.&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;Note: You can use as many times you want the &amp;&amp; and || operators.&lt;/div&gt;&lt;br /&gt;The best way to learn programming is by practicing!&lt;br /&gt;Recommended project: Write a calculator program, first ask for a number, then  let the user to select from a list of mathematic operations (*,/,+,-), than ask for a second number and than write out the result. Pay attention to every possibility, you don`t want to have a program that crashes all the time.&lt;br /&gt;&lt;br /&gt;If you fail at first don`t give up try it again. If you can`t solve this after several tries here you can find my tutorial about how to make it: &lt;a href="http://www.codingmix.com/2010/06/cplusplus-console-calculator-v1.html"&gt;Calculator v1&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;Next lesson: &lt;a href="http://www.codingmix.com/2010/06/cplusplus-lesson-7-switchcase-statement.html"&gt;C++ Lesson 7 - switch/case statement&lt;/a&gt;&lt;br /&gt;Previous lesson: &lt;a href="http://www.codingmix.com/2010/06/cplusplus-lesson-5-inputoutput.html"&gt;C++ Lesson 5 - Input/Output&lt;/a&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5682776130825686229-3820134802452056242?l=www.codingmix.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/yFUh8PX6m0I1kDnEpn7e6EZJLMk/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/yFUh8PX6m0I1kDnEpn7e6EZJLMk/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/yFUh8PX6m0I1kDnEpn7e6EZJLMk/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/yFUh8PX6m0I1kDnEpn7e6EZJLMk/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/codingmix/Cplusplus/~4/n-XghPRr_tk" height="1" width="1"/&gt;</description><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total><feedburner:origLink>http://www.codingmix.com/2010/06/cplusplus-lesson-6-ifelseelse-if_18.html</feedburner:origLink></item><item><title>C++ Lesson 5 - Input/Output</title><link>http://feedproxy.google.com/~r/codingmix/Cplusplus/~3/myZUyzDMaO8/cplusplus-lesson-5-inputoutput.html</link><category>Cplusplus lessons</category><category>Cplusplus</category><author>noreply@blogger.com (Csabi)</author><pubDate>Wed, 01 Sep 2010 07:09:33 PDT</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-5682776130825686229.post-4094648943764639542</guid><description>Think, how useful is a program that gives the same result at all or a program that can`t even show the result. Well, I wouldn't pay for a such program. This is the reson why in this lesson I will write more about writing out syntax and about how to create a program that can interact with the user.&lt;br /&gt;&lt;div id="full-post"&gt;&lt;br /&gt;To output something we will use &lt;b&gt;cout&lt;/b&gt;, to input something we will use &lt;b&gt;cin&lt;/b&gt;. These are predefined stream objects. These are declared in the &amp;lt;iostream&amp;gt; library (header file), so in order to use cout or cin we need to include it. iostream stands for &lt;b&gt;I&lt;/b&gt;nput/&lt;b&gt;O&lt;/b&gt;utput &lt;b&gt;Stream&lt;/b&gt;. A stream is a sequence of data of undetermined length (for example: when an user inputs something the computer don`t know what he will type in).&lt;br /&gt;&lt;br /&gt;&lt;b&gt;cout&lt;/b&gt;&lt;br /&gt;cout is used to write out something. The name stands from &lt;b&gt;c&lt;/b&gt;++ &lt;b&gt;out&lt;/b&gt;. This needs to be used with the &lt;&lt; operator. The &lt;&lt; operator inserts the data flow into the stream. You can use multiple times the &lt;&lt; operator:&lt;div class="code"&gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;&lt;br /&gt;using namespace std;&lt;br /&gt;&lt;br /&gt;int main(){&lt;br /&gt;   int myvariable = 15;&lt;br /&gt;   cout &amp;lt;&amp;lt; &amp;quot;I`m &amp;quot; &amp;lt;&amp;lt; myvariable &amp;lt;&amp;lt; &amp;quot; years old&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;   system(&amp;quot;pause&amp;quot;);&lt;br /&gt;}&lt;/div&gt;The above program will output something like this:&lt;div class="code"&gt;I`m 15 years old&lt;br /&gt;Press any key to continue ...&lt;/div&gt;&lt;br /&gt;Note: The semicolon (;) need to be placed only once at the end of the line.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;cin&lt;/b&gt;&lt;br /&gt;cin is used to read what the user inputs. The names stands for &lt;b&gt;c&lt;/b&gt;++ &lt;b&gt;in&lt;/b&gt;. This needs to be used with the &gt;&gt; operator. The &gt;&gt; operator reads the data from the stream (stream loads the data inputed from the keyboard). The &gt;&gt; operator can be used multiple times too, but usually we will use a single &gt;&gt; operator because the program is more useful if the user know what he needs to input. Here is an example:&lt;div class="code"&gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;&lt;br /&gt;using namespace std;&lt;br /&gt;&lt;br /&gt;int main(){&lt;br /&gt;   int age;&lt;br /&gt;   cout &amp;lt;&amp;lt; &amp;quot;What`s your age ?&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;   cin &amp;gt;&amp;gt; age;&lt;br /&gt;   cout &amp;lt;&amp;lt; &amp;quot;You are &amp;quot; &amp;lt;&amp;lt; age &amp;lt;&amp;lt; &amp;quot; years old.&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;   system(&amp;quot;pause&amp;quot;);&lt;br /&gt;}&lt;/div&gt;The above program will output something like this (depending on what you will input):&lt;div class="code"&gt;What`s your age ?&lt;br /&gt;15&lt;br /&gt;You are 15 years old.&lt;br /&gt;Press any key to continue ...&lt;/div&gt;&lt;br /&gt;Next lesson: &lt;a href="http://www.codingmix.com/2010/06/cplusplus-lesson-6-ifelseelse-if_18.html"&gt;C++ Lesson 6 - if/else/else if statements&lt;/a&gt;&lt;br /&gt;Previous lesson: &lt;a href="http://www.codingmix.com/2010/06/cplusplus-lesson-4-variables.html"&gt;C++ Lesson 4 - Variables&lt;/a&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5682776130825686229-4094648943764639542?l=www.codingmix.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/xVVCXYZ-jm19nQHS9b_Zr4ggLMg/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/xVVCXYZ-jm19nQHS9b_Zr4ggLMg/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/xVVCXYZ-jm19nQHS9b_Zr4ggLMg/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/xVVCXYZ-jm19nQHS9b_Zr4ggLMg/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/codingmix/Cplusplus/~4/myZUyzDMaO8" height="1" width="1"/&gt;</description><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total><feedburner:origLink>http://www.codingmix.com/2010/06/cplusplus-lesson-5-inputoutput.html</feedburner:origLink></item><item><title>C++ Lesson 4 - Variables</title><link>http://feedproxy.google.com/~r/codingmix/Cplusplus/~3/Tc6KAkPhqYM/cplusplus-lesson-4-variables.html</link><category>Cplusplus lessons</category><category>Cplusplus</category><author>noreply@blogger.com (Csabi)</author><pubDate>Wed, 01 Sep 2010 07:10:01 PDT</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-5682776130825686229.post-2798748566903162870</guid><description>What is a variable ? A variable is a facility to store data. We can store values in variables and we can change it`s value. In C++ we can use multiple variable types, but the following ones are the most used:&lt;div class="code"&gt;&lt;b&gt;int&lt;/b&gt; - An integer number&lt;br /&gt;&lt;b&gt;bool&lt;/b&gt; - Boolean, it can be only True or False&lt;br /&gt;&lt;b&gt;float&lt;/b&gt; - Floating point number&lt;br /&gt;&lt;b&gt;double&lt;/b&gt; - Double precision floating point number&lt;br /&gt;&lt;b&gt;char&lt;/b&gt; - A single character&lt;br /&gt;&lt;b&gt;string&lt;/b&gt; - String of text&lt;/div&gt;&lt;div id="full-post"&gt;Before using a variable, it needs to be initialized. The syntax is:&lt;div class="code"&gt;type identifier;&lt;br /&gt;&lt;br /&gt;or&lt;br /&gt;&lt;br /&gt;type identifier = value;&lt;/div&gt;In this lesson we will talk only about integer, these are the easiest to work with. Here is an example of initializing an integer without and with an initial value. I will call the variable &lt;b&gt;myvariable&lt;/b&gt;:&lt;div class="code"&gt;int myvariable; //without initial value&lt;br /&gt;&lt;br /&gt;int myvariable = 5; //the initial value is 5&lt;/div&gt;As I said the value of the variables can be changed using operators. The basic operators in C++ are: + , - , * , / , % , = , += , -= , *= , /= , %= , and here is a long list of examples:&lt;div class="code"&gt;int myvariable;   // intializing without initial value&lt;br /&gt;myvariable = 5 + 2;   // 7&lt;br /&gt;myvariable = myvariable - 4;   //3&lt;br /&gt;myvariable = 6 * myvariable;   //18&lt;br /&gt;myvariable = myvariable / 3;   //6&lt;br /&gt;myvariable = myvariable % 4;   //2 (division remainder)&lt;br /&gt;myvariable = 10;   //10&lt;br /&gt;myvariable += 2;   //12&lt;br /&gt;myvariable -= 8;   //4&lt;br /&gt;myvariable *= 2;   //8&lt;br /&gt;myvariable /= 2;   //4&lt;br /&gt;myvariable %= 3;   //1&lt;/div&gt;To write out the value of a variable just leave the double quotes from the cout line:&lt;div class="code"&gt;cout &lt;&lt; myvariable;&lt;/div&gt;, but we will talk more about cout in the next lesson. Here is an example program that uses integer variables:&lt;div class="code"&gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;using namespace std;&lt;br /&gt;int main(){&lt;br /&gt;   int myvariable;   // intializing without initial value&lt;br /&gt;   myvariable = 5 + 2;   //7&lt;br /&gt;   myvariable = myvariable - 4;   //3&lt;br /&gt;   myvariable = 6 * myvariable;   //18&lt;br /&gt;   myvariable = myvariable / 3;   //6&lt;br /&gt;   myvariable = myvariable % 4;   //2 (division remainder)&lt;br /&gt;   myvariable = 10;   //10&lt;br /&gt;   myvariable += 2;   //12&lt;br /&gt;   myvariable -= 8;   //4&lt;br /&gt;   myvariable *= 2;   //8&lt;br /&gt;   myvariable /= 2;   //4&lt;br /&gt;   myvariable %= 3;   //1&lt;br /&gt;   cout &amp;lt;&amp;lt; myvariable &amp;lt;&amp;lt; endl; &lt;br /&gt;   system(&amp;quot;pause&amp;quot;);&lt;br /&gt;}&lt;/div&gt;The above program should write out 1. Play with the code, remove and add new lines to familiarize with integers.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Increment and Decrement&lt;/b&gt;&lt;br /&gt;Using the incrementing (++) or decrementing (--) operators means to add or to subtract 1 from the value of an &lt;b&gt;integer&lt;/b&gt; variable. These operators can be used in two ways:&lt;div class="code"&gt;++number; //the value is increased &lt;b&gt;before&lt;/b&gt; the value is used in the rest of the expression&lt;br /&gt;&lt;br /&gt;or&lt;br /&gt;&lt;br /&gt;number++ //the value is increased &lt;b&gt;after&lt;/b&gt; the value is used in the rest of the expression&lt;/div&gt;For example try out the following code:&lt;div class="code"&gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;&lt;br /&gt;using namespace std;&lt;br /&gt;&lt;br /&gt;int main(){&lt;br /&gt;   int numberone = 2;&lt;br /&gt;   int numbertwo = 2;&lt;br /&gt;   cout &amp;lt;&amp;lt; ++numberone &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; numbertwo++ &amp;lt;&amp;lt; endl;&lt;br /&gt;   system(&amp;quot;pause&amp;quot;);&lt;br /&gt;}&lt;/div&gt;As you probably noticed the two written values are not the same. The first is 3, because the value is increased before it`s written and the second value is 2,because it`s increased only after it`s written.&lt;br /&gt;Note: The increment and decrement operators are working only with integers!&lt;br /&gt;&lt;br /&gt;&lt;b&gt;float&lt;/b&gt; and &lt;b&gt;double&lt;/b&gt;&lt;br /&gt;Float and double are working exactly like integers, but these can be floating point numbers, too (2.75 or 9.24323). The difference between float and double is that a double can have more numbers after the decimal point then a float. Initializing a float or a double should look like in the bellow example:&lt;div class="code"&gt;float myvariable;&lt;br /&gt;or&lt;br /&gt;float myvariable = 4.23;&lt;/div&gt;&lt;br /&gt;&lt;b&gt;Making the result of integer division a float&lt;/b&gt;&lt;br /&gt;Try out to give the value of dividing two integers to a float. The result will don`t have floating point any numbers you use. To solve this look at my example:&lt;br /&gt;&lt;div class="code"&gt;int numone = 1;&lt;br /&gt;int numtwo = 2;&lt;br /&gt;float result = (float) numone / numtwo;&lt;/div&gt;Try out the above example, it should work.&lt;br /&gt;&lt;div class="code"&gt;&lt;br /&gt;double myvariable;&lt;br /&gt;or&lt;br /&gt;double myvariable = 5.24;&lt;/div&gt;&lt;br /&gt;&lt;b&gt;Booleans&lt;/b&gt;&lt;br /&gt;Booleans can have only two values: &lt;b&gt;true(1)&lt;/b&gt; or &lt;b&gt;false(0)&lt;/b&gt;. Initializing a boolean should look like in the bellow example:&lt;div class="code"&gt;bool myvariable = true;&lt;br /&gt;or&lt;br /&gt;bool myvariable = false;&lt;/div&gt;Note: In C++ a and A are not equal. Be careful, &lt;b&gt;don`t&lt;/b&gt; use uppercase letters to write true or false. This is true for the name of the variables, too. For example: myvariable and Myvariable are two different variables.&lt;br /&gt;&lt;br /&gt;Next lesson: &lt;a href="http://www.codingmix.com/2010/06/cplusplus-lesson-5-inputoutput.html"&gt;C++ Lesson 5 - Input/Output&lt;/a&gt;&lt;br /&gt;Previous lesson: &lt;a href="http://www.codingmix.com/2010/06/cplusplus-lesson-3-hello-world.html"&gt;C++ Lesson 3 - Hello world&lt;/a&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5682776130825686229-2798748566903162870?l=www.codingmix.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/SOwxA8k6yC8xQCdWqzPNNK9sHAM/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/SOwxA8k6yC8xQCdWqzPNNK9sHAM/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/SOwxA8k6yC8xQCdWqzPNNK9sHAM/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/SOwxA8k6yC8xQCdWqzPNNK9sHAM/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/codingmix/Cplusplus/~4/Tc6KAkPhqYM" height="1" width="1"/&gt;</description><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total><feedburner:origLink>http://www.codingmix.com/2010/06/cplusplus-lesson-4-variables.html</feedburner:origLink></item><item><title>C++ Lesson 3 - Hello world</title><link>http://feedproxy.google.com/~r/codingmix/Cplusplus/~3/RRucSVCPiIw/cplusplus-lesson-3-hello-world.html</link><category>Cplusplus lessons</category><category>Cplusplus</category><author>noreply@blogger.com (Csabi)</author><pubDate>Fri, 03 Sep 2010 03:02:34 PDT</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-5682776130825686229.post-5544442831280551715</guid><description>In this lesson I will show you how to create a simple program, called Hello world, which will simply just write out "Hello world!". A Hello world program is the best way to start in programming language, because this gives a basic understand of the syntax of the code.&lt;br /&gt;&lt;br /&gt;First set up an empty Win32 Console Application project and add a C++ file (main.cpp) like in &lt;a href="http://www.codingmix.com/2010/06/cplusplus-lesson-set-up-visual.html"&gt;lesson 2&lt;/a&gt;.&lt;br /&gt;&lt;div id="full-post"&gt;&lt;br /&gt;Write the following code and press F5 to run it:&lt;div class="code"&gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;&lt;br /&gt;using namespace std;&lt;br /&gt;&lt;br /&gt;int main(){&lt;br /&gt;   cout &amp;lt;&amp;lt; &amp;quot;Hello world!&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;   system(&amp;quot;pause&amp;quot;);&lt;br /&gt;}&lt;/div&gt;The first line includes the &lt;b&gt;iostream&lt;/b&gt; library which provides the input and output functions. Without this function we can`t write out the result of the program. Notice that wen a library is included &lt;b&gt;#include&lt;/b&gt; is used.&lt;br /&gt;&lt;br /&gt;The second line tell the compiler to use the &lt;b&gt;std&lt;/b&gt; namespace, namespace allows to create groups of functions or objects under a name. Don`t worry, I will write more about namespace later. For now it`s good to know that if you leave this line from the code you will need to include std:: before each cout or cin from your code. Notice that the line is ended by a semicolon (";");&lt;br /&gt;&lt;br /&gt;The third line starts a function called main, which will return and int (integer). The main function starts first in any program. The curly braces { and } shows the beginning and end of the function.&lt;br /&gt;&lt;br /&gt;The next line writes out the "Hello world!" text. Notice that the line is ended by a semicolon (";").&lt;br /&gt;&lt;br /&gt;The next line is telling the compiler to pause the program and wait for the user to press Enter. Without &lt;b&gt;system("pause")&lt;/b&gt; the program will run so fast that you will not see the result. &lt;br /&gt;&lt;br /&gt;Let`s see the program in action:&lt;a href="http://3.bp.blogspot.com/_Ok7mVUxPcsk/TBTQYne-xnI/AAAAAAAAAPk/DngW-SXPnvU/s1600/Hello+world.jpg"&gt;&lt;img style="display:block; margin:5px auto 5px; text-align:center;width: 541px; height: 273px;" src="http://3.bp.blogspot.com/_Ok7mVUxPcsk/TBTQYne-xnI/AAAAAAAAAPk/DngW-SXPnvU/s1600/Hello+world.jpg" border="0" alt="Hello world example"id="BLOGGER_PHOTO_ID_5482235767751100018" /&gt;&lt;/a&gt;Note: Don`t worry if you haven`t understand everything. This lesson is written to have an idea about how a program looks and to see the basic syntax of C++. &lt;br /&gt;&lt;br /&gt;Another useful and simple thing to learn are comments: In C++ you can add comments (notes) after the code lines. The comments are skipped by the compiler and they don`t affect the program. Comment are helpful to note or explain things. The comment syntax is:&lt;div class="code"&gt;//Comment goes here&lt;/div&gt;or&lt;div class="code"&gt;/*Comment line 1&lt;br /&gt;Comment line 2 */&lt;/div&gt;&lt;br /&gt;Next lesson: &lt;a href="http://www.codingmix.com/2010/06/cplusplus-lesson-4-variables.html"&gt;C++ Lesson 4 - Variables&lt;/a&gt;&lt;br /&gt;Previous lesson: &lt;a href="http://www.codingmix.com/2010/06/cplusplus-lesson-set-up-visual.html"&gt;C++ Lesson 2 - Set up Visual C++&lt;/a&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5682776130825686229-5544442831280551715?l=www.codingmix.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/IidOF6dkDWbveUmPLLWVVWecIUE/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/IidOF6dkDWbveUmPLLWVVWecIUE/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/IidOF6dkDWbveUmPLLWVVWecIUE/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/IidOF6dkDWbveUmPLLWVVWecIUE/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/codingmix/Cplusplus/~4/RRucSVCPiIw" height="1" width="1"/&gt;</description><media:thumbnail url="http://3.bp.blogspot.com/_Ok7mVUxPcsk/TBTQYne-xnI/AAAAAAAAAPk/DngW-SXPnvU/s72-c/Hello+world.jpg" height="72" width="72" /><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">1</thr:total><feedburner:origLink>http://www.codingmix.com/2010/06/cplusplus-lesson-3-hello-world.html</feedburner:origLink></item><item><title>C++ Lesson 2 - Set up Visual C++</title><link>http://feedproxy.google.com/~r/codingmix/Cplusplus/~3/1mnYlHgm5xU/cplusplus-lesson-set-up-visual.html</link><category>Cplusplus lessons</category><category>Cplusplus</category><author>noreply@blogger.com (Csabi)</author><pubDate>Wed, 01 Sep 2010 07:10:29 PDT</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-5682776130825686229.post-8448597953122743886</guid><description>This lesson is about setting up Visual C++ for a console application. Console applications are the easiest C++ program so this is the best way to start.&lt;br /&gt;&lt;br /&gt;First it`s good to create a folder for your projects (anywhere you want), then go to:&lt;br /&gt;&lt;b&gt;File &gt;&gt; New &gt;&gt; Project...&lt;/b&gt; and select &lt;b&gt;Win32 Console Application:&lt;/b&gt;&lt;div id="full-post"&gt;&lt;br /&gt;&lt;a href="http://1.bp.blogspot.com/_Ok7mVUxPcsk/TBH6sREgkSI/AAAAAAAAAO8/iJuy_yiWYhA/s1600/Set+up+visual+c%2B%2B.jpg"&gt;&lt;img style="display:block; margin:5px auto 5px; text-align:center; width: 545px; height: 377px;" src="http://1.bp.blogspot.com/_Ok7mVUxPcsk/TBH6sREgkSI/AAAAAAAAAO8/iJuy_yiWYhA/s1600/Set+up+visual+c%2B%2B.jpg" border="0" alt="Set up Visual C++"id="BLOGGER_PHOTO_ID_5481437859890106658" /&gt;&lt;/a&gt;At the bottom give a name for your project, same as the solution name. At the location browse and select your projects folder to place the new project in.&lt;br /&gt;&lt;br /&gt;Then click &lt;b&gt;Ok&lt;/b&gt;, then &lt;b&gt;next&lt;/b&gt; (NOT finish), and check in the &lt;b&gt;Empty project&lt;/b&gt; checkbox:&lt;a href="http://3.bp.blogspot.com/_Ok7mVUxPcsk/TBINVKk-paI/AAAAAAAAAPE/dRBp7aGaWbk/s1600/Set+up+visual+c%2B%2B+project.jpg"&gt;&lt;img style="display:block; margin:5px auto 5px; text-align:center;cursor:pointer; cursor:hand;width: 545px; height: 461px;" src="http://3.bp.blogspot.com/_Ok7mVUxPcsk/TBINVKk-paI/AAAAAAAAAPE/dRBp7aGaWbk/s1600/Set+up+visual+c%2B%2B+project.jpg" border="0" alt="Set up Visual C++"id="BLOGGER_PHOTO_ID_5481458353731184034" /&gt;&lt;/a&gt;Then on the &lt;b&gt;Solution Explorer&lt;/b&gt; (at the left side) right click on &lt;b&gt;Source Files&lt;/b&gt; and select &lt;b&gt;Add &gt;&gt; New Item...&lt;/b&gt;&lt;a href="http://1.bp.blogspot.com/_Ok7mVUxPcsk/TBIqCTBnSHI/AAAAAAAAAPM/Q3TKGwupghE/s1600/Set+up+visual+c%2B%2B+project+2.jpg"&gt;&lt;img style="display:block; margin:5px auto 5px; text-align:center;width: 529px; height: 338px;" src="http://1.bp.blogspot.com/_Ok7mVUxPcsk/TBIqCTBnSHI/AAAAAAAAAPM/Q3TKGwupghE/s1600/Set+up+visual+c%2B%2B+project+2.jpg" border="0" alt="Set up Visual C++ console"id="BLOGGER_PHOTO_ID_5481489915418462322" /&gt;&lt;/a&gt;Select &lt;b&gt;C++ File (.cpp)&lt;/b&gt;, name it whatever you want (I will call it &lt;b&gt;main&lt;/b&gt;) , then click &lt;b&gt;Add&lt;/b&gt;&lt;a  href="http://1.bp.blogspot.com/_Ok7mVUxPcsk/TBJfbDRQzhI/AAAAAAAAAPU/zPBfa_VCgN8/s1600/Set+up+visual+c%2B%2B+project+3.jpg"&gt;&lt;img style="display:block; margin:5px auto 5px; text-align:center;width: 545px; height: 377px;" src="http://1.bp.blogspot.com/_Ok7mVUxPcsk/TBJfbDRQzhI/AAAAAAAAAPU/zPBfa_VCgN8/s1600/Set+up+visual+c%2B%2B+project+3.jpg" border="0" alt="C++ Set up add item"id="BLOGGER_PHOTO_ID_5481548614802132498" /&gt;&lt;/a&gt;And this is it, now just expand the &lt;b&gt;Source Files&lt;/b&gt; folder and double click on your file (what I`we called &lt;b&gt;main&lt;/b&gt;) and start coding !&lt;a href="http://4.bp.blogspot.com/_Ok7mVUxPcsk/TBJ6zMkr6aI/AAAAAAAAAPc/_1bSmZB92zE/s1600/Set+up+visual+c%2B%2B+project+4.jpg"&gt;&lt;img style="display:block; margin:5px auto 5px; text-align:center;width: 545px; height: 448px;" src="http://4.bp.blogspot.com/_Ok7mVUxPcsk/TBJ6zMkr6aI/AAAAAAAAAPc/_1bSmZB92zE/s1600/Set+up+visual+c%2B%2B+project+4.jpg" border="0" alt="Setting up Visual c++ ready"id="BLOGGER_PHOTO_ID_5481578716430330274" /&gt;&lt;/a&gt;Don`t forget to save your project (go to &lt;b&gt;File &gt;&gt; Save all&lt;/b&gt; or press &lt;b&gt;Ctrl + Shift + S&lt;/b&gt;).&lt;br /&gt;To debug and start your program press &lt;b&gt;F5&lt;/b&gt;, for sure when you will write one.&lt;br /&gt;&lt;br /&gt;Check out my video about this lesson: &lt;a href="http://www.youtube.com/watch?v=suaQxj3nnMc"&gt;How to set up Visual C++ for a Console Application&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Next lesson: &lt;a href="http://www.codingmix.com/2010/06/cplusplus-lesson-3-hello-world.html"&gt;C++ Lesson 3 - Hello world&lt;/a&gt;&lt;br /&gt;Previous lesson: &lt;a href="http://www.codingmix.com/2010/06/introducing-to-cplusplus-lesson-1.html"&gt;C++ Lesson 1 - Introducing&lt;/a&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5682776130825686229-8448597953122743886?l=www.codingmix.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/6zYdaKplQ-DGZyJ2GxmdZco8x10/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/6zYdaKplQ-DGZyJ2GxmdZco8x10/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/6zYdaKplQ-DGZyJ2GxmdZco8x10/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/6zYdaKplQ-DGZyJ2GxmdZco8x10/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/codingmix/Cplusplus/~4/1mnYlHgm5xU" height="1" width="1"/&gt;</description><media:thumbnail url="http://1.bp.blogspot.com/_Ok7mVUxPcsk/TBH6sREgkSI/AAAAAAAAAO8/iJuy_yiWYhA/s72-c/Set+up+visual+c%2B%2B.jpg" height="72" width="72" /><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">1</thr:total><feedburner:origLink>http://www.codingmix.com/2010/06/cplusplus-lesson-set-up-visual.html</feedburner:origLink></item><item><title>C++ Lesson 1 - Introducing</title><link>http://feedproxy.google.com/~r/codingmix/Cplusplus/~3/QdqKOnUeYek/introducing-to-cplusplus-lesson-1.html</link><category>Cplusplus lessons</category><category>Cplusplus</category><author>noreply@blogger.com (Csabi)</author><pubDate>Wed, 01 Sep 2010 07:10:41 PDT</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-5682776130825686229.post-6030285891847709676</guid><description>First let`s talk about programming. What programming really means? Programming means to write the source-code of a program. The source code is the human-readable part of a program. When a program is running it tell the computer what to do, but the computer don`t understands human languages. This is the reason why we need a Compiler. A compiler is a program what translates the human-readable code to machine code what is understood by the computer. &lt;br /&gt;&lt;div id="full-post"&gt;&lt;br /&gt;You probably think that the computer is very wise, but it`s not. The computer it`s able only to follow simple instructions. In the following lessons we will learn about giving some basic instruction to the computer using C++. C++ is a very powerful programming language, many of the newest and best games are made in C++.&lt;br /&gt;&lt;br /&gt;Ok, now that you know what means to program you are just one step from starting to program. As I mentioned above, the human-readable code must be translated to machine code. To translate the code we need a compiler. You can choose from many compiler, but I will choose Visual C++ Express from Microsoft. Visual C++ 2010 can be download for free from &lt;a href="http://www.microsoft.com/express/Downloads"&gt;here&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;It`s easy to install it and I think that somebody who wants to program knows how to install a simple program or a game. Note: Download only the Visual C++ Express, we will use only this one.&lt;br /&gt;&lt;br /&gt;In the following lesson you will see lots of example codes and downloadable source-codes at each lesson, but don`t just copy it, try to write it on yourself (multiple times if you think), because the best way to learn programming is practicing.&lt;br /&gt;&lt;br /&gt;Next lesson: &lt;a href="http://www.codingmix.com/2010/06/cplusplus-lesson-set-up-visual.html"&gt;C++ Lesson 2 - Set up Visual C++&lt;/a&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5682776130825686229-6030285891847709676?l=www.codingmix.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/FFkIWAeU7wHefJJhJHgZ__Grgfk/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/FFkIWAeU7wHefJJhJHgZ__Grgfk/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/FFkIWAeU7wHefJJhJHgZ__Grgfk/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/FFkIWAeU7wHefJJhJHgZ__Grgfk/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/codingmix/Cplusplus/~4/QdqKOnUeYek" height="1" width="1"/&gt;</description><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">3</thr:total><feedburner:origLink>http://www.codingmix.com/2010/06/introducing-to-cplusplus-lesson-1.html</feedburner:origLink></item><media:rating>nonadult</media:rating></channel></rss>

