VB - WORLD C/C++ Language FAQ

 

Frequently asked questions for the VB-World C/C++ forum - Beta Version

 

 

Contents

1. How to write a C++ program?

2. Where do I start learning C++?

3. How to make the "Hello World" program?

4. How to convert a number to a string?

5. How to convert a string to a number?

6. How to copy one string (char array) to another?

7. How to append one string (char array) to another?

8. How to read & write to files?

9. How to use API in a C++ program?

10. How to make a window from 'scratch'?

11. Where can I get free C++ books and tutorials?

12. Author

 

1. How to write a C++ program?

 - To write a C++ program you only need a simple text editor (like Notepad). The code is written in the text editor, but to make the text file an executable file, a DLL (dynamic link library), etc. , you need a C++ compiler. There are many on the market. Microsoft Visual C++ and Borland C++ Builder are the best. They have great debuggers, lots of additional controls, but are not very cheap. Many compilers are free to download. For example: Borland C++ Compiler, DevC++, and others.

 

 

2.Where do I start learning C++?

 - Almost everything can be done in C++. Executable files (*.exe) can be made, DLL files (*.dll), static libraries (*.lib). Of course, executable files are the most common product of programming. In Windows the applications can be Win32 applications or console applications. Console applications are the best way to start learning C++. They are simple to make (just the main function), easy to experiment and the API (Application Programming Interface) can be accessed through them.

 

Note: Most people don't make difference between Windows Console Applications and DOS Applications (mostly because they look the same), but there are big differences. The Win32 API can not be accessed by a DOS application, a DOS application is 16-bit which means that integers are 16 bits long (in console applications they are 32 bits long) and most important a Console application can not run under DOS. To make 16 bit DOS applications you need a 16 bit compiler.

 

 

3. How to make the "Hello World" program?

 - Put this code in a file with a .cpp extension and compile it using a suitable compiler?

---------------------------------------------------------------------------------------------------------------------------

#include <iostream>  //preprocessor statement

using namespace std;  //choosing the namespace that we are going to use

 

int main() //this is where the program starts executing

{

    cout<<"Hello World"<<endl;

    return 0;

}

---------------------------------------------------------------------------------------------------------------------------

 

Explanation:

- #include <iostream>
Sentences that begin with a pound sign (#) are directives for the preprocessor. They are not executable code lines but indications for the compiler. The sentence #include <iostream> tells the compiler's preprocessor to include the iostream standard header file. This file includes the declarations of the basic standard input-output library in C++.

 

- int main ()
This line corresponds to the beginning of the main function declaration. The main function is the point by where all C++ programs begin their execution.

 

- cout is the standard output stream in C++ which inserts a sequence of characters (in this case "Hello World") into this output stream (the screen). cout is declared in the iostream library, so in order to be able to use it this file must be included.

 

- return 0;
The return instruction makes the main() function to finish and return the code that the instruction is followed by, in this case 0.

 

Note: The iosteram.h header file can be included (in VC++), but many other compilers wont let you do that. In this case the  'using namespace std;' line is not needed.

 

4. How to convert a number to a string?

- Use the itoa function to convert an integer to a string.

Example:

---------------------------------------------------------------------------------------------------------------------------

#include <iostream>  //preprocessor statement

#include <stdlib.h>    //contains itoa & atoi

using namespace std;  //choosing the namespace that we are going to use

 

int main() //this is where the program starts executing

{

    char buffer[20];

    int i = 3445;

    _itoa( i, buffer, 10 );

    cout<<buffer<<endl;

    return 0;

}

---------------------------------------------------------------------------------------------------------------------------

 

 

5. How to convert a string to a number?

- Use the atoi function to convert  a string to an integer.

Example:

---------------------------------------------------------------------------------------------------------------------------

#include <iostream>  //preprocessor statement

#include <stdlib.h>    //contains itoa & atoi

using namespace std;  //choosing the namespace that we are going to use

 

int main() //this is where the program starts executing

{

    char buffer[20];

    int i = 3445;

    _itoa( i, buffer, 10 );

    cout<<buffer<<endl;

    return 0;

}

---------------------------------------------------------------------------------------------------------------------------

 

 

6. How to copy one string (char array) to another?

 - Use strcpy

Example:

#include <string.h> 

#include <iostream.h>

void main( ) 

char string[80]; 

strcpy( string, "Hello world from " ); 

strcat( string, "strcpy " ); 

strcat( string, "and " ); 

strcat( string, "strcat!" ); 

cout<<string<<endl;

}

 

Output

String = Hello world from strcpy and strcat!

7. How to append one string (char array) to another?

 - See Example above.

 

 

8. How to read & write to files?

 - 

Example:

---------------------------------------------------------------------------------------------------------------------------

#include <iostream.h>
#include <stdio.h>

 

void main()

{

FILE *f;

f = fopen("//filename","r+");  //appends a file (for reading & writing) to *f

//to put a character into the file

//putc(character, Pointer to FILE structure);

putc(65,f);

 

//to read a character from the file

//getc(Pointer to FILE structure);

int ch = getc();

 

//getc returns the character that is at the file pointer location

//to change the position of the pointer use fset();

//fset(Pointer to FILE structure, Number of bytes from origin, Initial position(must be SEEK_CUR, SEEK_END or SEEK_SET))

//to set the pointer at position 10 you will do this:

fseek(f,10,SEEK_SET);

}

---------------------------------------------------------------------------------------------------------------------------

 

9. How to use API in a C++ program?

 - Just include the windows.h header file:

Example:

---------------------------------------------------------------------------------------------------------------------------

#include <windows.h>

 

void main()

{

    //use any api function        

}    

---------------------------------------------------------------------------------------------------------------------------

 

10. How to make a window from 'scratch'? 

Example:

---------------------------------------------------------------------------------------------------------------------------

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); 

 

int i; 

while(i = GetMessage(&msg, NULL, 0, 0)) 

{ if (i == -1) break; 

    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;

 }

---------------------------------------------------------------------------------------------------------------------------

 

11. Where can I get free C++ books and tutorials?

 

C/C++

http://www.cplusplus.com/
http://www.strath.ac.uk/CC/Courses/NewCcourse/ccourse.html 
http://www.aul.fiu.edu/tech/visualc.html
http://www.gator.net/~garyg/c/cref.html

Thinking In C++ - Vol.1

Thinking In C++ - Vol.2

C For Beginners

C++ Tutorial - Part I

C++ Tutorial - Part II

Summary Of Popular C Mistakes

A brief programming tutorial in C for raw sockets

Connecting To MySQL Databases With C

Teach Yourself C++ in 21 Days, Second Edition

Object Orientated Programming in ANSI-C

 

12. Author

 

Vlatko Jankuloski & everybody on the C/C++ Forums