Author Topic: Shockwave's pathetic learning attempts thread...  (Read 20109 times)

0 Members and 1 Guest are viewing this topic.

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17426
  • Karma: 499
  • evil/good
    • View Profile
    • My Homepage
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;
}
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: Shockwave's pathetic learning attempts thread...
« Reply #1 on: March 12, 2007 »
Helps me, thanks.

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17426
  • Karma: 499
  • evil/good
    • View Profile
    • My Homepage
Re: Shockwave's pathetic learning attempts thread...
« Reply #2 on: March 12, 2007 »
I used the stdio.h instead of iostream, stdio.h seems a lot smaller. This compiles to about 15kb. :)
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: Shockwave's pathetic learning attempts thread...
« Reply #3 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.

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17426
  • Karma: 499
  • evil/good
    • View Profile
    • My Homepage
Re: Shockwave's pathetic learning attempts thread...
« Reply #4 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

Shockwave ^ Codigos
Challenge Trophies Won:

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: Shockwave's pathetic learning attempts thread...
« Reply #5 on: March 12, 2007 »
Ah right, that might explain why it's not working here.

Offline Paul

  • Pentium
  • *****
  • Posts: 1490
  • Karma: 47
    • View Profile
Re: Shockwave's pathetic learning attempts thread...
« Reply #6 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++
« Last Edit: March 13, 2007 by Paul »
I will bite you - http://s5.bitefight.se/c.php?uid=31059
Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17426
  • Karma: 499
  • evil/good
    • View Profile
    • My Homepage
Re: Shockwave's pathetic learning attempts thread...
« Reply #7 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.
Shockwave ^ Codigos
Challenge Trophies Won:

Offline mike_g

  • Amiga 1200
  • ****
  • Posts: 435
  • Karma: 34
    • View Profile
Re: Shockwave's pathetic learning attempts thread...
« Reply #8 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  :P
« Last Edit: March 13, 2007 by mike_g »

Offline TinDragon

  • Pentium
  • *****
  • Posts: 644
  • Karma: 24
    • View Profile
    • J2K's blog
Re: Shockwave's pathetic learning attempts thread...
« Reply #9 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 :)

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17426
  • Karma: 499
  • evil/good
    • View Profile
    • My Homepage
Re: Shockwave's pathetic learning attempts thread...
« Reply #10 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 :)
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Shockwave's pathetic learning attempts thread...
« Reply #11 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
Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17426
  • Karma: 499
  • evil/good
    • View Profile
    • My Homepage
Re: Shockwave's pathetic learning attempts thread...
« Reply #12 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.
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17426
  • Karma: 499
  • evil/good
    • View Profile
    • My Homepage
Re: Shockwave's pathetic learning attempts thread...
« Reply #13 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.
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17426
  • Karma: 499
  • evil/good
    • View Profile
    • My Homepage
Re: Shockwave's pathetic learning attempts thread...
« Reply #14 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;
}
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Shockwave's pathetic learning attempts thread...
« Reply #15 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
Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17426
  • Karma: 499
  • evil/good
    • View Profile
    • My Homepage
Re: Shockwave's pathetic learning attempts thread...
« Reply #16 on: March 14, 2007 »
Thanks Jim :)
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17426
  • Karma: 499
  • evil/good
    • View Profile
    • My Homepage
Re: Shockwave's pathetic learning attempts thread...
« Reply #17 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..
Shockwave ^ Codigos
Challenge Trophies Won:

Offline benny!

  • Senior Member
  • DBF Aficionado
  • ********
  • Posts: 4384
  • Karma: 228
  • in this place forever!
    • View Profile
    • bennyschuetz.com - mycroBlog
Re: Shockwave's pathetic learning attempts thread...
« Reply #18 on: March 14, 2007 »
Keep your steps coming, Shocky. It's nice to see how your initial hello world
program expands.
[ mycroBLOG - POUET :: whatever keeps us longing - for another breath of air - is getting rare ]

Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17426
  • Karma: 499
  • evil/good
    • View Profile
    • My Homepage
Re: Shockwave's pathetic learning attempts thread...
« Reply #19 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..
Shockwave ^ Codigos
Challenge Trophies Won: