Okay, so the source computes a program that stimulates a duel. Basically, the cycle will complete until there is one man standing.. though the problem is, in order to return a winners name, we must use a string function. I've had this working when I made it a void function, but now, since its a string it doesnt even work. So if you guys were to overlook my code and tell me where I am going wrong, it will be much appriciated. thanks a lot!
Code:#include <iostream> #include <string> using namespace std; using std::string; void shoot(bool& targetAlive, double accuracy) // Single Shot Function { double r = 0.1*(rand()%11); // Computes Random Number if (r < accuracy) // Random Number Less Then Accuracy { targetAlive = false; } } string startDuel(bool aaronAlive, bool bobAlive, bool charlieAlive, double aa, double bb, double cc) { int round = 1; do { cout << "Round #" << round << " : " << endl; if (aaronAlive) { if (charlieAlive) { shoot(charlieAlive, .33); } else { shoot(bobAlive, .33); } } if (bobAlive) { if (charlieAlive) { shoot(charlieAlive, 0.50); } else { shoot(aaronAlive, 0.50); } } if (charlieAlive) { if (bobAlive) { shoot(bobAlive, 1.00); } else { shoot(aaronAlive, 1.00); } } round ++; } while ( (aaronAlive && bobAlive) || (aaronAlive && charlieAlive) || (bobAlive && charlieAlive)); if (aaronAlive) { return "Aaron "; } else if (bobAlive) { return "Bob "; } else if (charlieAlive) { return "Charlie "; } } int main () { srand((unsigned)time(NULL)); string winner_; int Aaron = 0, Bob = 0, Charlie = 0, i; winner_ = startDuel(true, true, true , 0.33, 0.50, 1.00); for (i=0;i<=1000;i++) { winner_; } cout << endl; cout << " -- RESULTS -- " << endl; cout << endl; cout << "Aaron won " << Aaron << " times !" << endl; cout << "Bob won " << Bob << " times !" << endl; cout << "Charlie won " << Charlie << " times !" << endl; cout << endl; system("pause"); return 0; }



LinkBack URL
About LinkBacks




