简体   繁体   English

c++ 编译后的文件在其他电脑上不起作用

[英]c++ compiled file doesn't work in other pcs

I made a simple C++ program and compiled it, I shared the.exe file with my friends so that they can check it out as well.我做了一个简单的C++程序并编译,我把.exe文件分享给我的朋友们,让他们也看看。 However, when run in their pc, it says it needs some dll files.然而,当在他们的电脑上运行时,它说它需要一些 dll 个文件。 Even after downloading the dll files it needs it only runs for like 2 seconds and then closes.即使在下载 dll 文件后,它也只需要运行 2 秒左右,然后关闭。 They don't have any compiler installed in their pc by the way.顺便说一句,他们的电脑上没有安装任何编译器。

Edit: I used -static flag while compiling and it worked.编辑:我在编译时使用了 -static 标志并且它有效。 Thank you for your answers.谢谢您的回答。

#include <iostream>
#include <cstdlib>
#include <ctime>


int toss(int choice); 

void start(int choice); 

void printstats(int runs, int wickets, int balls, int target); 

int makecomputerturnbatting(int turn);

int makecomputerturnbowling(int turn);

int main(int agrc, char *agrv[]) {
    

    int choice; 
    

    do {
        std::cout << "0. Head\n1. Tails\nChoose a valid option: "; 
        std::cin >> choice; 
    } while (choice > 1);

    

    if (toss(choice)) {

        std::cout << "You won the toss.. " << std::endl;

        int choice; 

        do {
            std::cout << "0. Batting\n1. Bowling\nChoose a valid Option: ";
            std::cin >> choice;
        } while (choice > 1);

        start(choice); 

    } else {

        std::cout << "You lost the toss.." << std::endl;

        

        srand(time(0));

        start(rand() % 2);

    }

    return 0;
}

// Defining subroutines

int toss(int choice) {

    int faces[3] = {0, 1, 1}; 

    srand(time(0)); // srand for rand()
    int flippedface = faces[rand() % 4]; 

    if (flippedface == choice) { 
        return 1;
    } else {
        return 0;
    }

}

void start(int choice) {


    if (choice) { 
        std::cout << "\n || Starting game as bowling.. ||" << std::endl;

        int turn; 

        // gamestats
        int runs = 0;
        int balls = 0; 
        int wickets = 5; 
        int target; 



        do {


            std::cout << " || Your turn: ";
            

            do {
                std::cin >> turn;
            } while (turn > 6);


            std::cout << " ||";

            
            int computerturn = makecomputerturnbatting(turn);

            std::cout << "Computer's play: " << computerturn << std::endl;

            if (turn != 0) {
                if (turn == computerturn) {
                    wickets -= 1;
                    balls += 1;

                } else {
                    runs += computerturn;
                    balls += 1;
                }

            } else {

                std::cout << "\n|| Dot Ball! ||\n";
            }

            printstats(runs, wickets, balls, 0);
 
        } while (wickets != 0);

        target = runs;
        runs = 0;
        balls = 0;
        wickets = 5;
        
        // session 2
        do {
            std::cout << " || Playing as batsman now.. ||\n";
            std::cout << " || Your turn: ";
            

            do {
                std::cin >> turn;
            } while (turn > 6);


            std::cout << " ||";

            
            int computerturn = makecomputerturnbowling(turn);

            std::cout << "Computer's play: " << computerturn << std::endl;

            if (computerturn != 0) {
                if (turn == computerturn) {
                    wickets -= 1;
                    balls += 1;

                } else {
                    runs += computerturn;
                    balls += 1;
                }

            } else {

                std::cout << "\n|| Dot Ball! ||\n";
            }

            printstats(runs, wickets, balls, target);
 
        } while (wickets != 0 || runs == target);
        
        if (runs = target) {
            std::cout << "You won!" << std::endl;
        } else {
            std::cout << "You lost.." << std::endl;
        }

        // end of the game..


    } else {
        
        std::cout << "\n || Starting game as batting.. ||" << std::endl;

        int turn; // player's play

        int runs = 0;
        int balls = 0; 
        int wickets = 5; // we will keep it at 5..
        int target;

        // session 1

        do {


            std::cout << " || Your turn: ";
            

            do {
                std::cin >> turn;
            } while (turn > 6);


            std::cout << " ||";

            
            int computerturn = makecomputerturnbowling(turn);

            std::cout << "Computer's play: " << computerturn << std::endl;

            if (computerturn != 0) {
                if (turn == computerturn) {
                    wickets -= 1;
                    balls += 1;

                } else {
                    runs += computerturn;
                    balls += 1;
                }

            } else {

                std::cout << "\n|| Dot Ball! ||\n";
            }

            printstats(runs, wickets, balls, 0);
 
        } while (wickets != 0);

        target = runs;
        runs = 0;
        balls = 0;
        wickets = 5;

        do {


            std::cout << " || Your turn: ";
            

            do {
                std::cin >> turn;
            } while (turn > 6);


            std::cout << " ||";

            // generating computer's play
            
            int computerturn = makecomputerturnbatting(turn);

            std::cout << "Computer's play: " << computerturn << std::endl;

            if (turn != 0) {
                if (turn == computerturn) {
                    wickets -= 1;
                    balls += 1;

                } else {
                    runs += computerturn;
                    balls += 1;
                }

            } else {

                std::cout << "\n|| Dot Ball! ||\n";
            }

            printstats(runs, wickets, balls, target);
 
        } while (wickets != 0 || runs == target);

        if (runs = target) {
            std::cout << "You lost.." << std::endl;
        } else {
            std::cout << "You won!" << std::endl;
        }

    }

}

void printstats(int runs, int wickets, int balls, int target) {
    /*

    ==========

    Runs : int runs
    Wickets : int wickets
    Balls : int balls
    Target : int target

    ==========

    */

    std::cout << "==========" << std::endl;
    std::cout << "          " << std::endl;
    std::cout << " || Runs : " << runs << std::endl;
    std::cout << " || Wickets : " << wickets << std::endl;
    std::cout << " || Balls : " << balls << std::endl;
    if (target) {std::cout<<"|| Target : " << target << std::endl;}
    std::cout << "          " << std::endl;
    std::cout << "==========" << std::endl;

}

int makecomputerturnbatting(int turn) {    



    int computerturn;
    int likelyturn;
    int possibleturns[10] = {0, 0, 1, 2, 3, 4, 5, 6};

    if (turn == 6) {
        likelyturn = turn - 1;
    } else {
        likelyturn = turn + 1;
    }

    possibleturns[9], possibleturns[10] = likelyturn;


    srand(time(0));
    
    computerturn = possibleturns[rand()%11];


    return computerturn;


}

int makecomputerturnbowling(int turn) {

    int computerturn;
    int possibleturns[9] = {0, 1, 2, 3, 4, 5, 6};

    possibleturns[8], possibleturns[9] = turn;


    srand(time(0));
    computerturn = possibleturns[rand()%10];

    return computerturn;

}

Your code can exhibit undefined behaviour , namely an out of bounds array access:您的代码可能会出现未定义的行为,即越界数组访问:

int possibleturns[9] = {0, 1, 2, 3, 4, 5, 6};
// ...
computerturn = possibleturns[rand()%10];

rand()%10 is an integer between 0-9 (inclusive). rand()%10是 0-9(含)之间的 integer。 Your array has 9 elements, indices 0-8.您的数组有 9 个元素,索引为 0-8。 If the random number turns out to be 9 mod 10, you get undefined behaviour.如果随机数结果为 9 mod 10,则会出现未定义的行为。

Additional notes:附加条款:

  • You don't need to call srand before every rand .您不需要在每个rand之前调用srand Call it only once at startup (cf. srand() — why call it only once? )在启动时只调用一次(参见srand() — 为什么只调用一次?
  • possibleturns[8], possibleturns[9] = turn; doesn't do what you expect it to do.不做你期望它做的事。 It only assigns to index 9. You want possibleturns[8] = possibleturns[9] = turn;它只分配给索引 9。你想要possibleturns[8] = possibleturns[9] = turn; to assign to both分配给两者
  • The same issues are also in makecomputerturnbatting同样的问题也出现在makecomputerturnbatting

@mwahi, it seems that you have used some external library on your computer. @mwahi,您似乎在计算机上使用了一些外部库。 You need to include some DLLs with your output program.您需要在 output 程序中包含一些 DLL。 If you have compiled it with Visual Studio, you have to include a few DLLs.如果您使用 Visual Studio 编译它,则必须包含一些 DLL。 This page from Microsoft here explains how to link DLLs directly to your output program: https://learn.microsoft.com/en-us/cpp/build/linking-an-executable-to-a-dll?view=msvc-160 Microsoft 的此页面在这里解释了如何将 DLL 直接链接到您的 output 程序: https://learn.microsoft.com/en-us/cpp/build/linking-an-executable-to-a-dll?view=msvc- 160

The dependency on the DLLs is expected, search for the names of the DLLs and "redistributable".对 DLL 的依赖是预期的,搜索 DLL 的名称和“可再发行”。 The rest isn't expected, but there's far from enough info to diagnose the issue. rest 不是预期的,但没有足够的信息来诊断问题。

Also, to answer your question on "it opens only for 2 seconds and it closes", its probably because of a console program, that displays some stuff and closes.另外,要回答关于“它只打开 2 秒然后关闭”的问题,可能是因为控制台程序显示了一些内容并关闭了。 Tell your friends, to run it from a console indirectly, rather than opening it from the local folder.告诉你的朋友,从控制台间接运行它,而不是从本地文件夹打开它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM