-6

I'm making this little text rpg game and i want to display this ascii as a death screen. I have made a c++ file that has that (i found the code online and i didn't want to put it in the main code just cause it'd be too long to place that whenever you die, it reads a .txt file with the ascii) and i need a way to execute it from the main cpp file when i run it. Is there a way? I have searched the web, but nothing i could clearly understand came out.

here's the code for the ascii if it helps:

#include <iostream>
#include <fstream>
#include <string>

std::string getFileContents (std::ifstream&);    /*Gets filecontents*/

int main(int argc, char *argv[])
{

    std::ifstream Reader ("ded.txt");             //Open file

    std::string Art = getFileContents (Reader);       //Get file

    std::cout << Art << std::endl;               //Print it to the screen

    Reader.close ();                           //Close file

    return 0;
}

std::string getFileContents (std::ifstream& File)
{
    std::string Lines = "";        //All lines

    if (File)                      //Check if everything is good
    {
        while (File.good ())
        {
            std::string TempLine;                  //Temp line
            std::getline (File , TempLine);        //Get temp line
            TempLine += "\n";                      //Add newline character

            Lines += TempLine;                     //Add newline
        }
    return Lines;
    }
    else                           //Return error
    {
        return "ERROR File does not exist.";
    }
}
Toni Pop
  • 11
  • 3
  • 4
    Put the code inside main in a separate function. Then call this new function from your program. – Olaf Dietsche Oct 29 '16 at 21:39
  • 1
    I'm voting to close this question as off-topic because lack of basic research. – Joshua Oct 29 '16 at 21:45
  • silly me, totally forgot that only pro programmers are allowed to post question. it was in the tos "you cannot post newb questions, yes? you'll get your question shut down, yes?" – Toni Pop Oct 29 '16 at 21:55
  • 1
    Hh well, in my experience a lot of StackOverflow users are either perfectionists or racists against low quality and newbie questions :3 To be honest, neither of those are bad though, if all newbie questions were allowed SO wouldn't have the quality that it has now. I know that it doesn't help beginners but everything they need is just a few keypresses away provided that they use the correct keywords on Google. – Konstantinos Kamaropoulos Oct 29 '16 at 22:01
  • 1
    watch out for this: `while (File.good ())`. All it tells you is the file is good *right now*. There is no guarantee that `std::getline (File , TempLine);` will succeed, which means `TempLine` could be used containing invalid data when you use it. Better if you read, then test. `std::string TempLine; while (std::getline (File , TempLine)){ Lines += "\n" + TempLine; }` will do what you want thanks to the magic of [operator bool](http://en.cppreference.com/w/cpp/io/basic_ios/operator_bool). – user4581301 Oct 29 '16 at 22:05
  • @ToniPop The TOS don't say anything about "newb questions" - nor did anybody else. However, the first paragraph in [ask] says `Search, and research` and it doesn't appear you did either. – BJ Myers Oct 29 '16 at 22:35
  • *"I have searched the web"* - The internet is good at many things. It is not very good at being a replacement for proper teaching material. With C++ that means books. Get inspired by browsing [The Definitive C++ Book Guide and List](http://stackoverflow.com/q/388242/1889329). – IInspectable Oct 29 '16 at 23:47
  • 1
    @KonstantinosKamaropoulos I like how following the rules of the site makes us all "racists against low quality." If you think we should be more inclusive of low-quality content, you're on the wrong site. – BJ Myers Oct 30 '16 at 02:59
  • In case you didn't read my full comment, I didn't say that this is a bad thing and that it's a way to keep only high quality content in the site. :) – Konstantinos Kamaropoulos Oct 30 '16 at 08:15

1 Answers1

2

There is no need to make a new program to print this on the screen...

And that's for a simple reason, it will actually print it on a different window and that's not what you want, right?

Another misunderstanding you have is that you don't execute .cpp
files. .cpp files contain source code that you have to compile in order to execute them. And again this is not how stuff works in C++.

So, to the solution.

On your programs code make a new function, let's say PrintDeathScreen. You have two options. Read the ASCII from the file as you would do (but I don't really recomend that) or hard-code it into a variable and then just print it on the screen. This will save you from the trouble to open and read the file.