while loop help

Hi. I'm working on this assignment where I have modified this knock-off version of the Angry Birds game several times. I am on the last step which is this:

6. Modify the program to give the user at most 4 tries to hit the swine. If the swine is hit, then stop the loop.

I know I need to add another end condition to my do-while loop (at the bottom of the code) but I am not sure how to make the loop stop after 4 times. I have tried several things and am stuck. Any tips would be greatly appreciated. My current loop starts at line 104. Thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;

const double pi = 3.14159265;
const double grav = 9.8; // Earth gravity (m/s^2)

// Given time, angle, velocity, and gravity
// Update x and y values
void Trajectory(double t, double a, double v,
                double& x, double& y) {
   x = v * t * cos(a);
   y = v * t * sin(a) - 0.5 * grav * t * t;
   return;
}

// convert degree value to radians
double DegToRad(double deg) {
    return ((deg * pi) / 180.0);
}

// print time, x, and y values
void PrintUpdate(double t, double x, double y) {
    cout << "Time " << fixed << setprecision(0)
         << setw(3) << t << "   x = " << setw(3)
         << x << "   y = " << setw(3) << y << endl;
    return;
} 


void PrintIntro(){
    cout << "Welcome to Upset Fowl!\n";
    cout << "The objective is to hit the mean Swine by launching an Upset Fowl.\n";
}


void GetUsrInput(double swineX, double fowlAngle, double fowlVel){
    cout << "\nThe Mean Swine is " << swineX << " meters away.\n";
    cout << "Enter fowl launch angle (deg): ";
    cin >> fowlAngle;
    fowlAngle = DegToRad(fowlAngle); // convert to radians
    cout << "Enter fowl launch velocity (m/s): ";
    cin >> fowlVel;
}


double LaunchFowl(double fowlAngle, double fowlVel){
     double t = 1.0; // time (s)
     double fowlX = 0.0; // object's horiz. dist. from start (m)
     double fowlY = 0.0; // object's height above ground (m)
     double fowlLandingX = 0.0;
     
    do {
        PrintUpdate(t, fowlX, fowlY);
        Trajectory(t, fowlAngle, fowlVel, fowlX, fowlY);
        t=t+1.0;
    } while ( fowlY > 0.0 ); // while above ground
    PrintUpdate(t, fowlX, fowlY);


    fowlLandingX = fowlX;
   return fowlLandingX;
}

 bool DtrmIfHit(double fowlLandingX, double swineX){
    bool didHitSwine = false;
    double beforeSwineX = 0.0;
    
    beforeSwineX = swineX - 30;
    if ((fowlLandingX <= swineX) && (fowlLandingX >= beforeSwineX)) {
        cout << "Hit'em!!!" << endl;
        didHitSwine = true;
    } else {
        cout << "Missed'em..." << endl;
        didHitSwine = false;
    }
     return didHitSwine;
 }
 
int main() {
    
    
    double fowlAngle = 0.0; // angle of launch of fowl (rad)
    double fowlVel = 0.0; // velocity of fowl (m/s)
    double fowlLandingX = 0.0;
    double swineX = 0.0; // distance to swine (m)
    bool didHitSwine = false; // did hit the swine?
    
    srand(time(0));
    swineX = (rand() % 201) + 50;

    
   PrintIntro();

   GetUsrInput(swineX, fowlAngle, fowlVel);
    
   fowlLandingX = LaunchFowl(fowlAngle, fowlVel);

   didHitSwine = DtrmIfHit(fowlLandingX, swineX); 
 
   do {
        LaunchFowl(fowlAngle, fowlVel);
        DtrmIfHit(fowlLandingX, swineX);
        GetUsrInput(swineX, fowlAngle, fowlVel);
   }
   while (didHitSwine == false);
 
    return 0;
}
Last edited on
Your GetUsrInput function doesn't change the values of fowlAngle or fowlVel in main. If you pass the values by reference, this will be possible:

e.g.
1
2
3
4
GetUsrInput(double swineX, double& fowlAngle, double& fowlVel)
{
    // ...
}


Likewise, you never do anything with the return value of LaunchFowl or DtrmIfHit while in your while loop.

See how you're repeating the code on lines 98-102 and lines 105-107?
Why not just do something like
1
2
3
4
5
6
7
8
PrintIntro();

do {
   GetUsrInput(swineX, fowlAngle, fowlVel);   
   fowlLandingX = LaunchFowl(fowlAngle, fowlVel);
   didHitSwine = DtrmIfHit(fowlLandingX, swineX); 
}
while (didHitSwine == false);


If you want a maximum number of tries, you can do something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int num_tries = 0;
PrintIntro();

do {
   GetUsrInput(swineX, fowlAngle, fowlVel);   
   fowlLandingX = LaunchFowl(fowlAngle, fowlVel);
   didHitSwine = DtrmIfHit(fowlLandingX, swineX); 
   num_tries++;
}
while (didHitSwine == false && num_tries < 4);

if (didHitSwine)
{
    cout << "Congrats!\n";
}
else
{
    cout << "Failure!\n";
}


This way, the loop will end once didHitSwine becomes true, or num_tries becomes 4.
Last edited on
Ahhhh this helped me so much! In the back of my mind, I knew I might need a pass by reference somewhere, but just didn't know where/how to incorporate it. This helped me figure it out, thank you!!
Topic archived. No new replies allowed.