mwahi
mwahi

Reputation: 5

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. However, when run in their pc, it says it needs some dll files. Even after downloading the dll files it needs it only runs for like 2 seconds and then closes. They don't have any compiler installed in their pc by the way.

Edit: I used -static flag while compiling and it worked. 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;

}

Upvotes: 0

Views: 119

Answers (2)

IneoSenigupta
IneoSenigupta

Reputation: 27

@mwahi, it seems that you have used some external library on your computer. You need to include some DLLs with your output program. If you have compiled it with Visual Studio, you have to include a few DLLs. 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

The dependency on the DLLs is expected, search for the names of the DLLs and "redistributable". The rest isn't expected, but there's far from enough info to diagnose the issue.

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. Tell your friends, to run it from a console indirectly, rather than opening it from the local folder.

Upvotes: 0

king_nak
king_nak

Reputation: 11513

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). Your array has 9 elements, indices 0-8. If the random number turns out to be 9 mod 10, you get undefined behaviour.

Additional notes:

  • You don't need to call srand before every rand. Call it only once at startup (cf. srand() — why call it only once?)
  • 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; to assign to both
  • The same issues are also in makecomputerturnbatting

Upvotes: 2

Related Questions