Home > Mobile >  c compiled file doesn't work in other pcs
c compiled file doesn't work in other pcs

Time:10-08

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;

}

CodePudding user response:

Your code can exhibit screenie

There must be something wrong with the compiling process. Does it actually work on your computer? If it does, then there must be something with the DLLs linking that your friends aren't able to open it.

  •  Tags:  
  • c
  • Related