Rpg printing to console

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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <windows.h>
using namespace 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 ;)

SuperSonic
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.
or just use a string instead of a char array?
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?
Last edited on
don't quite understand what you mean.

http://www.cplusplus.com/doc/tutorial/ntcs/

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.

Much smaller footprint...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

std::string message;
//....

void setmessage(const std::string &input)
{
   message = input;
}

void write(int speed)
{
   for(int i = 0; i < message.size(); ++i)
  {
     cout << message[i];
     Sleep(1000/speed);
  }
}
Or you could make just 1 function and avoid the copy.

Also, you may need to flush the output.
@clanmjc: Thank you for explaining all of that =D

@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?
all unwritten characters in the buffer are written to its controlled output sequence as soon as possible ("flushed").
http://www.cplusplus.com/reference/iostream/manipulators/flush/
Thank you ne555. I will read through this =)
Topic archived. No new replies allowed.