Author Topic: aken gets a grasp on coding  (Read 3166 times)

0 Members and 1 Guest are viewing this topic.

Offline akenbosch

  • Pretentious Noob
  • 29
  • doesent care about canon
aken gets a grasp on coding
yep, i decided to give coding a try. my first try yeilds this:
Code: [Select]
#include <stdlib.h>
#include <stdio.h>

int main()
{
    int age;
    int number;
    int combon;
    char agev [20];
    char noun [20];
    char numberv [20];
    char dood [20];

    puts("follow onscreen instructions and receive a username");
    puts("enter a noun: ");
    scanf("%s",noun);
    puts("enter a number (in numeral(s)");
    scanf("%s",numberv);
    number=atoi(numberv);
    puts("enter your age (in numbers): ");
    scanf("%s",agev);
    age=atoi(agev);
    combon=age*number;
    printf("%s%d is you new username",noun,combon);
    scanf("%s",dood);

    return(0);
}

this compiles fine, so i got more confident and tried a text editor:

--------------
base file (.c)
--------------


#include <windows.h>
#pragma hdrstop

#include "Main.h"

static char g_szClassName[] = "MyWindowClass";
static HINSTANCE g_hInst = NULL;

#define IDC_MAIN_TEXT   1001

BOOL LoadFile(HWND hEdit, LPSTR pszFileName)
{
   HANDLE hFile;
   BOOL bSuccess = FALSE;

   hFile = CreateFile(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
      OPEN_EXISTING, 0, 0);
   if(hFile != INVALID_HANDLE_VALUE)
   {
      DWORD dwFileSize;
      dwFileSize = GetFileSize(hFile, NULL);
      if(dwFileSize != 0xFFFFFFFF)
      {
         LPSTR pszFileText;
         pszFileText = (LPSTR)GlobalAlloc(GPTR, dwFileSize + 1);
         if(pszFileText != NULL)
         {
            DWORD dwRead;
            if(ReadFile(hFile, pszFileText, dwFileSize, &dwRead, NULL))
            {
               pszFileText[dwFileSize] = 0; // Null terminator
               if(SetWindowText(hEdit, pszFileText))
                  bSuccess = TRUE; // It worked!
            }
            GlobalFree(pszFileText);
         }
      }
      CloseHandle(hFile);
   }
   return bSuccess;
}

BOOL SaveFile(HWND hEdit, LPSTR pszFileName)
{
   HANDLE hFile;
   BOOL bSuccess = FALSE;

   hFile = CreateFile(pszFileName, GENERIC_WRITE, 0, 0,
      CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
   if(hFile != INVALID_HANDLE_VALUE)
   {
      DWORD dwTextLength;
      dwTextLength = GetWindowTextLength(hEdit);
      if(dwTextLength > 0)// No need to bother if there's no text.
      {
         LPSTR pszText;
         pszText = (LPSTR)GlobalAlloc(GPTR, dwTextLength + 1);
         if(pszText != NULL)
         {
            if(GetWindowText(hEdit, pszText, dwTextLength + 1))
            {
               DWORD dwWritten;
               if(WriteFile(hFile, pszText, dwTextLength, &dwWritten, NULL))
                  bSuccess = TRUE;
            }
            GlobalFree(pszText);
         }
      }
      CloseHandle(hFile);
   }
   return bSuccess;
}

BOOL DoFileOpenSave(HWND hwnd, BOOL bSave)
{
   OPENFILENAME ofn;
   char szFileName[MAX_PATH];

   ZeroMemory(&ofn, sizeof(ofn));
   szFileName[0] = 0;

   ofn.lStructSize = sizeof(ofn);
   ofn.hwndOwner = hwnd;
   ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0\0";
   ofn.lpstrFile = szFileName;
   ofn.nMaxFile = MAX_PATH;
   ofn.lpstrDefExt = "txt";

   if(bSave)
   {
      ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY |
         OFN_OVERWRITEPROMPT;
         
      if(GetSaveFileName(&ofn))
      {
         if(!SaveFile(GetDlgItem(hwnd, IDC_MAIN_TEXT), szFileName))
         {
            MessageBox(hwnd, "Save file failed.", "Error",
               MB_OK | MB_ICONEXCLAMATION);
            return FALSE;
         }
      }
   }
   else
   {
      ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
      if(GetOpenFileName(&ofn))
      {
         if(!LoadFile(GetDlgItem(hwnd, IDC_MAIN_TEXT), szFileName))
         {
            MessageBox(hwnd, "Load of file failed.", "Error",
               MB_OK | MB_ICONEXCLAMATION);
            return FALSE;
         }
      }
   }
   return TRUE;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
   switch(Message)
   {
      case WM_CREATE:
         CreateWindow("EDIT", "",
            WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL | ES_MULTILINE |
               ES_WANTRETURN,
            CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
            hwnd, (HMENU)IDC_MAIN_TEXT, g_hInst, NULL);

         SendDlgItemMessage(hwnd, IDC_MAIN_TEXT, WM_SETFONT,
            (WPARAM)GetStockObject(DEFAULT_GUI_FONT), MAKELPARAM(TRUE, 0));
      break;
      case WM_SIZE:
         if(wParam != SIZE_MINIMIZED)
            MoveWindow(GetDlgItem(hwnd, IDC_MAIN_TEXT), 0, 0, LOWORD(lParam),
               HIWORD(lParam), TRUE);
      break;
      case WM_SETFOCUS:
         SetFocus(GetDlgItem(hwnd, IDC_MAIN_TEXT));
      break;
      case WM_COMMAND:
         switch(LOWORD(wParam))
         {
            case CM_FILE_OPEN:
               DoFileOpenSave(hwnd, FALSE);
            break;
            case CM_FILE_SAVEAS:
               DoFileOpenSave(hwnd, TRUE);
            break;
            case CM_FILE_EXIT:
               PostMessage(hwnd, WM_CLOSE, 0, 0);
            break;
            case CM_ABOUT:
               MessageBox (NULL, "File Editor for Windows !\n Using the Win32 API" , "About...", 0);
         }
      break;
      case WM_CLOSE:
         DestroyWindow(hwnd);
      break;
      case WM_DESTROY:
         PostQuitMessage(0);
      break;
      default:
         return DefWindowProc(hwnd, Message, wParam, lParam);
   }
   return 0;
}


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
   LPSTR lpCmdLine, int nCmdShow)
{
   WNDCLASSEX WndClass;
   HWND hwnd;
   MSG Msg;

   g_hInst = hInstance;

   WndClass.cbSize        = sizeof(WNDCLASSEX);
   WndClass.style         = 0;
   WndClass.lpfnWndProc   = WndProc;
   WndClass.cbClsExtra    = 0;
   WndClass.cbWndExtra    = 0;
   WndClass.hInstance     = g_hInst;
   WndClass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
   WndClass.hCursor       = LoadCursor(NULL, IDC_ARROW);
   WndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
   WndClass.lpszMenuName  = "MAINMENU";
   WndClass.lpszClassName = g_szClassName;
   WndClass.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

   if(!RegisterClassEx(&WndClass))
   {
      MessageBox(0, "Window Registration Failed!", "Error!",
         MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
      return 0;
   }

   hwnd = CreateWindowEx(
      WS_EX_CLIENTEDGE,
      g_szClassName,
      "A File Program",
      WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, CW_USEDEFAULT, 320, 240,
      NULL, NULL, g_hInst, NULL);

   if(hwnd == NULL)
   {
      MessageBox(0, "Window Creation Failed!", "Error!",
         MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
      return 0;
   }

   ShowWindow(hwnd, nCmdShow);
   UpdateWindow(hwnd);

   while(GetMessage(&Msg, NULL, 0, 0))
   {
      TranslateMessage(&Msg);
      DispatchMessage(&Msg);
   }
   return Msg.wParam;
}


------------
header file (.h)
------------------


#define CM_FILE_SAVEAS 9072
#define CM_FILE_EXIT 9071
#define CM_FILE_OPEN 9070
#define CM_ABOUT        9069

-----------------------------------
.rc file, since im using devc++
---------------------




MAINMENU MENU
{
 POPUP "&File"
 {
  MENUITEM "&Open...", CM_FILE_OPEN
  MENUITEM "Save &As...", CM_FILE_SAVEAS
  MENUITEM SEPARATOR
  MENUITEM "E&xit", CM_FILE_EXIT
 }

 POPUP "&Help"
 {
  MENUITEM "&About", CM_ABOUT
 }

}
500 ICON MOVEABLE PURE LOADONCALL DISCARDABLE "C:/Program Files/c++/Icon/Mainicon.ico"
------------------------------------------------------
i think im ready too move on to fsscp (with a different compiler of course).


but too complete my first goal...

where is the "swivel thruster" code that makes the territary thrusters move?
« Last Edit: September 03, 2007, 09:15:39 pm by Flipside »

Burn the sucker out of the sky!
EAT PHOTONS INFIDEL! MAY THE HEAT OF A THOUSAND SUNS CONSUME YOU! :mad2:


snail gives a debriefing: http://www.hard-light.net/forums/index.php/topic,48825.msg991954.html#msg991954

 

Offline CP5670

  • Dr. Evil
  • Global Moderator
  • 212
Re: aken gets a grasp on coding
:lol:

This should get interesting...

 

Offline Mobius

  • Back where he started
  • 213
  • Porto l'azzurro Dolce Stil Novo nella fantascienza
    • Skype
    • Twitter
    • The Lightblue Ribbon | Cultural Project
Re: aken gets a grasp on coding
Of course! I want to see the results ;7
The Lightblue Ribbon

Inferno: Nostos - Alliance
Series Resurrecta: {{FS Wiki Portal}} -  Gehenna's Gate - The Spirit of Ptah - Serendipity (WIP) - <REDACTED> (WIP)
FreeSpace Campaign Restoration Project
A tribute to FreeSpace in my book: Riflessioni dall'Infinito

 

Offline Hades

  • FINISHING MODELS IS OVERRATED
  • 212
  • i wonder when my polycounts will exceed my iq
    • Skype
    • Steam
Re: aken gets a grasp on coding
Of course! I want to see the results ;7


This will be interesting.Will it kill your computer, or will it work and you will know how to code; we will find out in a few hours/days. ;7
[22:29] <sigtau> Hello, #hard-light?  I'm trying to tell a girl she looks really good for someone who doesn't exercise.  How do I word that non-offensively?
[22:29] <RangerKarl|AtWork> "you look like a big tasty muffin"
----
<batwota> wouldn’t that mean that it’s prepared to kiss your ass if you flank it :p
<batwota> wow
<batwota> KILL

 

Offline phreak

  • Gun Phreak
  • 211
  • -1
Re: aken gets a grasp on coding
code tags for great justice!
Offically approved by Ebola Virus Man :wtf:
phreakscp - gtalk
phreak317#7583 - discord

 

Offline Nuke

  • Ka-Boom!
  • 212
  • Mutants Worship Me
Re: aken gets a grasp on coding
i havent a clue about coding yet i got a working 3d engine to compile and run. go figure :D
I can no longer sit back and allow communist infiltration, communist indoctrination, communist subversion, and the international communist conspiracy to sap and impurify all of our precious bodily fluids.

Nuke's Scripting SVN

 

Offline Hippo

  • Darth water-horse
  • 211
  • Grazing.
    • All Hands to War
Re: aken gets a grasp on coding
This is going to sound hash, because it is meant to be.

DO NOT plagiarize or blatantly copy code from other sites and assume that this means you know how to decently code.

DO NOT take a program you already poorly wrote, change it around so it works, repost it on a different username five and a half months later, and then put blatantly copied code beneath it and try to pass off that you know how to decently code.

DO go away.


That is all.


For people who prefer a graphical representation, please see here for a side by side comparison where minor changes are easily notable.
« Last Edit: September 03, 2007, 09:12:21 pm by Hippo »
VBB Survivor -- 387 Posts -- July 3 2001 - April 12 2002
VWBB Survivor -- 100 Posts -- July 10 2002 - July 10 2004

AHTW

 

Offline Flipside

  • əp!sd!l£
  • 212
Re: aken gets a grasp on coding
Right, locked.

I did wonder how he managed to suddenly learn correct variable naming in the space of 2 programs.

Edit : In fact, for the first time, I'm going to request a timed banning for this, which annoys me because I've never felt I had to ask for one of those for any member before now.
« Last Edit: September 03, 2007, 09:35:45 pm by Flipside »

 

Offline Goober5000

  • HLP Loremaster
  • Moderator
  • 214
    • Goober5000 Productions
Re: aken gets a grasp on coding
Ugh.  Granted.  And title intensified.