Having trouble making a function do what I want.

This is a discussion on Having trouble making a function do what I want. within the C++ Programming forums, part of the General Programming Boards category; Okay, I'm trying to create a text interface with my console for a blackjack game, I'm trying to make a ...

  1. #1
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964

    Having trouble making a function do what I want.

    Okay, I'm trying to create a text interface with my console for a blackjack game, I'm trying to make a function called init_output to setup a sort of template for output where certain fields can be updated.

    This is the structure defining the "menu" of sorts.
    Granted it isn't pretty, but eventually I want to add some serialization to this class and make a text editor that can save these structures to the hard drive, so I can load masses of them for a text adventure or something.

    Code:
    struct text_menu
    {
    	typedef std::map<int, std::string> tmenu;
    	text_menu()			//Construct Blackjack text interface
    	{
    		int id;			//declare ID for output item
    		std::string output;		//declare the actual output item
    		id = 0;
    		output = "Dealer's Cards";
    		text.insert(std::make_pair(id, output));
    		id = 1;
    		output = "--------------";
    		text.insert(std::make_pair(id, output));
    		id = 2;
    		output = "Nothing Here.";
    		text.insert(std::make_pair(id, output));
    		id = 1;
    		output = "--------------";
    		text.insert(std::make_pair(id, output));
    		id = 3;
    		output = "Player's Cards";
    		text.insert(std::make_pair(id, output));
    		id = 1;
    		output = "--------------";
    		text.insert(std::make_pair(id, output));
    		id = 4;
    		output = "Nothing Here.";
    		text.insert(std::make_pair(id, output));
    		id = 1;
    		output = "--------------";
    		text.insert(std::make_pair(id, output));
    		id = 5;
    		output = "What would you like to do?\n1) Hit me\n2) Stay\n3) Fold\n";
    		text.insert(std::make_pair(id, output));
    	}
    	tmenu text;
    };
    All I'm doing with that is creating a map of strings and ID's to those strings. I may want to make a list of 0's and 1's for every entry into the map so I know which items are dynamic, or updateable, and which items are static.

    Here is the IO class that I will use to manage these structures in massive quantities (eventually), right now I have the structure hard coded but I've realized essentially it's a file type.

    Code:
    class IO
    {
    public:
    	typedef	std::map<int, std::string> output;
    	
    	void update_output(int id, std::string update)
    	{
    		output::iterator index = coutput.find(id);
    		(*index).second = update;
    	}
    
    	void init_output(text_menu text)
    	{
    		
    	}
    
    	void display_output()
    	{
    		for( output::iterator index = coutput.begin(); index != coutput.end(); ++index)
    		{
    			cout << (*index).second << std::endl;
    		}
    	}
    	int get_input()
    	{
    		std::cin >> input;
    		return input;
    	}
    	void clear_screen()
    	{
    		system("cls");
    	}
    	output coutput;
    	int input;
    	
    };
    #endif
    While the update_output function strictly is for usage of the game to update certain complex strings like a players hand or something, if I had a list of 1's and 0's in the structure I could accomplish this better, because then I could return a variable to the system to automatically connect game updates with text updates and such.

    Essentially though I'm having trouble with the init_output function. I need to copy the map in tmenu.text over to coutput, I've been googling copy stuff for maps but I havn't found anything that really explained it, can anyone help?
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    text_menu::text and IO::coutput are of the same type, both text_menu::text and IO::coutput are declared public -> Simple assignement could be used.
    e.g.
    Code:
    	void init_output(text_menu text)
    	{
    		coutput = text.text;
    	}
    Kurt

  3. #3
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Wow, "that was easy"

    Can I insert it rather than set it equal to?

    So I can load multiple menu's into the coutput map.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  4. #4
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    I'm having a problem with the output, with this function:
    Code:
    	void play_game()
    	{
    		dealer.shuffle_deck(deck);
    		int gameloop = 1;
    		io.init_output(tmenu);
    		while(gameloop != 0)
    		{
    			io.display_output();
    			gameloop = blackjack(io.get_input()); 
    
    			io.clear_screen();
    		}	
    		
    	}
    For some reason it's not copying over all my dividers as you see I created here, are duplicate ID's not allowed?

    Code:
    struct text_menu
    {
    	typedef std::map<int, std::string> tmenu;
    	text_menu()			//Construct Blackjack text interface
    	{
    		int id;			//declare ID for output item
    		std::string output;		//declare the actual output item
    		id = 0;
    		output = "Dealer's Cards";
    		text.insert(std::make_pair(id, output));
    		id = 1;
    		output = "--------------";
    		text.insert(std::make_pair(id, output));
    		id = 2;
    		output = "Nothing Here.";
    		text.insert(std::make_pair(id, output));
    		id = 1;
    		output = "--------------";
    		text.insert(std::make_pair(id, output));
    		id = 3;
    		output = "Player's Cards";
    		text.insert(std::make_pair(id, output));
    		id = 1;
    		output = "--------------";
    		text.insert(std::make_pair(id, output));
    		id = 4;
    		output = "Nothing Here.";
    		text.insert(std::make_pair(id, output));
    		id = 1;
    		output = "--------------";
    		text.insert(std::make_pair(id, output));
    		id = 5;
    		output = "What would you like to do?\n1) Hit me\n2) Stay\n3) Fold\n";
    		text.insert(std::make_pair(id, output));
    	}
    	tmenu text;
    };
    Any idea's? Here is the output:
    Code:
    Dealer's Cards
    --------------
    Nothing Here.
    Player's Cards
    Nothing Here.
    What would you like to do?
    1) Hit me
    2) Stay
    3) Fold
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    No, map doesn't allow duplicate keys.

    Why don't you make a simple array (vector) of strings you want to print?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Can I still search the vector using an ID? I guess the ID is the index? Because there are some items that I would want to reuse, like dividers for instance.

    I could use string compare right?
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Shamino View Post
    Can I still search the vector using an ID? I guess the ID is the index? Because there are some items that I would want to reuse, like dividers for instance.

    I could use string compare right?
    But if you are re-using the same thing, surely you only need one, and it can have ONE unique ID - I appologize if this isn't really what you asked - I haven't read ALL of the code and text in the thread...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Well, you could have a vector of available strings:
    Code:
    vec[0] = " "
    vec[1] = "This"
    vec[2] = "is"
    vec[3] = "a"
    vec[4] = "sentence"
    vec[5] = "."
    And then you might have a vector of numbers that tell you which items to use in which order:
    Code:
    sentence[] = {1, 0, 2, 0, 3, 0, 4, 5}
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #9
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    I'm having trouble getting the vector setup to compile right, it's mostly just synatx issues:
    Code:
    #ifndef IO_H
    #define IO_H
    #include <iostream>
    #include <string>
    #include <map>
    #include <string>
    
    struct text_menu
    {
    	typedef std::vector<std::string> tmenu;
    	text_menu()			//Construct Blackjack text interface
    	{
    		std::string output;		//declare the actual output item
    		output = "Dealer's Cards";
    		text.push_back(output);
    		output = "--------------";
    		text.push_back(output);
    		output = "Nothing Here.";
    		text.push_back(output);
    		output = "--------------";
    		text.push_back(output);
    		output = "Player's Cards";
    		text.push_back(output);
    		output = "--------------";
    		text.push_back(output);
    		output = "Nothing Here.";
    		text.push_back(output);
    		output = "--------------";
    		text.push_back(output);
    		output = "What would you like to do?\n1) Hit me\n2) Stay\n3) Fold\n";
    		text.push_back(output);
    	}
    	tmenu text;
    };
    
    class IO
    {
    public:
    	typedef	std::vector<std::string> output;
    	
    	void update_output(int id, std::string update)
    	{
    	}
    
    	void init_output(text_menu text)
    	{
    		coutput = text.text;
    	}
    
    	void display_output()
    	{
    		for (int index = 0; index < coutput.size(); index++)
    		{
    		}
    		/*
    		for( output::iterator index = coutput.begin(); index != coutput.end(); ++index)
    		{
    			cout << (*index).second << std::endl;
    		}
    		*/
    	}
    	int get_input()
    	{
    		std::cin >> input;
    		return input;
    	}
    	void clear_screen()
    	{
    		system("cls");
    	}
    	output coutput;
    	int input;
    	
    };
    #endif
    That is the new text_menu structure and IO class.

    I'm having trouble with the syntax for updating one of the elements, as well as the syntax for outputting the whole deal.

    Any ideas?
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,031
    Code:
    cout << coutput[index];
    For update you can try:
    Code:
    coutput[id] = update;
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 09:28 AM
  2. Replies: 28
    Last Post: 07-17-2006, 12:35 AM
  3. help on making int to string from function
    By peter_hii in forum C++ Programming
    Replies: 5
    Last Post: 04-12-2006, 03:58 PM
  4. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 07:44 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21