std::basic_string is nice, but it'll never replace char* myString = new char[100];. Besides, if you want to do anything with Windows, you need char buffers for a lot of it.
here is a neat little thing that draws a bunch of randomly colored characters
//required for cout
#include <iostream>
//required for console functions, and RGB
#include <windows.h>
//required for string functions
#include <string>
//this really isnt needed but whatever....
#include <stdlib.h>
//required for time related functions
#include <time.h>
//need this for string type, and umm make sure you got this
using namespace std;
//prototypes the DrawColorString function
void DrawColorString(string szText, int X, int Y, WORD color);
//defines main function
int main()
{
//seed the random number generator
srand ( time(NULL) );
//defines character array with 1 character
char myStr[1];
//starts a for loop for the a variable 0-59
for(int a=0;a<60;a++)
{
//starts another for loop for the b variable 0-30
for(int b=0;b<30;b++)
{
//sets the character array at index 0 to random value
myStr[0]=rand()%255;
//calls the DrawColorString function to draw a character to the screen
//myStr is the character string
//a is the x position
//b is the y position
//sets the color to a random value
DrawColorString(myStr, a, b, RGB(rand()%255,rand()%255,rand()%255));
}
}
//goes to the next line
printf("\n");
//return a value
return 0;
}
//defines function to draw a colored string
void DrawColorString(string szText, int X, int Y, WORD color)
{
//defines the OutputH handle
HANDLE OutputH;
//defines the position Coordinate
COORD position = {X, Y};
//returns the handle of the console
OutputH = GetStdHandle(STD_OUTPUT_HANDLE);
//set the text color of the console
SetConsoleTextAttribute(OutputH, color);
//sets the position of the console
SetConsoleCursorPosition(OutputH, position);
//outputs the string value to the screen
cout << szText;
using C++ doesn't mean you have to stick to its stupid new rules like having to use "cout" and taking the ".h" off headers ¬_¬
"Say you're hanging from a huge cliff at the top of mt. everest and a guy comes along and says he'll save you, and proceeds to throw religious pamphlets at you while simultaniously giving a sermon." - Dustin G
Kris/CK4R1: You're absolutely right, you dont, but strange how I get shouted down for telling you correct syntax, yet just a few posts before I got in trouble for using incorrect but valid syntax (just like you did) myself... It's great how some people can have double standards, wouldnt you agree? ^__^
Mike
"Now I guess we're... 'Path-E-Tech Management'" -Dilbert
C++ IS NOT C. C++ has a whole load more stuff like overloading, classes (with inheritence, encapsulation and polymorphism), exceptions, template classes, overloaded operators...
The string class is cool. Here's a snippet...
string MyStr;
MyStr = "Hello!";
MyStr += " What a nice " + "day";
cout<<MyStr.c_str();
To do that with char buffers you'd have a whole lot of strcats, strcpys and stuff, plus you run the risk of overrunning your buffer - the string class auto resizes to fit.
- Tigerworks
Assault Andy Administrator
I make other people create vaporware
Registered 29/07/2002
Points 5686
21st April, 2004 at 07:29:04 -
Very nice Shad, now people let's let him get back to writing more tuts.
sorry if i sound [\write] like im shouting, I just think that people should be taught both methods then given the choice for themselves when they learn the pros / cons etc
"Say you're hanging from a huge cliff at the top of mt. everest and a guy comes along and says he'll save you, and proceeds to throw religious pamphlets at you while simultaniously giving a sermon." - Dustin G
"Say you're hanging from a huge cliff at the top of mt. everest and a guy comes along and says he'll save you, and proceeds to throw religious pamphlets at you while simultaniously giving a sermon." - Dustin G