C Programming Central
C/C++
Tutorials
FAQ
Compilers
Books
Ideas
Code Archive
Forums
Challenges
Creations
Downloads
Reviews
Contests
Site
Main
News Archive
Staff
Contact
Links
Support
Store



Best viewed with any browser

Mod Chip



Ask a question:


1. How do I change my code into a program?
2. How do I add color to my DOS console program?
3. How do I pause my program and wait until enter is pressed?
4. How do I execute another program in my program?


1. How do I change my code into a program?
You need to compile the code. You do this with a compiler, see the
compilers page.[back to top]


2. How do I add color to my DOS console program?
Add this code to the beginning of the function to use color in:

HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
WORD wOldColorAttrs;
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
GetConsoleScreenBufferInfo(h, &csbiInfo);
wOldColorAttrs = csbiInfo.wAttributes;

Then add this code to change your text color:

SetConsoleTextAttribute ( h, FOREGROUND_COLOR-HERE | FOREGROUND_INTENSITY );


Or this code to change your background:

SetConsoleTextAttribute ( h, BACKGROUND_COLOR-HERE | BACKGROUND_INTENSITY );


Colors to choose from:

RED
GREEN
BLUE

To change back to white on black (or whatever the color was when you used the first bit of code):

SetConsoleTextAttribute ( h, wOldColorAttrs);
[back to top]


3. How can I pause my program and wait until enter is pressed?
There are a few ways. First is getchar() which is in stdio.h, next is getch() in conio.h, or if yourworing in DOS you can use system("pause") in stdio.h.
[back to top]


4. How do I execute another program in my program?
To execute anotther program you use system() in stdio.h which sends a command to the operating system. For example, in DOS you would use system("program-name") in *nix you would use system("./program-name").
[back to top]