Advertisement

Simulating Typing in C

Started by June 03, 2015 07:45 PM
11 comments, last by BitMaster 9 years, 6 months ago

hi all

new programmer learning C here, the book I've read is basically restricted to text based output and the language itself. While I practice I was wanting to create a few text based games to make it fun as the exercises in the book are very boring. However to create a "cool" atmosphere I am wanting to simulate typing when I print text rather than it appear straight away. I have figured out how to do this, but I am struggling on how to pass or find the actual string length to my new print function. Here is the doubtless silly attempt I've come up with so far :


#include <stdio.h>
#include <windows.h>

void print(char * text);

int main(void)
{
	print("This is some text");
	return 0;
}

void print(char * text)
{
	int length;
	for(length = 0; length < 5000; ++length)
	{
		text += 1;
		if (*text == '\0')
		break;
	}
    for(int i = 0; i < length; ++i)
    {
    printf("%c", text[i]);
    Sleep(10);
    }
}

I am just trying to understand how I determine the length of the passed string in a clean manner, rather than telling it to search upto 5000 characters looking for the NULL termination, which doesn't work anyway.

Thanks for any help!

If your strings are null-terminated, is there any particular reason you aren't using strlen?

Advertisement
Why are you wanting to determine the length of the text, why not just:


Void Print(const char *Text){//note that we also make the input const-correct.
   For(const char *c = Text; *c!='\0'; c++){ 
     Printf("%c", *c); 
     Sleep(10); 
  }   
Return;
}
Your already counting the characters, so why not just print them at the same time?

Edit: you seem to know how to do pointer arthmitic, and derefrence pointers, you just need to know how to combine the two so your not using a magic number like 5000.
Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

wow thank you both for the quick response, the strlen function slipped my mind completley! - and thanks for the code example slicer4ever, I should be ok now on this : )

unless you just want strictly "C" code only, I would highly recommend using std::string and its associated member functions to loop through its characters individually; should be much safer than a raw char*

Is there any specific reason you are sticking with C and not mixing it as C/C++? I have to second the std::string class because it does make it easier to work with text. You can access each element in the string as an array so for example:


#include <string>
#include <iostream>
#include <windows.h>

//... Skipped initialization code for brevity

void print(std::string text)
{
  for(int i=0; i<text.length(); i++)
  {
    std::cout << text[i];
    Sleep(10);
  }
}

I typically use the streams for input and output but you could easily replace it with your printf function. The reason I use streams is because there so handy at working with different data types.

FYI, I've spent the last few month building my own ASCII (text) based game and game engine. Here is a linked to my development journal for your reference to see what can be done with text and a little creativity.

http://www.gamedev.net/blog/2057/entry-2261050-introduction/

Also if your interested in learning more about strings and what you can do with them here is a site that has done a great job at documenting many of the standard libraries in C/C++:

http://www.cplusplus.com/reference/string/string/

Good luck and have fun making your game!

Advertisement

Thanks for the replies, I did google which language to learn before I got the C book. The main reason I opted for C was because I'm also interested in system programming and apparently most operating systems are in C (like linux and BSD). The icing on the cake was that C is much smaller and easier to learn : )

Thanks for the links, will definitely check them out!

Is there any specific reason you are sticking with C and not mixing it as C/C++? I have to second the std::string class because it does make it easier to work with text. You can access each element in the string as an array so for example:


#include <string>
#include <iostream>
#include <windows.h>

//... Skipped initialization code for brevity

void print(std::string text)
{
  for(int i=0; i<text.length(); i++)
  {
    std::cout << text[i];
    Sleep(10);
  }
}

I typically use the streams for input and output but you could easily replace it with your printf function. The reason I use streams is because there so handy at working with different data types.

FYI, I've spent the last few month building my own ASCII (text) based game and game engine. Here is a linked to my development journal for your reference to see what can be done with text and a little creativity.

http://www.gamedev.net/blog/2057/entry-2261050-introduction/

Also if your interested in learning more about strings and what you can do with them here is a site that has done a great job at documenting many of the standard libraries in C/C++:

http://www.cplusplus.com/reference/string/string/

Good luck and have fun making your game!

for C++, it's text.size(), not text.length(). Please try and not give incorrect information in the beginner's section, it's already confusing enough for some of them :)

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

Well, while we're already (incorrectly) nitpicking the example (std::string has both size() and length()... length because it makes more sense conceptually and size for consistency with other containers)...


void print(std::string text)

This will rather pointlessly create a copy of the string, 9 out of 10 times you want to pass it as const reference:

void print(const std::string& text)


for(int i=0; i<text.length(); i++)

Generally the rule of thumb is to avoid calling functions in your end condition. Since you didn't make the string const, the compiler might end up evaluation length on every iteration. Before C++11, an implementation could actually internally call strlen and end up pointlessly iteration the whole string to count characters on every iteration of your loop.

"Complex" loop conditions that won't change during the iteration should be evaluated once before the loop:

const size_t length = text.length();
for (int i = 0; i < length; ++i)

In this case however, string follows the usual container conventions, so in C++11, just use a range based for loop.

for (char c : text)
f@dzhttp://festini.device-zero.de

I would highly recommend using std::string, [it's much safer]

[Why] C and not [C++]?

[C++ nit-picking]

[C++ nit-picking]

"Why are you using C strings in your C program? You should use C++ strings in a C++ program instead." Ugh. OP is learning C, not C++.

How about we suggest that he use Java instead? Java is far safer than C++, because exceptions, virtual environment, and other fun stuff.


/rant

This topic is closed to new replies.

Advertisement