Dark Bit Factory & Gravity

PROGRAMMING => C / C++ /C# => Topic started by: Shockwave on March 12, 2007

Title: Shockwave's pathetic learning attempts thread...
Post by: Shockwave on March 12, 2007
Going to post all my hello world stuff in here in the vain hope that it might help someone.. Perhaps.

Here's the first.. It does use an API! windows.h to do a message box (nicer to look at than getchar :P );

Code: [Select]
//------------------------------------------------------------------------------
// Includes
//
// This program is a test to see if I managed to get addition working...
// By Shockwave.
//------------------------------------------------------------------------------

#include <stdio.h>
#include <windows.h>

int main()
{
    unsigned short int a=5,b=4,c=0;
    c=a+b;
    printf ("The result of A+B is: %u",c);
//------------------------------------------------------------------------------
// Menu Box.
// Looks Nicer than waiting for a key to be pressed :-P
//------------------------------------------------------------------------------

    MessageBox (0, "Click Ok To Exit!\nOr you can carry on looking at this empty screen for the rest of the day...\n" ,"Program Has Finished.", 0);
    return 0;
}
Title: Re: Shockwave's pathetic learning attempts thread...
Post by: Stonemonkey on March 12, 2007
Helps me, thanks.
Title: Re: Shockwave's pathetic learning attempts thread...
Post by: Shockwave on March 12, 2007
I used the stdio.h instead of iostream, stdio.h seems a lot smaller. This compiles to about 15kb. :)
Title: Re: Shockwave's pathetic learning attempts thread...
Post by: Stonemonkey on March 12, 2007
What kind of project should it be?

btw, using Jims gfx window example (messed around with quite a bit) and got some stuff like gtriangles and image drawing I get an exe at 11k atm.
Title: Re: Shockwave's pathetic learning attempts thread...
Post by: Shockwave on March 12, 2007
Empty project :)

I think that I am going to be learning the basics of the language before I do any geometry. I made this in Devc for now as I don't have VC on my desktop... Yet!

Here is the link to stdio, it seems like a good library and would do all you want in a console I think.

STDIO (http://www.cplusplus.com/cstdio)

Title: Re: Shockwave's pathetic learning attempts thread...
Post by: Stonemonkey on March 12, 2007
Ah right, that might explain why it's not working here.
Title: Re: Shockwave's pathetic learning attempts thread...
Post by: Paul on March 13, 2007
Works fine :)
I'ts nice that you post your learning code for all to see and learn from

I already had studio.h, probably came with dev c++
Title: Re: Shockwave's pathetic learning attempts thread...
Post by: Shockwave on March 13, 2007
devc seems to be a lot easier to install, don't like the IDE as much, but probably it would be an easier choice for those who are coming from the Freebasic ide, they are both very similar.

I'd say fbide was probably based on devc's ide having used both.
Title: Re: Shockwave's pathetic learning attempts thread...
Post by: mike_g on March 13, 2007
I really have to get around to having a go at doing some graphics stuff in C sometime.

Shockwave, You probably know this but stdio.h does the standard C i/o stuff, iostream is C++. If you like to keep your code as short as possible then C is the way to go. From what little experience I have had with C++ its a whole load more code, to write. For small projects it seems to me that C is better.

I'm using dev c++ version 4.9.9.2 (the newest one), the IDE looks quite a bit nicer than the previous version, but still no function folding :( So far the best thing I have made with Dev C++ is my colour scheeme (http://i92.photobucket.com/albums/l15/mikegrundel/DevCColourScheeme.png)  :P
Title: Re: Shockwave's pathetic learning attempts thread...
Post by: TinDragon on March 13, 2007
I have to say that personally I feel people wanting to experiment with C/C++ are much better off with DevC++ package over these cut down express visual studio jobs, the MS one might be an awesome IDE but they seem to come with most of the standard libs missing from the package. The DevC++ install includes all the standard stuff and some other stuff as well, and with some digging around you can get devpacs to install things like DX etc.

I did post in this forum a project for devc++ that had tinyptc using directdraw if you want to experiment and get somethin more visual that text on screen Shockwave. Principals for use are much the same as in FB excpet it's a C file not C++ but unless yoor going for OOP and all that fancy stuff C will do for demo effects :)
Title: Re: Shockwave's pathetic learning attempts thread...
Post by: Shockwave on March 13, 2007
Thanks Jon, none of your points can be argued with, in fact the installation of visual c on my laptop was the most frustrating installation of any program I have ever installed in my life, no exageration.

I guess that now I am finally getting into this language it might be time for me to hit the hardware, at the moment I am thinking Directx and in the long term I want a job doing this so I think that I will need to use OOP :)
Title: Re: Shockwave's pathetic learning attempts thread...
Post by: Jim on March 14, 2007
Might be a bit early to start correcting mistakes like this, on the other hand if you get it right now, it won't come back to haunt you later.
Code: [Select]
unsigned short int a=5,b=4,c=0;
    c=a+b;
    printf ("The result of A+B is: %u",c);
If you look up printf in the help, and then click through to 'printf Type Field Characters' you can see all the types of things that printf can print.  %u  is the format specifier for 'unsigned int'.  In your program, 'c' is of type 'unsigned short' which is different.  There's no format specifier for unsigned short, so you need a type-cast - you need to change the type of 'c' to match the %u that you promised.  So the simplest fix is
Code: [Select]
unsigned short int a=5,b=4,c=0;
    c=a+b;
    printf ("The result of A+B is: %u",(unsigned int)c);
Another possible fix is to use C++ instead (cout << c).

Now, you'll probably say "but it worked ok", and it almost certainly did.  But in the C standard this is called 'undefined behaviour' which means the code could do anything, including blowing up the planet or working properly :)

Jim
Title: Re: Shockwave's pathetic learning attempts thread...
Post by: Shockwave on March 14, 2007
Thanks Jim :) I definately want my code to be stable so cheers for the fix.. I am going to be doing some more simple stuff today.. Hopefully I'll have some more stuff to post later.
Title: Re: Shockwave's pathetic learning attempts thread...
Post by: Shockwave on March 14, 2007
Wahey! A for next loop...
I think I did it right... Any tips / bugfixes welcome and appreciated! :D

Code: [Select]
//------------------------------------------------------------------------------
// This program will do some simple "for next" loops.
// And then wait for a user to click the window at the end to close.
// Lame code by Shockwave. Heh!
//------------------------------------------------------------------------------

   #include <Windows.h>
   #include <stdio.h>

//------------------------------------------------------------------------------
// Every program must have a Main()!
//------------------------------------------------------------------------------
 
int main()
{
          //--------------------------------------------------------------------
          // The "for next" loop!
          //--------------------------------------------------------------------
          {
          int a;
          for (a = 0 ; a < 10; a++ )         
          printf( "This should loop 10 times... %u\n",(unsigned int)a);
          }
   
          MessageBox (0, "The Program Has ended.\nOutput is shown in the console window." ,"This Program Has Finished.", 0);
          //--------------------------------------------------------------------
          // Not really needed but to be complient, we should return 0 when we end.
          //---------------------------------------------------------------------
          return 0;
}

Written with Devc.
Title: Re: Shockwave's pathetic learning attempts thread...
Post by: Shockwave on March 14, 2007
Hehe, another one. I wanted to be able to loop backwards too. So just so I knew I had got it..

Code: [Select]
//------------------------------------------------------------------------------
// This program will do some simple "for next" loops.
// And then wait for a user to click the window at the end to close.
// Lame code by Shockwave. Heh!
//------------------------------------------------------------------------------

   #include <Windows.h>
   #include <stdio.h>

//------------------------------------------------------------------------------
// Every program must have a Main()!
//------------------------------------------------------------------------------
 
int main()
{
          //--------------------------------------------------------------------
          // The "for next" loop! Counting forwards!
          //--------------------------------------------------------------------
          {
          int a;
          for (a = 0 ; a < 10; a++ )         
          printf( "This should loop 10 times... Counting up!!%u\n",(unsigned int)a);
          }

          MessageBox (0, "Now we will loop backwards.\nOutput is shown in the console window." ,"That's the first bit!", 0);
         
          //--------------------------------------------------------------------
          // The "for next" loop! Counting backwards!
          //--------------------------------------------------------------------
          {
          int a;
          for (a = 10 ; a > 0; a-- )         
          printf( "This should loop 10 times... Counting Down!!%u\n",(unsigned int)a);
          }

             
          MessageBox (0, "The Program Has ended.\nOutput is shown in the console window." ,"This Program Has Finished.", 0);
          //--------------------------------------------------------------------
          // Not really needed but to be complient, we should return 0 when we end.
          //---------------------------------------------------------------------
          return 0;
}
Title: Re: Shockwave's pathetic learning attempts thread...
Post by: Jim on March 14, 2007
Code: [Select]
printf( "This should loop 10 times... %u\n",(unsigned int)a);Because 'a' is an int, you could have done
Code: [Select]
printf( "This should loop 10 times... %d\n",a);%d means the parameter is a (signed) int.

Other than that, you've got an extra set of curly brackets in there that you don't need, but yep, you've reinvented the for loop :)
You definitely need the return 0, it was required until the standard was updated in 1999 (C99).

Jim
Title: Re: Shockwave's pathetic learning attempts thread...
Post by: Shockwave on March 14, 2007
Thanks Jim :)
Title: Re: Shockwave's pathetic learning attempts thread...
Post by: Shockwave on March 14, 2007
More lameness :P

Here's a while Wend loop...

Wow I could code an outstanding Hello World now :D

Code: [Select]
//
// Let's see if we can get a While.. Wend loop working :)
// Hopelessly basic code by Schlockwave.
//
//------------------------------------------------------------------------------

#include <windows.h>
#include <stdio.h>

//------------------------------------------------------------------------------
// Every Program should have a Main!
//------------------------------------------------------------------------------

int main()
{
   
    //--------------------------------------------------------------------------
    // We need a couple of integers, one to have the target, one to have the count
    //--------------------------------------------------------------------------
   
    int howmanytimes = 5;
    int counter = 0;
   
    //--------------------------------------------------------------------------
    // Here's the While.. Wend loop!
    //--------------------------------------------------------------------------
    while (counter<howmanytimes)       // Test to see if condition true..
    {
    counter++;                         // ++ Means Inc -- Means Dec
    printf("COUNTER VALUE :%d   COUNTER TARGET :%d\n",counter,howmanytimes);
    MessageBox (0, "Click Ok To Continue!" ,"We are in the middle of a While .. Wend Loop!", 0);
    }
    //--------------------------------------------------------------------------
    // We should Return 0 when we leave :-)
    //--------------------------------------------------------------------------
    MessageBox (0, "Program has ended because condition was fulfilled!\n press ok to exit!\n" ,"We are now out of the loop :-)", 0);
    return 0;
}

Hope someone somewhere benefits from this stuff..
Title: Re: Shockwave's pathetic learning attempts thread...
Post by: benny! on March 14, 2007
Keep your steps coming, Shocky. It's nice to see how your initial hello world
program expands.
Title: Re: Shockwave's pathetic learning attempts thread...
Post by: Shockwave on March 14, 2007
I figure that I need to cover these basic steps, then once I have a 30% grasp of the language I can move on to do the intereting stuff :)

I know that there are a lot of people here who use various basics that would like to use C++, hopefully I can take some of them along with me..
Title: Re: Shockwave's pathetic learning attempts thread...
Post by: Paul on March 14, 2007
Quote
know that there are a lot of people here who use various basics that would like to use C++,

Me for one :)
Title: Re: Shockwave's pathetic learning attempts thread...
Post by: Shockwave on March 15, 2007
Well then Paul, you'd be pleased to know that I have another ropey listing for you then :)

This one is a simple example of the use of arrays in C++
It defines an array, puts some values into it and then loops through them displaying the contents..

Code: [Select]
//
// This is just an excercise I did to see if I could define a simple
// array of integers and print the contents. By Schhlockkwav3 Aemon Holmes
// is boring.
//------------------------------------------------------------------------------

#include <windows.h>
#include <stdio.h>

//
// Every program should have a main.
//

int main()
{
    //
    // Create an array of integers and slap fome values in there.
    //
    int myarray[5] = {100,200,300,200,100};
    //
    // Loop counter;
    //
    int lp;
    //
    // Loop throught each element of the array;
    //
    for (lp = 0 ; lp<5 ; lp++)
   
    {
        //
        // Print  The element No. (Nb. Arrays start at 0 in C++) and the value
        //
        printf("contents of the array Element No.%d Value %d ! J00 Fuxxorz!!!\n",lp,myarray[lp]);
    }
    //
    // All done so wait for the click before leaving.
    //
    MessageBox (0, "J00 FuXX0rz.\nContents Of Array Displayed In Window." ,"This Program Has Finished.", 0);   
    return 0;
}

Apologies again for the basic nature of these sources, but I am afraid it's all I know how to do at the moment :D
Title: Re: Shockwave's pathetic learning attempts thread...
Post by: benny! on March 15, 2007
Hey Shocky.

Well done and congratulations. Arrays are an important step. Same with loops.
I guess in the beginning you are satisfied with the fact that the code runs. But
may I point out a little thing I would do differently.

You define the loopcounter lp before the actual loop. I would do define it
within the for - instruction like this :

for ( int lp = 0 ; lp<5 ; lp++ ) ...

This has the following reason...

First, I think it looks smarter. But second - and this is more important - when you
define the variable lp before the actual loop - it still exists after the loop.
That means, you still allocate the memory for it - and this may be wasty ;)

You can check this by adding the following line right after your loop has finished :

Code: [Select]
printf("lp: %d", lp );
There you see, that the lp variable is still in use and the output would be 5.

Using my way by defining the loopcounter within the for-statement, the language
C automatically frees the memory for it as soon as the loop has ended. You can
easily test that while trying to print out the value of lp after the loop when you
define it within the for-statement. You will get an error - that this variable hasnt
defined concerning the scope.

Here some pseudocode that hopefully makes it clearer ....

//Scope One

int lp;                                      // lp is defined for whole Scope One

     for ( lp = 0 ; lp < 5 ; lp ++ ) { // Scope Two
         int i = 0;                         // i is only defined for Scope Two
     ...
     }

// Scope One
Title: Re: Shockwave's pathetic learning attempts thread...
Post by: Shockwave on March 15, 2007
Got it :D Thanks mate I never knew that trick.
Title: Re: Shockwave's pathetic learning attempts thread...
Post by: Emil_halim on March 15, 2007

nice to see you shocky , learnning C/C++ language , step by step is your successefull way.

so you are doing better.

enjoy c++ coding.
Title: Re: Shockwave's pathetic learning attempts thread...
Post by: Shockwave on March 15, 2007
Thanks Emil :) I am finding it pretty tedious going at the moment, not difficult but I know I am going to find it boring until I get my first stuff moving around on screen.

But I know it's a  process I need to go through as some of the help I've had here proves, so I am going slowly and listening carefully :)
Title: Re: Shockwave's pathetic learning attempts thread...
Post by: Emil_halim on March 15, 2007

That means, you still allocate the memory for it - and this may be wasty ;)


@benny

I am afraid , this is not correct,  putting the variable inside the for scope is good and will prevent access of it by accident, but will not  deallocate the memory for it as you said.

Look  each C/C++ function will allocate all auto variables in the stack at one time , I.E in the begging and free this memory when exit and return to it’s caller. 
Title: Re: Shockwave's pathetic learning attempts thread...
Post by: Jim on March 15, 2007
Also, if you try to build that with most current C compilers, the variable will still be in scope after the for loop anyway.  (C99 and C++ compilers behave the way you say they do).

Jim
Title: Re: Shockwave's pathetic learning attempts thread...
Post by: mike_g on March 15, 2007
Well even if its not 100% correct, it answers something that I once wondered about.

I started declaring ints for for loops inside the brackets cos it saved out a line of code. I never knew there was a difference until I noticed that when you wanted to use the same variable twice for 2 different loops (in dev c++) like this you had to declare it in both instances. Dint know why that was till now.
Title: Re: Shockwave's pathetic learning attempts thread...
Post by: Clyde on March 15, 2007
btw, as for being Pathetic attempts, we all have to start somewhere. And welldone on your progress dude.
Title: Re: Shockwave's pathetic learning attempts thread...
Post by: benny! on March 15, 2007
@Emil_halim:
Right. That's what I meant. So when you define the looppointer variable in the,
lets call it, global scope you put it on the bottom of the stack (stacks
are organized LIFO AFAIK ). That means it remains on the stack until the program
or function ends.

PROGRAM-START

int x=0;
{
   int y=0;
  {
      int z=0;
  }
}

PROGRAMM-END


At the point where z is initialized the stack would look like that - or ?

1. z
2. y
3. x

After the first closing "}" I thought because of the LIFO organization the stack
decreases because "z" is taken "away", then "y" and then "z". So the more
variables initialized at the PROGRAM-START increases the STACK-SPACE ???

Please correct me if I am wrong - didn't touch C for some time and with Java
Garbage Collector I don't really think about STACK/HEAP/MEMORY and stuff.
Title: Re: Shockwave's pathetic learning attempts thread...
Post by: Jim on March 15, 2007
Quote
Please correct me if I am wrong
You're wrong  :P.  The C and C++ standards don't define anything about how automatic variables are implemented, it's up to whoever writes the compiler.  Could be a stack which grows up or down, could be dynamic allocation, could be registers, could be encoded in the positions of the stars.  It probably works how you say, but it doesn't have to.

Jim
Title: Re: Shockwave's pathetic learning attempts thread...
Post by: benny! on March 16, 2007
@Jim:

Well, this is at least what I remember from my C-courses from University. Neverthe-
ess, I am no C-guru - but what I found about stack's and so on seems to be correct.

E.g. : http://en.wikipedia.org/wiki/Stack-based_memory_allocation (http://en.wikipedia.org/wiki/Stack-based_memory_allocation)

A good point you mentioned that C / C++ doesn't define any standards about how the compiler
internally handles the allocation for variables. But at this point it would be nice to know
how the big player in C compiler handle this. ( Everyone knows there is always an exception
from the rule ).
Title: Re: Shockwave's pathetic learning attempts thread...
Post by: Jim on March 16, 2007
FWIW I think every compiler I've ever used uses a stack that grows down, exactly as you describe, but that doesn't stop a compiler optimiser moving stack variables into registers and hence not using the stack at all.

Jim
Title: Re: Shockwave's pathetic learning attempts thread...
Post by: benny! on March 16, 2007
@Jim:

Right. Since stack is very limited memory I guess there is a lot optimising internally
going on. Nevertheless. Let's say defining the loopcounter inside the for-statement
looks at least more pretty ;)
Title: Re: Shockwave's pathetic learning attempts thread...
Post by: Shockwave on March 16, 2007
Saves on space in the Ide at least, but seriously now because I want to learn this the right way, would either of these methods cause code to fail on some machines?
Title: Re: Shockwave's pathetic learning attempts thread...
Post by: Jim on March 16, 2007
It'll only cause a problem if you want to compile your code as C instead of C++.  If you compile it in C and use the same variable name inside a single function then it won't compile.
eg.
Code: [Select]
void ImOKinCPPbutNOTinC(void)
{
 for (int x = 0; x < 5; x++)
 {
 }
 for (int x = 0; x < 5; x++)
 {
 }
}
won't build in C.

C isn't a subset of C++, though a lot of people think it is.  They're different languages, so you need to know which one you're coding for.

Jim

edit:
In fact, even
Code: [Select]
void ImOKinCPPbutNOTinC(void)
{
 for (int x = 0; x < 5; x++)
 {
 }
}
will only compile with C99 which barely any compilers support yet.
Title: Re: Shockwave's pathetic learning attempts thread...
Post by: benny! on March 16, 2007
@SW:

I just looked through some old plain ANSI C guides and they all do it
like you did it first, meaning initializing the loopcounter before the actual
for-statement.

So, that could be mean, that my way could causes some errors with some old
ANSI C compiler (not tested - just guessing).

Nevertheless, in all C++ guides and from what I learned nowadays (with C++/Java)
it seems to be rather common to initialize the counter within the for-statement.

All in all, it is important as a programmer to know the different scopes of variables
(concerning the compiler you use). And like Emil says it may help you in a cleaner
coding way and maybe in a better structure/encapsulation of your programs.

Guess you can never be sure that every C/C++ code runs without any modifications
on every C/C++ compiler.

//EDIT: oops, jim was faster  :'(
Title: Re: Shockwave's pathetic learning attempts thread...
Post by: Shockwave on March 16, 2007
Thank you guys!

Title: Re: Shockwave's pathetic learning attempts thread...
Post by: Emil_halim on March 16, 2007
 Just for the sake of clearance ,  for the C++  beginner , ignore that where the C++ compiler will emit the variables in the stack or in the heap memory.

Always any simple variable such as loop counter , declare it inside the usage scope, any other variables such as classes , use the new keyword that will allocate a heap memory for that variable and will automatically free when the program end.   
Title: Re: Shockwave's pathetic learning attempts thread...
Post by: Jim on March 16, 2007
Totally.
Title: Re: Shockwave's pathetic learning attempts thread...
Post by: Hotshot on March 16, 2007
Quote
Java
Garbage Collector I don't really think about STACK/HEAP/MEMORY and stuff.

That correct as I did java in college before.

Shocky....someone got start somewhere boring stuff to mega mind blowing demo(that will come to you as u learn along)

good luck shocky as I know that you can code in multi language without no problem....if u ever get stuck...there always jim and other coder who know c++  :clap:

cheers
Title: Re: Shockwave's pathetic learning attempts thread...
Post by: Shockwave on March 16, 2007

good luck shocky as I know that you can code in multi language without no problem....if u ever get stuck...there always jim and other coder who know c++  :clap:


You're right there.. We're really spoilt here :) Thanks to everybody for thier help so far in this, despite it all being simple stuff at the moment I am enjoying what I am doing for sure.
Title: Re: Shockwave's pathetic learning attempts thread...
Post by: Emp on April 07, 2007
Have been looking through this thread and I think, if everyone is still learning/progressing, that I'll tag along. I used C++ a little a few years back when I was involved in, for better or worse, some things  :|| but have gracefully forgotten absolutely everything useful.

I'm a regular PHP developer so the syntax is similar for me. Have been looking through the DX SDK for the past few days and grabbed a couple of eBooks. So will be interesting to see what's possible at a basic level ;)


Using VS 2005.
Title: Re: Shockwave's pathetic learning attempts thread...
Post by: benny! on April 07, 2007
@Emp:
Cool. Keep us informed about your progress ... and good luck!
Title: Re: Shockwave's pathetic learning attempts thread...
Post by: Shockwave on April 07, 2007
Nice to know that someone else is following this thread Emp, it's not dead, I will be posting more of my shit in here soon :) I've had to take a little bit of a break from it for a couple of weeks because of other stuff.

I've been reading up on functions and classes, still really basic stuff, I've just not booted up C++ for a while. Please do feel free to tag along, hopefully I'll learn something more from you too.
Title: Re: Shockwave's pathetic learning attempts thread...
Post by: benny! on April 08, 2007
Right, Shocky. I was also wondering when you post your next steps. Don't give
it up! The IDE is running, you successfully wrote your first programms - don't
stop it now!!!