Tuesday, December 22, 2009

Colored Output in C++ Console

Greetings!

I know I'm a day late, but there was a family emergency yesterday.

Our next topic is colored output in C++.
Have you ever wanted to get away from the boring white text on black background in console output? Well, I have an easy solution for you ;)

There is a wonderful function called SetConsoleTextAttribute. This function can set the output handle and the colors of the output (background and foreground).

It is a very easy function to use. Here is an example of the function:

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 
BACKGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);

First, the function keeps the output to the standard output (cout), then come the colors. There are three options for color in both foreground and background.

BACKGROUND_RED

BACKGROUND_GREEN

BACKGROUND_BLUE

FOREGROUND_RED

FOREGROUND_GREEN

FOREGROUND_BLUE


Use these as flags in the second part of the function's parameters, seperated by vertical lines (Shift+\).

If you are changing the background or foreground colors, you must also put:

FOREGROUND_INTENSITY
or
BACKGROUND_INTENSITY

depending on which color you are changing. The example function above changes the output to a white background (using all colors), with the "default" foreground (black). The color defaults to black when you do not choose it, if you use this function.

I used this function plenty to make a simulation program that included quizzes. I changed the colors of the incorrect or correct answers to visually stimulate the user.

One more thing: You need to include the windows.h header for this code to work. Use the following line in your code:


#include "windows.h"

I will upload test code when I can and link it to this post.


Until then,
Happy Coding!
kieblera5

Monday, December 14, 2009

Intro to HTML with notepad

It's very interesting how a notepad utility can serve as an editor for your website or blog.
Kiebler5 did a great job by describing the various uses of "namespace" in c++; However, this intro to HTML is intended to give beginners a feel of what programming looks like. (ps. HTML is not a programming laguage, it is a markup language)

HTML is a language for describing web pages.

* HTML stands for Hyper Text Markup Language
* A markup language is a set of markup tags
* HTML uses markup tags to describe web pages

I will show you how to create and run a simple html page. [click on images to enlarge]
Step. 1) Open notepad.
(Quick way to open notepad - hit Ctrl + R - and type "notepad" in the dialog box and hit "OK".)
Snapshot 1:
Step. 2) Type your own HTML code or copy this code into your notepad.
Screenshot 2:
Step. 3) Save the txt file as a .html format. This step is very important. This is because without the HTML extension your operating system may detect the file as a txt file.
In snapshot 3, I saved my html code as "simplePage.html".
Screenshot 3:
Step. 4) Locate your and run your html file.
Screenshot 4:
Next, Intro to CSS.
Until then,
Happy Coding
Verse316

Namespaces in C++

So... The first topic that I would like to discuss the topic of namespaces in C++. What is a namespace? Well, it's a way to group classes, objects and functions all under one name.

For instance:

namespace myNamespace
{
int x;
}


To access the variable inside the namespace, we can use the code namespace_name::variable_name:



myNamespace::x

So, now that you know somewhat about namespaces, let's get to the reason that I brought this up. Certain C++ programmers, when using a cout statement, like to use:



std::cout<<"Hello World!\n";

They put the namespace tag of std in front of the cout. A better way to do this is to say at the beginning of the code:



using namespace std;
//more code here
cout<<"Hello World!\n";

This way, you don't need the std tag. You could see how many extra strokes it would take to put std:: in front of every cin and cout? So, why do people do it? Well, the only real reason is that when you say "using namespace std;", you are including EVERYTHING in the std namespace. Maybe you don't want everything to be included. You could always say:



using std::cout;

This would replace putting the std:: tag in front of every cout; however, you wouldn't be using the entire namespace and then you wouldn't have to worry about possible class name-clashing in the future. See if there is something already named, you can't make a class with the same name yourself. For instance, if you try to make a Time class, it might not work depending on your include statements. I had this problem once and had to rename the class.

Other good things from namespaces to include: There are a lot of different things in the std namespace that can all be found at this link: http://www.cplusplus.com/reference/

Every one of those items is included when you use the std namespace. The best things, in my opinion, are the functions in cctype. If you include cctype with the following code:



#include <cctype>

,then you can have access to functions like toupper, tolower, isalpha, isdigit, etc. These functions can tell you about characters and return if the character is a number, letter, space, uppercase, lowercase, etc. and can also change a character from a lower to an upper and vice-versa.

Well, there you have it. That's the low-down on namespaces. Remember, you can create your own namespaces! Just follow the code outlined near the beginning. And if you are one of those people that use std:: before every cout and cin, start using "using std::cout" and "using std::cin" at the beginning of your code after the includes.

Next update from me? Changing colors in standard C++ console output :)

Until then,
Happy Coding,
kieblera5

Welcome!

#include "Blogger.h"

using quickIntro lol;

int codingMonday()
{
cout << "Welcome to codingMonday!!!" << endl;
cout << "Brief History: CodingMonday is a day created by two bored college students that happen to like programming\n";
cout << "Each Monday, we will discuss a random, yet useful, topic on C++, C#, or any other language or technology"<< endl;
cout << "These topics will be accompanied with explanations and source code files for you, the reader, so that you can see the actual code instead of just talk and theory.\n";

return 0;

}

Enjoy,

Verse316 and kieblera5