Author Topic: My everyday C++ learning notes  (Read 33362 times)

Offline CaptainCPS

  • FBNeo Dev
  • ******
  • Posts: 1513
  • Karma: +127/-0
  • FB Alpha Team
    • CaptainCPS's Home
My everyday C++ learning notes
« on: December 26, 2007, 01:42:28 AM »
The purpose of this thread is to share with NeoSource users 'some' notes about what I learn from C++ coding, and discus different point of views (with those who like to participate here).

btw, I will not post rated material, so I assume that the reader know at least a little about C++, if you don't understand something please let me know so I can help you too :smilie:. Everything here is written by me and if I use coding from another site I will let it know very clear where it comes from.

I will start with something useful when declaring variables of different types...

:: Making life easier with Arrays

When declaring variables you have various alternatives, for example, you have 10 documents and you plan to save the document titles into different variables for use later in some part of your application / program, well in that case you can simply do this two things to declare variables :

Quote

char Document_01[64] = "Die Hard I";
char Document_02[64] = "The Butterfly";
char Document_03[64] = "Rocketman";
char Document_04[64] = "Predator";
char Document_05[64] = "Believe it or Not";
char Document_06[64] = "I love my Pizza";
char Document_07[64] = "Pictures of Animals";
char Document_08[64] = "Zelda";
char Document_09[64] = "XXX";
char Document_10[64] = "NeoSource for Dummies";


...or, OK the is still one more ''simple'' way to put this stuff...

Quote

char Document_01[64] = "Die Hard I", Document_02[64] = "The Butterfly", Document_03[64] = "Rocketman", Document_04[64] = "Predator", Document_05[64] = "Believe it or Not", Document_06[64] = "I love my Pizza", Document_07[64] = "Pictures of Animals", Document_08[64] = "Zelda", Document_09[64] = "XXX", Document_10[64] = "NeoSource for Dummies";


Man, ...that coding looks nasty  :p, lets bring Arrays right now to our application before this convert into a war zone  :p. Ok, so I will describe arrays in my own way, they are very similar to normal variables but with only one declaration you can save looooot of typing, why?, well it stores data values into different elements, you can specify how much elements it will have like this (lets use 10 elements for our 10 document titles):

Quote

char Document[10][64] = { {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""} };


So now that you know that, I will make the same thing as the previous examples BUT with just an Array  :smilie::

Quote

char Document[10][64] = { {"Die Hard I"}, {"The Butterfly"}, {"Rocketman"}, {"Predator"}, {"Believe it or Not"}, {"I love my Pizza"}, {"Pictures of Animals"}, {"Zelda"}, {"XXX"}, {"NeoSource for Dummies"} };


The titles of the documents are obtained by simply adding the values directly into the array right now, but I will show you how we can get them from another array that could be declared in other place and with more data different than the last one, in that case we just handle the array contents specifying the element number, check it out:

We have the titles coming from an array called 'Title' that have 10 elements.

Quote

char Title[10][64] = {
{"Die Hard I"},
{"The Butterfly"},
{"Rocketman"},
{"Predator"},
{"Believe it or Not"},
{"I love my Pizza"},
{"Pictures of Animals"},
{"Zelda"},
{"XXX"},
{"NeoSource for Dummies"}
};


Now I will use a loop to copy all the titles to the 'Document' array without writing the 'Title[1], Title[2], Title[3], etc...':

Quote

for( int nElement = 0; nElement < 10; nElement++) {
   strcpy( Document[nElement], Title[nElement] );
}


That loop is not posible when using individual variables for each document title and requires a long switch (of course if we have the titles coming from an array):

Quote

for( int nElement = 0; nElement < 10; nElement++) {
   switch(nElement)
   {
      case 1:
         strcpy( Document_01, Title[nElement] );
      break;
      case 2:
         strcpy( Document_02, Title[nElement] );
      break;
      case 3:
         strcpy( Document_03, Title[nElement] );
      break;
      case 4:
         strcpy( Document_04, Title[nElement] );
      break;
      case 5:
         strcpy( Document_05, Title[nElement] );
      break;
      case 6:
         strcpy( Document_06, Title[nElement] );
      break;
      case 7:
         strcpy( Document_07, Title[nElement] );
      break;
      case 8:
         strcpy( Document_08, Title[nElement] );
      break;
      case 9:
         strcpy( Document_09, Title[nElement] );
      break;
      case 10:
         strcpy( Document_10, Title[nElement] );
      break;
   }
}



So that ends my C++ Learning notes for today ^^, if you have anything to comment or share Ill be happy to read ^^

I attached a example .cpp file that can be compiled to test what I was doing here  :smilie:
(If you have Visual Studio you can compile it from the Visual Studio command prompt located on Tools, write 'c:\***yourpath***\cl easyarrays_01.cpp' and press enter)

SeeYaa!
 :biggrin:

Offline CaptainCPS

  • FBNeo Dev
  • ******
  • Posts: 1513
  • Karma: +127/-0
  • FB Alpha Team
    • CaptainCPS's Home
Re: My everyday C++ learning notes
« Reply #1 on: January 06, 2008, 12:33:39 AM »
Since this thread would be a mess if I talk about different types of coding in C++ I will better be posting in the future each topic in it's own Thread.

I'm gonna post something soon about opening files and getting a text input stream buffer from it, so then we can use that buffer to analyse it or just display it on screen =)

SeeYaa!
 :biggrin:

Offline kenshiro

  • Expert
  • *****
  • Posts: 145
  • Karma: +21/-0
Re: My everyday C++ learning notes
« Reply #2 on: August 15, 2008, 04:33:47 AM »
I know that topic is a bit old, but i like the idea to share coding tricks, and learn new ways to write code :cool:

Captain CPSX, do you think you will continue to post in that thread?  :rolleyes:

I would like to participate too, but not sure my explanations will be properly lol  :biggrin: (i'm a French native).

That was the morning suggestion :biggrin: lol

Offline Surachaiy

  • New Member
  • *
  • Posts: 2
  • Karma: +0/-1
Re: My everyday C++ learning notes
« Reply #3 on: November 02, 2017, 04:17:35 AM »
Recording messages is a great reminder to me.