Hey guys =D
I was bored the other day so I decided to make a class for c++. The class allows you to write console messages in an RPG style. So here is the class as well as example code:
#include <iostream>
#include <windows.h>
usingnamespace std;
/********************************************
* Introduction *
* Hello there, and welcome to my *
* type writer class for c++. I made this *
* to provide the user a simple solution *
* for creating messages that are displayed *
* like ones in an RPG. You can use this in *
* ANY of your projects royalty free! And, *
* you don't need to credit me... but it *
* would be nice of course. At least tell *
* me if you decide to use it so that I see *
* it at work in your game/project =D *
* *
* How to use *
* You can create a message in three simple *
* steps. First, you must declare a type *
* writer object. Next, use *
* setmessage(), to initiate your type *
* writer's message. Once that's done, use *
* write() to display your message. Note: *
* in the write() function, there is a *
* parameter called speed which stands for *
* the number of characters displayed per *
* second. Well, that's it. I hope you *
* enjoy! ;) *
* *
* SuperSonic *
********************************************/
class typewriter
{
public:
char message[256];
void setmessage(char input[256])
{
counter = 0;
messagedone = false;
for(int i = 0; i < 256; i++)
{
message[i] = input[i];
}
}
void write(int speed)
{
while(!messagedone)
{
if(message[counter])
{
cout << message[counter];
counter++;
}
else
{
messagedone = true;
}
Sleep(1000/speed);
}
}
private:
int counter;
bool messagedone;
};
int main()
{
typewriter helloworld;
helloworld.setmessage("Hello world. Like my new type write effect?");
helloworld.write(30);
Sleep(3000);
return 0;
}
I would like to know what you guys think. If you know of ways that I could shorten the code or anything like that, pleas post them. Thanks and happy coding to all ;)
It is possible that your string is not null terminated when you set it if the string you set it with is > 256 characters. You should probably null terminate yourself as well.
Hello clanmjc and Disch and thank you for your replies =)
@clanmjc: I don't quite understand what you mean. I'm still a little newbish to c++. What exactly does null terminating mean and how do I accomplish it?
@Disch: I thought about using a string but doesn't that function differently than a char array?
Using strings here would be a much better alternative as Disch has suggested. You aren't doing anything 'crafty' that a string cannot do, with a much simpler implementation.
You still use the [] operator to do your typewriter.
@ne555: I originally had the copy function so that it would be easier for people to copy messages without having to run the for loop all the time. But now that I'm using strings, I will remove the function. Thanks, and also, when you say flush, what do you mean by that?