fork download
  1. // artillery.cpp, Maggie Johnson
  2. // Description: A simple game with a little physics
  3. #include
  4. #include
  5. #include
  6. using namespace std;
  7.  
  8. const int kNumShells = 10; // allowed 10 shells per target
  9. const int kMinDist = 200; // min distance for a target
  10. const int kMaxDist = 900; // max distance for a target
  11. const double kVelocity = 200.0; // initial velocity of 200 ft/sec
  12. const double kGravity = 32.2; // gravity for distance calculation
  13. const double kPi = 3.1415;
  14.  
  15. // Returns the distance a shot travels given its angle.
  16. int DistanceCalc (double in_angle) {
  17.  
  18. double time_in_air;
  19. // The following calculates how far the shot goes given
  20. // its angle of projection, velocity, and how long it stays
  21. // in the air.
  22. time_in_air = (2.0 * kVelocity * sin(in_angle)) / kGravity;
  23. return (int) round((kVelocity * cos(in_angle)) * time_in_air);
  24. }
  25.  
  26. // Get user's angle input and calculates distance where shot lands.
  27. // Returns the distance the shot lands.
  28. int CheckShot() {
  29.  
  30. int distance;
  31. double angle;
  32. cout << "What angle? " << endl;
  33. if (!(cin >> angle))
  34. return -1;
  35.  
  36. // Convert to radians.
  37. angle = (angle * kPi) / 180.0;
  38. distance = DistanceCalc(angle);
  39. return distance;
  40. }
  41.  
  42. // Generate a random number for the enemy position.
  43. int Initialize() {
  44. int enemy_position;
  45.  
  46. // Initialize random seed.
  47. srand (time(NULL));
  48.  
  49. // Generate random number between kMinDist and kMaxDist
  50. enemy_position = rand() % kMaxDist + kMinDist;
  51. cout << "The enemy is " << enemy_position << " feet away!!!" << endl;
  52. return enemy_position;
  53. }
  54.  
  55. // This function plays the game
  56. int Fire(int number_killed) {
  57.  
  58. int enemy, shots, hit;
  59. int distance;
  60.  
  61. // Initialize variables.
  62. shots = kNumShells;
  63. enemy = Initialize();
  64. distance = 0;
  65. hit = 0;
  66.  
  67. do {
  68. // Get the distance where shot lands & compare it to enemy position.
  69. distance = CheckShot();
  70. // Some error checking on the input.
  71. if (distance == -1) {
  72. cout << "Enter numbers only..." << endl;
  73. cin.clear();
  74. cin.ignore(10000,'\n');
  75. continue;
  76. }
  77. // Compare the enemy position with the computed distance.
  78. if (abs(enemy - distance) <= 1) {
  79. hit = 1;
  80. number_killed++;
  81. cout << "You hit him!!!" << endl;
  82. cout << "It took you " << kNumShells - shots + 1 << " shots." << endl;
  83. cout << "You have killed " << number_killed << " enemies." << endl;
  84. } else {
  85. shots--;
  86. if (distance > enemy) {
  87. cout << "You over shot by " << abs(enemy - distance) << endl;
  88. } else {
  89. cout << "You under shot by " << abs(enemy - distance) << endl;
  90. }
  91. }
  92. } while ((shots > 0) && (!(hit)));
  93. if (shots == 0)
  94. cout << "You have run out of ammo..." << endl;
  95. return number_killed;
  96. }
  97.  
  98. // This shows the introductory screen.
  99. void StartUp() {
  100. cout << "Welcome to Artillery." <> done;
  101. } while (done != 'n');
  102. cout << "You killed " << killed << " of the enemy." << endl;
  103. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:3:10: error: #include expects "FILENAME" or <FILENAME>
prog.cpp:4:10: error: #include expects "FILENAME" or <FILENAME>
prog.cpp:5:10: error: #include expects "FILENAME" or <FILENAME>
prog.cpp: In function 'int DistanceCalc(double)':
prog.cpp:22:48: error: 'sin' was not declared in this scope
   time_in_air = (2.0 * kVelocity * sin(in_angle)) / kGravity;
                                                ^
prog.cpp:23:47: error: 'cos' was not declared in this scope
   return (int) round((kVelocity * cos(in_angle)) * time_in_air);
                                               ^
prog.cpp:23:63: error: 'round' was not declared in this scope
   return (int) round((kVelocity * cos(in_angle)) * time_in_air);
                                                               ^
prog.cpp: In function 'int CheckShot()':
prog.cpp:32:3: error: 'cout' was not declared in this scope
   cout << "What angle? " << endl;
   ^
prog.cpp:32:29: error: 'endl' was not declared in this scope
   cout << "What angle? " << endl;
                             ^
prog.cpp:33:9: error: 'cin' was not declared in this scope
   if (!(cin >> angle))
         ^
prog.cpp: In function 'int Initialize()':
prog.cpp:47:15: error: 'NULL' was not declared in this scope
   srand (time(NULL));
               ^
prog.cpp:47:19: error: 'time' was not declared in this scope
   srand (time(NULL));
                   ^
prog.cpp:47:20: error: 'srand' was not declared in this scope
   srand (time(NULL));
                    ^
prog.cpp:50:25: error: 'rand' was not declared in this scope
   enemy_position = rand() % kMaxDist + kMinDist;
                         ^
prog.cpp:51:3: error: 'cout' was not declared in this scope
   cout << "The enemy is " << enemy_position << " feet away!!!" << endl;
   ^
prog.cpp:51:67: error: 'endl' was not declared in this scope
   cout << "The enemy is " << enemy_position << " feet away!!!" << endl;
                                                                   ^
prog.cpp: In function 'int Fire(int)':
prog.cpp:72:7: error: 'cout' was not declared in this scope
       cout << "Enter numbers only..." << endl;
       ^
prog.cpp:72:42: error: 'endl' was not declared in this scope
       cout << "Enter numbers only..." << endl;
                                          ^
prog.cpp:73:7: error: 'cin' was not declared in this scope
       cin.clear();
       ^
prog.cpp:78:29: error: 'abs' was not declared in this scope
     if (abs(enemy - distance) <= 1) {
                             ^
prog.cpp:81:7: error: 'cout' was not declared in this scope
       cout << "You hit him!!!" << endl;
       ^
prog.cpp:81:35: error: 'endl' was not declared in this scope
       cout << "You hit him!!!" << endl;
                                   ^
prog.cpp:87:9: error: 'cout' was not declared in this scope
         cout << "You over shot by " << abs(enemy - distance) << endl;
         ^
prog.cpp:87:65: error: 'endl' was not declared in this scope
         cout << "You over shot by " << abs(enemy - distance) << endl;
                                                                 ^
prog.cpp:89:9: error: 'cout' was not declared in this scope
         cout << "You under shot by " << abs(enemy - distance) << endl;
         ^
prog.cpp:89:66: error: 'endl' was not declared in this scope
         cout << "You under shot by " << abs(enemy - distance) << endl;
                                                                  ^
prog.cpp:94:5: error: 'cout' was not declared in this scope
     cout << "You have run out of ammo..." << endl;
     ^
prog.cpp:94:46: error: 'endl' was not declared in this scope
     cout << "You have run out of ammo..." << endl;
                                              ^
prog.cpp: In function 'void StartUp()':
prog.cpp:100:3: error: 'cout' was not declared in this scope
   cout <<  "Welcome to Artillery." <> done;
   ^
prog.cpp:100:37: error: expected primary-expression before '>' token
   cout <<  "Welcome to Artillery." <> done;
                                     ^
prog.cpp:100:39: error: 'done' was not declared in this scope
   cout <<  "Welcome to Artillery." <> done;
                                       ^
prog.cpp: At global scope:
prog.cpp:101:5: error: expected unqualified-id before 'while'
   } while (done != 'n');
     ^
prog.cpp:102:3: error: 'cout' does not name a type
   cout << "You killed " << killed << " of the enemy." << endl;
   ^
prog.cpp:103:1: error: expected declaration before '}' token
 }
 ^
stdout
Standard output is empty