   how do i pass a parameter "literally" (using c++)
  high,i have come up with a couple different problems in my game, both involving the same problem. the first one is, i want to have a LoadMap() function that loads a specific map depending on what level it is.anyway, i cant seem to figure out how to do this without making a big mess. the code looks like this:ifstream file_in(MAP1);where at the top of my source i did #define MAP1 "Map1.txt"how do i pass this as a perameter? its not possible to send a DEFINED thing around as a parameter (is it?) so  if i have multiple maps, i cant do  something likestring MAP1 = "map1.txt"string MAP2 = "map2.txt"LoadMap(MAP1) or LoadMAp(MAP2)this wont work. so how do i do this? is there anyway to pass a variable around "literally"? if not, how do i do this? this isnt the only time ive run into the same problem. now im working on putting music into my game and i want to load a specific song by sending it as a parameter to a LoadMusic() function. void LoadMusic(){if(!mySong.Init(SONG_NAME))mySong.PlaySong();}again here i have a DEFINED word SONG_NAME as "song.mp3"and i want to pass a certain song to the load music function but again this wont work with strings so how do i do it?[edited by - graveyard filla on January 31, 2004 11:32:33 PM][edited by - graveyard filla on January 31, 2004 11:34:07 PM]
  Passing them as strings works fine for me:#include <iostream>#include <string*gt;#define MOO "moocow"void display(std::string str) {  std::cout << str << std::endl;}int main(int, char **) {  display(MOO);  return 0;}  ;quote:Original post by graveyard filla  string MAP1 = "map1.txt"string MAP2 = "map2.txt"LoadMap(MAP1) or LoadMAp(MAP2)This most assuredly will work, except that you might need to use "char *" as your type instead of "string".;  If you are opening the map file outside of the map loading function then you could make your map loading function take an ifstream reference then pass the stream you have used to open the file into your loading function to use inside.;  Passing the stream is a good idea; you can separate all that messy validity checking from the actual loading code. ;quote:Original post by Zahlman  quote:Original post by graveyard filla   string MAP1 = "map1.txt"string MAP2 = "map2.txt"LoadMap(MAP1) or LoadMAp(MAP2)This most assuredly will work, except that you might need to use "char *" as your type instead of "string".No, "string" should work there, as should "const string&"If I am not mistaken, expected types of both "string&" and "string*" would cause it to fail.;  well its not working for me. could anyone give me the exact synax for passing an ifstream? this is what my LoadMap Function looks like. i know there has to be a better way- plus, im about to add a bunch more maps to my game and i dont want to have to make this simple functon 10 pages long, because i know im doing something wrong. im re-writing the same line of code over and over just to replace a single word. heres the code bool LoadMap(){if (gamedata.current_level == 1 ) //load map1.txt{// Create a "ifstream" (way to read from a file) for the map text fileifstream file_in(MAP1);// Error Check -- If we couldn't open the file, we can't load the mapif(!file_in)return false;// First line of "map file" contains two integers.// First int is the width of the map// Second int is the height of the mapfile_in >> gamedata.width; // Read in the width of the mapfile_in >> gamedata.height; // Read in the height of the map// Now we can create a CHAR_INFO array that is big enough to hold the mapgamedata.screenBuff = new CHAR_INFO[gamedata.width * gamedata.height];// Error check -- Make sure we could allocate the memoryif(gamedata.screenBuff == NULL){file_in.close(); // Close the file we openedreturn false;}// These will be used to index into our screenBuff// *Remember* even though "screenBuff" is a one dimensional array, we're going to// treat it as a two dimensional arrayint x = 0;int y = 0;char temp; // This will be a "temp" char -- This will hold the current character  // we've read in from the filewhile(y != gamedata.height) // Keep going until we reach the end of the "map"{file_in >> temp;switch(temp){case '0': // We've read in an "open space" in our maze// Fill the current location in the screen buffer with a space character (' ')// that has the color "blue"gamedata.screenBuff[x + y * gamedata.width].Char.AsciiChar = ' ';gamedata.screenBuff[x + y * gamedata.width].Attributes =  BACKGROUND_BLUE;//BACKGROUND_RED| BACKGROUND_BLUE | BACKGROUND_GREEN;break;case '#':  // We've read in a "wall" in our maze// Fill the current location in the screen buffer with the character // defined by WALL that has the background color blue and foreground // color greengamedata.screenBuff[x + y * gamedata.width].Char.AsciiChar = WALL;gamedata.screenBuff[x + y * gamedata.width].Attributes = BACKGROUND_BLUE | FOREGROUND_RED | FOREGROUND_GREEN;// |BACKGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_GREEN; //| FOREGROUND_GREEN;break;default: // If we read any other character (char), there's an error in our // map file -- So close it and returnfile_in.close();return false;}// Increment 'x' every time we write a character until 'x' equals the // width of map, then start 'x' back at zeroif(++x == gamedata.width){x = 0;y++; // We need to increment y so we effectively "go down to the next // line" in our screen buffer}} // end of while(y != gamedata.height)// Okay if we get here the map has been loaded successfully --   // Now all we have to do is close the file we opened and then return true   file_in.close();return true; // Success :)} //end of if gamedata.leveloneelse if (gamedata.current_level == 2) //else load the map2 file{// Create a "ifstream" (way to read from a file) for the map text fileifstream file_in(MAP2);// Error Check -- If we couldn't open the file, we can't load the mapif(!file_in)return false;// First line of "map file" contains two integers.// First int is the width of the map// Second int is the height of the mapfile_in >> gamedata.width; // Read in the width of the mapfile_in >> gamedata.height; // Read in the height of the map// Now we can create a CHAR_INFO array that is big enough to hold the mapgamedata.screenBuff = new CHAR_INFO[gamedata.width * gamedata.height];// Error check -- Make sure we could allocate the memoryif(gamedata.screenBuff == NULL){file_in.close(); // Close the file we openedreturn false;}// These will be used to index into our screenBuff// *Remember* even though "screenBuff" is a one dimensional array, we're going to// treat it as a two dimensional arrayint x = 0;int y = 0;char temp; // This will be a "temp" char -- This will hold the current character  // we've read in from the filewhile(y != gamedata.height) // Keep going until we reach the end of the "map"{file_in >> temp;switch(temp){case '0': // We've read in an "open space" in our maze// Fill the current location in the screen buffer with a space character (' ')// that has the color "blue"gamedata.screenBuff[x + y * gamedata.width].Char.AsciiChar = ' ';gamedata.screenBuff[x + y * gamedata.width].Attributes =  BACKGROUND_BLUE;break;case '#':  // We've read in a "wall" in our maze// Fill the current location in the screen buffer with the character // defined by WALL that has the background color blue and foreground // color greengamedata.screenBuff[x + y * gamedata.width].Char.AsciiChar = WALL;gamedata.screenBuff[x + y * gamedata.width].Attributes = FOREGROUND_GREEN;//BACKGROUND_BLUE | BACKGROUND_RED |BACKGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_GREEN; //| FOREGROUND_GREEN;break;default: // If we read any other character (char), there's an error in our // map file -- So close it and returnfile_in.close();return false;}// Increment 'x' every time we write a character until 'x' equals the // width of map, then start 'x' back at zeroif(++x == gamedata.width){x = 0;y++; // We need to increment y so we effectively "go down to the next // line" in our screen buffer}} // end of while(y != gamedata.height)// Okay if we get here the map has been loaded successfully --   // Now all we have to do is close the file we opened and then return true   file_in.close();return true; // Success :)}//end of else if statement} // end of bool LoadMap()as you can see, i had to write an else/if statement to check if its a different map, but i had to write the code exactly how it was above. i want to add more maps but if i do then this function is gunna be like 10 pages long. please what am i doing wrong?ive tried doing   if (gamedata.current_level == 1 ) //load map1.txt{// Create a "ifstream" (way to read from a file) for the map text fileifstream file_in(MAP1);// Error Check -- If we couldn't open the file, we can't load the mapif(!file_in)return false;else if (gamedata.current_level == 2){// Create a "ifstream" (way to read from a file) for the map text fileifstream file_in(MAP2);// Error Check -- If we couldn't open the file, we can't load the mapif(!file_in)return false;//then the rest of the map loding code here, only i only have to write it once.BUT THIS DOESNT WORK!!! one more thing - the way my LoadMap function is now, it gives me a warning "not all control paths have a return value". well thats bullshit because they do! dont they? i dunnothanks for any help   [edited by - graveyard filla on February 1, 2004 5:08:59 PM];  When you say it doesn''t work, what exactly do you mean? Both methods should work as far as opening the file is concerned, so perhaps the problem is in the part after that? Have you done anything to check that possibility? You are opening the file in text mode instead of binary, is this deliberate? Which mode did you use to save the files?Strip the function back to the essentials. Make it a function that merely opens a file, reads a number from it, then closes the file. When you have confirmed that it''s doing that correctly, then parameterize it so you can give it a file name or an istream to open. When that is confirmed to be working, then add the map loading code. If you try to do it all at once when you don''t know what you are doing, you''ll have a hard time finding the problem. ;quote:Original post by graveyard filla  well its not working for me. could anyone give me the exact synax for passing an ifstream? this is what my LoadMap Function looks like.There''s nothing wrong with your syntax.quote:im re-writing the same line of code over and over just to replace a single word.Yes, this is a bad thing. Let''s skip to the part where you try to do it properly...if (gamedata.current_level == 1 ) {  // Create a "ifstream" (way to read from a file) for the map text file  ifstream file_in(MAP1);  // Error Check -- If we couldn''t open the file, we can''t load the map  if(!file_in) return false;} else if (gamedata.current_level == 2) {  // Create a "ifstream" (way to read from a file) for the map text file  ifstream file_in(MAP2);  // Error Check -- If we couldn''t open the file, we can''t load the map  if(!file_in) return false;}//then the rest of the map loding code here  Ok, first of all you''re still duplicating more than you need to. But the problem is that in both cases you have "file_in" declared inside the if statement - after that ends, it falls out of scope. To clean this all up, get even more precise - just use the if statement to determine the actual string arguement that you will use for the ifstream constructor. Like this:string filename;if (gamedata.current_level == 1) filename = MAP1;else if (gamedata.current_level == 2) filename == MAP2;ifstream file_in(filename);if (!file_in) return false;// use the file  Of course, if you''re going to have several of these things, then you really really don''t want to write a big if statement for it. No, you don''t want a switch, either. You want something like:string myfilenames[NUM_FILES];myfilenames[0] = "map1.txt";myfilenames[1] = "map2.txt";// etc. I.e., initialize an array of these things...ifstream file_in(myfilenames[gamedata.current_level - 1]);// -1 because array indexing starts at zero. You could number your levels starting with zero instead, I guess :/if (!file_in) return false;// Otherwise, use file.  See how easy...quote:one more thing - the way my LoadMap function is now, it gives me a warning "not all control paths have a return value". well thats bullshit because they do! dont they?They most assuredly do not. You haven''t considered the possibility that gamedata.current_level is something other than 1 or 2. Well, you may make sure that it is by the way that you write the rest of your code, but the compiler isn''t anywhere near that smart.;  thanks for your help, but im still having problems. i decided before to change the way everything worked, to test (again) it and see if i could really replace the MAP1 part with a string. well it doesnt work, just as it didnt before. how do i get the ifstream to accept a string? if i do #define MAP1 "Map1.txt"it works fine. if i do string MAP1 = "Map1.txt", i get errors. they say something like"error C2664: ''__thiscall std::basic_ifstream >::std::basic_ifstream >(const char *,int)'' : cannot convert parameter 1 from ''class std::basic_string,class std::allocator >'' to ''const char *''   No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called"so how do i do a ifstream file_in(MAP1); without having MAP1 DEFINED as "Map1.txt", or without having to actually put "Map1.txt". i want MAP1 to be a VARIABLE, NOT a DEFINE. i cannot do what i want with DEFINES. Thanks for any help!!!