Public Group

# [C++] Printing a string character by character.

This topic is 2562 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi everyone, first time poster here.

Just wanted to say that it seems like you have a great community going here. I'm a complete rookie in C++, and I've been trying to find some good tutorials to practice with. So far, I'm in way over my head, but day by day it gets less confusing, and that's a good thing. I decided, though, that the best way to go about learning is to try my hand at making a text-based game from scratch, using only reference sites.

I recently stumbled across this forum post describing how to print out a string, character by character, using iterators. Full disclosure: I haven't reached iterators in my tutorials yet.

Code follows:

// builder.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <cstdlib> #include <iostream> #include <string> #include <windows.h> int main() { typedef std::string::const_iterator iter; std::string msg ( "This is a message" ); iter begin = msg.begin(); iter end = msg.end(); for ( iter it = begin; it != end; ++it ) { std::cout.put ( *it ); Sleep (75); } std::cout<<std::endl; }

This is perfect. Exactly the right speed, exactly what I wanted it to do. The only problem is that it uses a static value for the string ("This is a message"), whereas I'd like my entire (text-based) game to be displayed with this scrolling effect. The idea is that I include the above code in a separate header file, and call to the function whenever text needs to be displayed, thereby creating the desired effect for all the text in the game (except menus, which will have their own header file).

I'm having a really hard time trying to convert the function from static input to dynamic input; any ideas how I might get this done?

Thanks in advance for the patience, I still don't quite know my elbow from my ass.

##### Share on other sites
Well you're 90% of the way there, just move your existing code into a separate function and pass the msg as a parameter.
 void slow_type(const std::string & msg) { typedef std::string::const_iterator iter; iter begin = msg.begin(); iter end = msg.end(); for ( iter it = begin; it != end; ++it ) { std::cout.put ( *it ); Sleep (75); } std::cout<<std::endl; } 

##### Share on other sites
Ahh, that must be the 'thinking outside the box' people keep raving on and on about.

Worked perfectly with a forward declaration. Thanks so very much. And if it's alright with your community, I might periodically show the work on my game for suggestions on optimization. ^^

##### Share on other sites
Hi guys, quick bump on this, here's the code for my main menu, with custom commands:

commands.h

#include "stdafx.h" #include <string> #include "statUI.h" string Command; void CmdPrompt(); void NewGame(); void Credits(); int Menu; void CmdPrompt() { if (Command=="/r") Menu = 0; if (Command=="N"||"n") Menu = 1; if (Command=="C"||"c") Menu = 3; switch(Menu){ case 0: statUI(); break; case 1: NewGame(); break; case 3: Credits(); break; } _getch(); }

And here are the contents of StatUI, #included above:

#include <iostream> #include <conio.h> using namespace std; int Gold; void statUI() { system("cls"); cout << "\n\tGold: " << Gold << "\t\t\t\t\tWinter, Year 1" << endl; cout << "\t-------------------------------------------------------" << endl; _getch(); }

And finally, the main .cpp file with all the #includes:

// builder.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "commands.h" #include <dos.h> #include <stdio.h> #include <conio.h> #include <cstdlib> #include <iostream> #include <string> #include <Windows.h> #include "sType.h" #include "intro.h" #include "NewGame.h" #include "Credits.h" void Splash(); int main() { Splash(); _getch(); return 0; }

I know the code is messy, I'm sorry. It's basically all patchwork. I'm more concerned with actually building something, and figuring it out as I go.

So the problem here is, when I run the program, everything launches fine, except no matter what I choose, it always executes the Credits(); function. This happened after implementing the switch(Main) function in commands.h. Previously, I had been using an if/else statement, but it also had the problem of only executing the NewGame(); function.

Any assistance would be greatly appreciated.

##### Share on other sites
You can't do something like (a == 'a' || 'b'), you need to do (a == 'a' || a == 'b').

##### Share on other sites
Heh, the devil's in the details. Thanks again for your help.

##### Share on other sites
[color="#000088"]for [color="#666600"]( iter it [color="#666600"]= [color="#000088"]begin[color="#666600"]; it [color="#666600"]!= [color="#000088"]end[color="#666600"]; [color="#666600"]++it [color="#666600"])

Why are you starting on the second character?

##### Share on other sites
What are you talking about? That doesn't start at the second character.

##### Share on other sites

[color="#000088"]for [color="#666600"]( iter it [color="#666600"]= [color="#000088"]begin[color="#666600"]; it [color="#666600"]!= [color="#000088"]end[color="#666600"]; [color="#666600"]++it [color="#666600"])

Why are you starting on the second character?

Why do you think it would start on the second character?

begin seems to be initialized as .begin(), which should point to the first character.

##### Share on other sites

[quote name='Serapth' timestamp='1310579365' post='4834922']
[color="#000088"]for [color="#666600"]( iter it [color="#666600"]= [color="#000088"]begin[color="#666600"]; it [color="#666600"]!= [color="#000088"]end[color="#666600"]; [color="#666600"]++it [color="#666600"])

Why are you starting on the second character?

Why do you think it would start on the second character?

begin seems to be initialized as .begin(), which should point to the first character.
[/quote]

Brain fart, ignore me.

1. 1
2. 2
3. 3
Rutin
21
4. 4
5. 5
gaxio
10

• 14
• 30
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631778
• Total Posts
3002308
×