Thread: How to make this function work with cout statements?

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    67

    How to make this function work with cout statements?

    I have made a program which utilizes a loop to output a string character by character with a delay of 100 milliseconds inbetween, until, the user presses ENTER which speeds up the process by changing the delay to 10 miliseconds (This function, not THIS exact method, is what you usually see in games to speed up the npc's character text.)

    Code:
    #include <iostream>
    #include <string>
    #include <windows.h>
    
    
    using namespace std;
    
    int main( int argc, char* args[])
    {
        string intro_speech = "Hello, and welcome to the introduction!";
        unsigned int speed_msg = 100;
    
        for(unsigned i = 0; i < intro_speech.length(); i++)
        {
            cout << intro_speech[i];
            Sleep(speed_msg);
            if (GetAsyncKeyState(VK_RETURN)){
            speed_msg = 10;
        }
        else if(GetAsyncKeyState(VK_RETURN) != true){
        speed_msg = 100;
        }
    }
        cin.ignore();
        cin.get();
        return 0;
    }
    But the bad thing is I only know how to do this for a defined string(When I mean "defined" I mean it has a stream of text stored) . So if my game had a thousand sentences for NPC's, and I wish to implement the method of speeding up the sentence by pressing enter, I would have to make a thousand strings and for loops which would take FOREVER and take up alot of memory...

    I thought of making a function but I don't know how to go about it..
    I want it use it like this:

    Code:
    Speed_Msg("This is an example");
    So basically the function "Speed_Msg" would be made up with the code blocks you see in the program posted at the beginning of this thread, but without having to use a defined string, it will use the cout command and know what string to output by looking at the argument in its parameter (As you can see by the example above.)

    But I don't know how to set up the function to do something like that? Any comments or help will be greatly appreciated!

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Firstly I notice you have some wrong indentgation there that needs fixing up, it looks almost intentionally misleading.
    Secondly, I suggest that you just give it a try, i.e. try putting the parts of the code you would reuse into what you intend to be the function body.
    Then we'll help you with whatever goes wrong.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    67
    I apologize for my writing effiency, it does need a tad refining. Yet, if your unable to read this program, than I suggest you go check out some of the tutorials on the cprogramming website. Alex Allain has written an extremely detailed tutorial on FOR loops.

    Also, if you had read my comment above, I had posted the function AND the codeblocks I had try inserting and compiling(Skipping through a sentence will lead to loss of important information). My question is how can I go about making a void function that can recieve a stream of text
    Code:
    Speed_Msg("This is an example");
    

    writtened in its parameters as an argument, and than apply the function to it?

    So here, this is what it looked like when I tried to do this:
    Code:
    #include <iostream>
    #include <string>
    #include <windows.h>
    
    
    using namespace std;
    
    void Speed_Msg()
    {
        string Apply_Msg;
        unsigned int speed_msg = 100;
    
        
        for(unsigned i = 0; i < Apply_Msg.length(); i++)
        {
            cout << Apply_Msg[i];
            Sleep(speed_msg);
           
     if (GetAsyncKeyState(VK_RETURN)){
            speed_msg = 10;
            }
    
    else if(GetAsyncKeyState(VK_RETURN) != true){
        speed_msg = 100;
        }
    }
        Sleep(2000);
        cin.ignore();
        cin.get();
    
    }
     
    int main( int argc, char* args[])
    {
           Speed_Msg("This will now be outputted with the delay effect!");
    
          return 0;
    }
    Without the proper knowledge, I am unable to proceed. I am going to have to refer back to the article of pointers, because I think that is what I'll need, but even than, I will still not know how to impliment it in the functions parameters(If it's even possible with strings..)

    I appreciate your comment iMalc! (I will work on my indentation, I promise ;P)

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    First thing you need to do then is read up on functions, focussing on how to declare, use and pass parameters.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    67
    Quote Originally Posted by Salem View Post
    First thing you need to do then is read up on functions, focussing on how to declare, use and pass parameters.
    I will certainly do this.

    But, is what I'm trying to do even possible? Adding a stream of text into a string by using the parameters, such as:
    Code:
    Speed_Msg("This will output anything typed here with quotations, and apply the speed function!");

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Yes you'll be able to do that, just as soon as you add a string parameter to your function.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    67
    I'm confused. Are you saying I should define a string like
    Code:
    string example = "This is random";
    than
    Code:
    Apply_Msg(example);
    Because if so, that's not really what I wanted because if I had a thousand sentences worth of text output, would I not have to declare a thousand strings if I wanted them sperate? What I wanted to do was just type a message in the functions parenthesis that would output the text from a cout command in the function "Speed_Msg", but I would need to know some how to put text written in the functions parenthesis into a string, which I have no idea how to do from there. Sorry to be stupid. I am still learning on functions and strings..

    Could you direct me to a link or something which best describes how to do this? Alex Allain had excellent tutorials on this website, could you tell me one of his tutorials that explains this process. Been googling but so far yet found nothing which explains this specific situation.
    Last edited by cplusplusnoob; 03-29-2012 at 01:34 PM.

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. With cout - Why Wont This Work?
    By mike_g in forum C++ Programming
    Replies: 5
    Last Post: 09-21-2007, 09:14 AM
  2. Can't get cout to work in for() loop
    By motarded in forum C++ Programming
    Replies: 1
    Last Post: 03-03-2006, 09:00 AM
  3. how do you repeat cout statements?
    By findme in forum C++ Programming
    Replies: 10
    Last Post: 11-21-2005, 11:34 AM
  4. How Does cout Work
    By jrahhali in forum C++ Programming
    Replies: 8
    Last Post: 08-25-2004, 03:19 PM