![]() |
| | #1 |
| Absent Minded Programmer Join Date: May 2005
Posts: 933
| Having trouble making a function do what I want. 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;
};
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
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. |
| Shamino is offline | |
| | #2 |
| Registered User Join Date: Aug 2005
Posts: 1,330
| 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;
}
|
| ZuK is offline | |
| | #3 |
| Absent Minded Programmer Join Date: May 2005
Posts: 933
| 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. |
| Shamino is offline | |
| | #4 |
| Absent Minded Programmer Join Date: May 2005
Posts: 933
| 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();
}
}
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;
};
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. |
| Shamino is offline | |
| | #5 | |
| The larch Join Date: May 2006
Posts: 3,222
| 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. Quote:
| |
| anon is offline | |
| | #6 |
| Absent Minded Programmer Join Date: May 2005
Posts: 933
| 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. |
| Shamino is offline | |
| | #7 | |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Quote:
-- 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. | |
| matsp is offline | |
| | #8 | |
| The larch Join Date: May 2006
Posts: 3,222
| 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] = "." Code: sentence[] = {1, 0, 2, 0, 3, 0, 4, 5}
__________________ I might be wrong. Quote:
| |
| anon is offline | |
| | #9 |
| Absent Minded Programmer Join Date: May 2005
Posts: 933
| 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
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. |
| Shamino is offline | |
| | #10 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,783
| Code: cout << coutput[index]; Code: coutput[id] = update;
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Which Library Files for these unreferenced functions? | lehe | C++ Programming | 3 | 01-31-2009 10:30 PM |
| How to fix misaligned assignment statements in the source code? | biggyK | C++ Programming | 28 | 07-16-2006 11:35 PM |
| help on making int to string from function | peter_hii | C++ Programming | 5 | 04-12-2006 02:58 PM |
| const at the end of a sub routine? | Kleid-0 | C++ Programming | 14 | 10-23-2005 06:44 PM |
| c++ linking problem for x11 | kron | Linux Programming | 1 | 11-19-2004 10:18 AM |