Results 1 to 26 of 26

Thread: New to C++. Help in functions, subs, and no MFC. Where do i start?

  1. #1

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500

    Red face

    Hello
    I am new to VC++, but i am quite interested in programming. I did a few examples from books, butnthey always made me use the MFC, and they did not explaint to me how to make functions, sub, and then call them. If some one could explain that to me, that would be great, and also how to write a c++ program that is blank(like a new project in VB), but that does not run as a console, but as a usual windows program. thanx a lot

    P.S.: I only use VC++
    Amon Ra
    The Power of Learning.

  2. #2
    Addicted Member
    Join Date
    Jan 2001
    Location
    Little Rock, Ar
    Posts
    151

    Exclamation

    http://newdata.box.sk/bx/c/

    I sugest you start with the console. At least to you get the hang of functions & loops in C.

    Functions are done like this

    Code:
    void vFunction(int pram1,int pram2)// like a sub returns nothing
    {
    }
    
    int iFunction(int pram1) // function returns value
    {	
    	return(pram1);
    }
    
    void main()// always required
    {
    	int a;
    
    	vFunction(1,2);
    	
    	a = iFunction(1);
    
    	iFunction(4);
    }

  3. #3
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    If youj are a begginer don't start with windows programming. Learn C++ good first by experimrnting with console apps.

    Thare are no subs in C++. Subs are really void functions (which don't return a value).

    void mysub()
    {//do something}
    //no value is returned]

    With out MFC you can't draw controls on a dialog. You need to create them using CreateWindowEx API Function.

    Actually you can draw controls if you are using resources.
    Here is a sample of how to make a window using raw API.
    Code:
    1. open visual c++
    2. File > New
    3. Click Projects tab
    4. click Win32 Application and give it a name
    5. click finish
    6. click the File View Tab on TreeView
    7. go to File > New
    8. in the first tab Click C++ Source File and name it
    9. Copy and paste the code in the new file
    10. right click the Header folder and add Windows.h
    11. click execute
    
    #include <windows.h>
    LRESULT CALLBACK WindowFunc(HWND, UINT, WPARAM, LPARAM);
    char szWinNamw[] = "MyWin";
    int WINAPI WinMain(HINSTANCE hThisInst, HINSTANCE hPrevInst,
    				LPSTR kposzArgs, int nWinMode)
    
    
        {
        	HWND hwnd;
        	MSG msg;
        	WNDCLASSEX wcl;
        	wcl.cbSize = sizeof(WNDCLASSEX);
        	wcl.hInstance = hThisInst;
        	wcl.lpszClassName = "My Window";
        	wcl.lpfnWndProc = WindowFunc;
        	wcl.style = 0;
        	wcl.hIcon = LoadIcon(NULL, IDI_APPLICATION);
        	wcl.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
        	wcl.hCursor = LoadCursor(NULL, IDC_ARROW);
        	wcl.lpszMenuName = NULL;
        	wcl.cbClsExtra = 0;
        	wcl.cbWndExtra = 0;
        	wcl.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
        	if(!RegisterClassEx(&wcl)) return 0;
        	hwnd = CreateWindow(
        		"My Window",
        		"Skeleton Window",
        		WS_OVERLAPPEDWINDOW,
        		CW_USEDEFAULT,
        		CW_USEDEFAULT,
        		CW_USEDEFAULT,
        		CW_USEDEFAULT,
        		HWND_DESKTOP,
        		NULL,
        		hThisInst,
        		NULL
        		);
        	ShowWindow(hwnd, nWinMode);
        	UpdateWindow(hwnd);
        	while(GetMessage(&msg, NULL, 0, 0))
    
    
            	{
            		TranslateMessage(&msg);
            		DispatchMessage(&msg);
            	}
            	return msg.wParam;
        }
        LRESULT CALLBACK WindowFunc(HWND hwnd, UINT message,
        							WPARAM wParam, LPARAM lParam)
    
    
            {
    
    
                	switch(message)	{
                	case WM_DESTROY:
                		PostQuitMessage(0);
                		break;
                	default:
                		return DefWindowProc(hwnd, message, wParam, lParam);
                	}
                	return 0;
            }
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  4. #4
    Guest
    Here is a simple DOS i/o program. I used stdio functions instead of iostream.
    Code:
    // File that contains the standard i/o features. You can
    // use the file iostream.h, (which will be a little easier,)
    #include "stdio.h"
    
    
    void main()
    {
    	// Declare a string variable (used to hold our name)
    	char* cName;
    
    	// Print a message to the console window using printf()
    	printf("Hello, what is your name? ");
    
    	// Recieve input from the user using scanf()
    	// we are storing it in the variable called cName
    	scanf("%s",cName);
    	
    	// Output our data to the console window. %s tells us that
    	// the variable cName should be in its place. \n simply tells
    	// the compiler to go to a new line
    	printf("\nHello %s\n", cName);
    
    }

  5. #5
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    stdio is a system header. It should be in angle brackets.
    PHP Code:
    #include <stdio.h> 
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Also, you need to allocate a buffer rather than just a random pointer.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  7. #7

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500

    Thumbs up Help

    Thanks guys, i will try that. thanx for helping
    Amon Ra
    The Power of Learning.

  8. #8

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500

    Question LOL :)

    How do i start a console in VC++?? Which option do i select?
    Amon Ra
    The Power of Learning.

  9. #9
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Win32 Console Application.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  10. #10

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500

    Talking OK

    ok, thanx a lot
    Amon Ra
    The Power of Learning.

  11. #11

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500

    Question to start

    I selected Win32 console application, but there are no files in the project. what should i add to write a small console?
    Amon Ra
    The Power of Learning.

  12. #12
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Add a new .cpp file, and add the following code:
    Code:
    #include <iostream.h> // Allows you to use input/output
    
    void main() { // All console programs must have this function
        cout << "Hello World!" << endl;
    }
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  13. #13

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500

    Lightbulb Fantastic :) :)

    thanx. I just created my first C++ prog
    Next, how do i declare variables? Are the types the same as the ones in VB? thanx for the help , parksie
    appreciate
    Amon Ra
    The Power of Learning.

  14. #14
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    To declare a variable you just use:
    Code:
    type variablename;
    ...as in:
    Code:
    char mychar; // 1 byte
    short myshort; // 2 bytes
    int myint; // 4 bytes
    long mylong; // Also 4 bytes
    float myfloat; // Same as a VB single
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  15. #15

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500

    Wink Thx

    Cool! Hmmm, do you have an idea of what i could try to do as practice?
    Amon Ra
    The Power of Learning.

  16. #16

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500

    Question ?

    Where do i declare the variables? is there a public or private declaration?
    Amon Ra
    The Power of Learning.

  17. #17
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    You can declare them anywhere

    There's no public/private as such. For example:
    Code:
    void MyFunction() {
        int x;
    }
    
    void Other() {
        x = x + 1;
    }
    This is not allowed, since x is out of scope, because it's in a different set of braces. However, this would work:
    Code:
    int x;
    
    void MyFunction() {
        x = x + 2;
    }
    
    void Other() {
        x = x + 1;
    }
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  18. #18

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500

    Talking k

    OK thanx!
    Hmmm, so u can declare anywhere. cool. Btw, what are the operators?
    Amon Ra
    The Power of Learning.

  19. #19
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    +, -, (), *, /, % (Mod), ^ (Xor), & (And), | (Or)
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  20. #20

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500

    thanx

    thanx
    Amon Ra
    The Power of Learning.

  21. #21
    Addicted Member substring's Avatar
    Join Date
    Feb 2001
    Posts
    148

    Smile

    Amon,

    It might be easier for you to start with File|New, then select the Files tab, and then select C++ Source File. It will take you to the IDE with a blank page for you to type in codes. When you are ready to build, the program will ask you to set up a default project workplace. All you have to do is to type in a name with the cpp extension, and voila.

    This might be less confusing to you than doing the File|New|Projects.

    Hope this helps.

    substring.

  22. #22

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500

    Thumbs up Great

    Thanx for the tip. You ar eright, it might be easier like that.
    Amon Ra
    The Power of Learning.

  23. #23
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169

    Re: Now that I have got it, Can I ask some questions.

    Originally posted by Wak
    What does this bit mean?????

    LRESULT CALLBACK WindowFunc(HWND, UINT, WPARAM, LPARAM);
    char szWinNamw[] = "MyWin";

    I understand it a little

    And that's it. Thanx
    WindowFunc is a prototype for your event handler's function, and the other is just a definition of a string array.

    To make it a grey background you need to specify the correct brush when you populate your WNDCLASSEX structure. What are you using at the moment?
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  24. #24
    Hyperactive Member Wak's Avatar
    Join Date
    Nov 2000
    Location
    Brisbane, Queensland
    Posts
    298

    Unhappy OK THEN, HOW BOUT THIS?

    I know that if I put GRAY_BRUSH in there, it'll make the form gray, but how would I go about making it the standard colour, according to the settings, that each person has?? You know.. the convetional way. I'm retarded. Thanx
    Visual Basic 6.0 Enterprise
    Visual C++ 6.0 Professional

    Wak

  25. #25
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Try using COLOR_BTNFACE.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  26. #26
    Hyperactive Member Wak's Avatar
    Join Date
    Nov 2000
    Location
    Brisbane, Queensland
    Posts
    298

    thanx

    I figured out from your post that I could use COLOR_APPWORKSPACE. thant again
    Visual Basic 6.0 Enterprise
    Visual C++ 6.0 Professional

    Wak

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width