The Daily Click ::. Forums ::. Non-Klik Coding Help ::. Guide to C++
 

Post Reply  Post Oekaki 
 

Posted By Message

ChrisB

Crazy?

Registered
  16/08/2002
Points
  5457
19th April, 2004 at 19:09:58 -

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.

 
n/a

vortex2



Registered
  27/05/2002
Points
  1406
19th April, 2004 at 19:58:59 -

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;

}


 
A bit of insanity with every bite!

Galaxy613



Registered
  29/01/2003
Points
  1765
19th April, 2004 at 20:10:04 -

erm...shads? I believe that WAS C++, I don't believe that

for(int a=0;a<10;a++){...

isn't vaild C; and I found out something neat:


#typedef STR char *
#include <stdio.h>

int main(){
STR name;

printf("Whats your name? "); scanf("%s",&name);
printf("\nHello %s!\n",&name);

system("Pause");

return 0;
}

pointers with typedef!

Image Edited by the Author.

 
Image
My forum: http://subsoap.com/ck/forums/index.php

ShadowCaster

Possibly Insane

Registered
  02/01/2002
Points
  2203
19th April, 2004 at 22:29:19 -

C definately isnt C++ [and vice versa], Vortex.

CK4RL: Who said anything about the FOR loop?

Mike ¿

 
"Now I guess we're... 'Path-E-Tech Management'" -Dilbert

vortex2



Registered
  27/05/2002
Points
  1406
19th April, 2004 at 23:49:10 -

C isnt c++ but c++ is based on c so c++ IS c.... and I use visual c++ 6.0 soo .

Image Edited by the Author.

 
A bit of insanity with every bite!

Kris

Possibly Insane

Registered
  17/05/2002
Points
  2017
20th April, 2004 at 14:28:02 -

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

Galaxy613



Registered
  29/01/2003
Points
  1765
20th April, 2004 at 20:31:12 -

Kris is right

 
Image
My forum: http://subsoap.com/ck/forums/index.php

ShadowCaster

Possibly Insane

Registered
  02/01/2002
Points
  2203
21st April, 2004 at 02:47:49 -

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

Tigerworks

Klik Legend

Registered
  15/01/2002
Points
  3882
21st April, 2004 at 03:10:39 -

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

Game of the Week WinnerVIP Member360 OwnerGOTM JUNE - 2009 - WINNER!GOTM FEB - 2010 - WINNER!	I donated an open source project
21st April, 2004 at 07:29:04 -

Very nice Shad, now people let's let him get back to writing more tuts.

 
Creator of Faerie Solitaire:
http://www.create-games.com/download.asp?id=7792
Also creator of ZDay20 and Dungeon Dash.
http://www.Jigxor.com
http://twitter.com/JigxorAndy

vortex2



Registered
  27/05/2002
Points
  1406
21st April, 2004 at 10:46:10 -

moo .

 
A bit of insanity with every bite!

Kris

Possibly Insane

Registered
  17/05/2002
Points
  2017
21st April, 2004 at 11:49:21 -

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

Decal



Registered
  07/06/2003
Points
  1509

VIP Member
22nd April, 2004 at 16:13:36 -

I don't have a c++ editor.
What is the best c++ editor that it's free and downloadable?
And is there a downloadable trial version of Visual C++?

 
www.decal.rr.nu

Decal



Registered
  07/06/2003
Points
  1509

VIP Member
22nd April, 2004 at 16:13:37 -

I don't have a c++ editor.
What is the best c++ editor that it's free and downloadable?
And is there a downloadable trial version of Visual C++?

 
www.decal.rr.nu

Kris

Possibly Insane

Registered
  17/05/2002
Points
  2017
22nd April, 2004 at 16:49:24 -

Dev C++ is free. Dunno about MSVC demos though...

http://www.bloodshed.net/devcpp.html

 
"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
   

Post Reply



 



Advertisement

Worth A Click