1

The title pretty much says it all.

I am learning C++ at the moment and decided to try to make a game. My friend who also wants to learn C++, but does not have VS installed tried to run it, and it crashed on start up.

It was fine when I ran it from outside VS, and I sent it to him in a ZIP file with all the DLLs and in a release build. Unsure why this is happening, anyone have any ideas?

Here is the code, its fairly badly written and the readability is poor (Sorry.)

#include "stdafx.h"
#include <SFML/Graphics.hpp>
#define _WIN32_WINNT 0x0500
#include <windows.h>
#include <iostream>

int main()
{
    HWND hWnd = GetConsoleWindow();
    ShowWindow(hWnd, SW_HIDE);

    bool gameover(1);

    sf::RenderWindow window(sf::VideoMode(240, 500), "Hangman but not very good.");
    sf::Text text;
    sf::Font font;
    sf::Sprite sprite;
    sf::Texture texture;

    texture.loadFromFile("0.png");

    sprite.setTexture(texture);

    char uInput[11] = {0};
    char displayed[11] = {0};
    bool setup = false;
    int charCount(0);
    int iii(0);
    bool correct(0);
    long long wrongGuesses(0);
    int wordlength(0);
    long long gameoverCounter(0);
    char currentGuess = 0;
    bool bypass = true;
    char pngC = '1';
    char IOarray[] = "x.png";
    bool firstSetup = true;
    int correctCounter = 0;

    font.loadFromFile("Xefus.ttf");
    text.setColor(sf::Color::Yellow);
    text.setCharacterSize(24);
    text.setFont(font);
    text.setString("Please enter the word to guess, press enter to stop");

    while(window.isOpen())
    {
        sf::Event event;
        while(gameover)
        {
            while(window.pollEvent(event))
            {
                if(event.type == sf::Event::Closed)
                    window.close();

                if(event.type == sf::Event::TextEntered)
                {
                    bypass = true;

                    if(iii < 10 && !setup)
                        if(event.text.unicode < 128 && event.text.unicode > 96)
                        {
                            uInput[iii] = static_cast<char>(event.text.unicode);
                            ++iii;
                            ++wordlength;
                        }

                    {
                        for(int i = 0; i < wordlength; ++i)
                            if(event.text.unicode == uInput[i] && !firstSetup)
                            {
                                displayed[i] = uInput[i];
                                bypass = false;
                                ++correctCounter;
                            }
                            else if(bypass = true)
                                ++wrongGuesses;
                    }
                }

                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Return))
                    setup = true;

                if(sf::Keyboard::isKeyPressed(sf::Keyboard::BackSpace))
                {
                    uInput[iii] = 0;
                    iii -= 1;
                }
            }

            if(!setup)
            {
                text.setString(uInput);
                displayed[wordlength - 1] = ('*');
                setup = false;
            }
            else
            {
                text.setString(displayed);
                bypass = false;

                if(firstSetup)
                {
                    firstSetup = false;
                    gameoverCounter = 0;
                    wrongGuesses = 0;
                    correctCounter = 0;
                }

                if(wrongGuesses >= wordlength)
                {
                    ++gameoverCounter;
                    wrongGuesses = 0;
                    IOarray[0] = static_cast<char>(gameoverCounter + 48);

                    texture.loadFromFile(IOarray);
                }

                if(gameoverCounter >= 9)
                {
                    return 0;
                    window.clear();
                    window.draw(sprite);
                    window.draw(text);
                    window.display();
                }

                if(correctCounter >= wordlength)
                {
                    return 0;
                    window.clear();
                    window.draw(sprite);
                    window.draw(text);
                    window.display();
                }

                wrongGuesses = 0;
            }

            window.clear();
            window.draw(sprite);
            window.draw(text);
            window.display();
        }
    }
}
nvoigt
  • 61,531
  • 23
  • 73
  • 116
Joel Bevan
  • 31
  • 7
  • 1
    What's the exact error message appearing when you run that program on a different PC? – πάντα ῥεῖ May 02 '15 at 10:17
  • Have you tried to include all dependencies in a [statically linked](http://stackoverflow.com/questions/37398/how-do-i-make-a-fully-statically-linked-exe-with-visual-studio-express-2005) exe? – mins May 02 '15 at 10:18
  • Thanks for the reply, should i statically link SFML or just that which was mentioned in the thread you posted? @mins? – Joel Bevan May 02 '15 at 10:34
  • Just asked him, looks like he says that the graphics dll is missing. He checked the file and its there, not sure if it has something to do with the linking or not (I did dynamic as I do not know how to static link them.) @πάνταῥεῖ – Joel Bevan May 02 '15 at 10:45
  • @JoelBevan If the necessary DLL is available, your friend may need to add the directory where it is to their `PATH` environment variable. – πάντα ῥεῖ May 02 '15 at 10:47
  • SFML is likely to be the missing one, but the exact error message would help here. VC runtime libraries are supposed to be present in a standard Windows installation not too old. Else they can be [downloaded](https://www.microsoft.com/en-us/download/details.aspx?id=40784) and installed by your friend once for all. – mins May 02 '15 at 10:47
  • @mins Fairly sure that graphics DLL is missing, but that is the only one I included in code. I would try statically linking it if I knew how, but have not been able to dig up any info on it. Thanks again for the reply. – Joel Bevan May 02 '15 at 10:53
  • If that helps (: http://stackoverflow.com/questions/2035083/compile-to-a-stand-alone-executable-exe-in-visual-studio#2035183 (Pierre-Luc Champigny answer). – mins May 02 '15 at 11:01

2 Answers2

0

One of the reasons for this type of problem is missing dlls. So make sure you have all dependent dlls available on the system.

One common mistake that we do while copying the dependent libraries is that we forget to copy c++ run time dlls which is required to run any C++ application. This library is installed with the visual studio, that is why the application is running on your system but not on your friend's.

C++ run time libraries are: msvcr, msvcrt, msvcp

Also you can use Dependency Walker software to check for any other missing dependencies.

Hope it helps

Vikash Kesarwani
  • 805
  • 4
  • 13
0

Vikash is right, this is a case of missing runtime libraries. What you need is to install the right Redistributable package in your target computer.

Google and download the appropriate version for your application:

"Visual C++ Redistributable for Visual Studio YYYY" where YYYY is the year of your Visual Studio.

Once downloaded, install this on your target computer then run your application.

Andrew

  • Is there any way around this, or is it standard for all applications? – Joel Bevan May 02 '15 at 14:03
  • That's the easiest way. The harder way is to use the Dependency Walker, identify what DLLs the application needs then create an installer/package. The package would contain all the dependent libraries together with your application in the same folder. – Andrew Artajos May 02 '15 at 14:10