Loop Control

Page 1 of 1

4 Replies - 1749 Views - Last Post: 26 October 2006 - 06:15 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=20077&amp;s=494d21c9e9ba325799715cbb9b273efb&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 seawolf275

• New D.I.C Head

Reputation: 0
• Posts: 4
• Joined: 24-October 06

Loop Control

Posted 24 October 2006 - 08:07 PM

I am having some difficulty with this program . I am to creat a pupkin shoot program and I thought I have it figured out. I can't seem to get out of main loop to get the equation to work out. I don't expect the answer...just some guidance. thanks
Seawolf

(attached file included)
```#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
using namespace std;

int attempts = 0;
double computeDistance(double &Velocity , double &toRadians); // loop
double toRadians(double Angle, const double PI = acos(-1.));
double TargetDistance;
double Velocity;
double Angle;
double YourHit;
double radians;
double TargetRange;
double HiTargetRange;
double LoTargetRange;

int main() {
cout << "Program for The Granbde Pumpkin Shoot.\n\n";
cout << "Program by John M. Grow Cs-07-1.\n\n";

cout<<"Enter the Target Distance.\n";
cin >> TargetDistance;
HiTargetRange = (TargetDistance + (TargetDistance * .001));
LoTargetRange = (TargetDistance - (TargetDistance * .001));

cout<<"\nYou only have 5 chances to get this right.\nSo think carefully before you enter you data.\n\n\n";

while (attempts < 5){
cout <<"\nEnter the velocity (speed) of the pumpkin:   ";
cin >> Velocity;
cout <<"Now enter the angle of the cannon:		   ";
cin >>Angle;
attempts ++;

double YourHit = computeDistance(Velocity , toRadians); // loop

cout << YourHit;

}

// end of program results   win try again lose
if (YourHit <= HiTargetRange && YourHit >= LoTargetRange ){
cout <<"\n\n	  GOOD JOB........YOU WIN\n\n";
return 1;
}
else if (attempts >= 6){
cout <<"\a\n\n\aGAME OVER. INSERT ANOTHER QUARTER TO CONTINUE.";
return 2;
}
}

double computeDistance(double &Velocity , double toRadians ){

double YourHit;
double speed;

speed = pow(Velocity, 2);
YourHit = (speed * sin(2*radians))/32.2;
return YourHit;
}

double toRadian(double Angle, const double PI = acos(-1.)){
double radians;
radians = (Angle*PI)/180;
return radians;
}
```

Is This A Good Question/Topic? 0

Replies To: Loop Control

#2 dragonlady

• D.I.C Head

Reputation: 0
• Posts: 57
• Joined: 07-August 05

Re: Loop Control

Posted 24 October 2006 - 08:39 PM

Forgive me, but I'm not sure I understand the question. I see you do have a loop in the main function, but from within that loop you are calling computeDistance, which seems to be the answer to your question (?).

However I do see a potential compiler error. In the line:
double YourHit = computeDistance(Velocity , toRadians);
you don't have a variable called toRadians. Do you mean to pass Angle.toRadians? Also, you have some inconsistencies where you're saying that the computeDistance function takes in references as parameters, but you're not actually passing it references. You might get some compiler errors from that.
Was This Post Helpful? 0

#3 seawolf275

• New D.I.C Head

Reputation: 0
• Posts: 4
• Joined: 24-October 06

Re: Loop Control

Posted 25 October 2006 - 10:59 AM

What I was trying to accomplish was have the loop within main, give me a distance. The loops calls for a conversion of angles into radians; hence the second loop toRadians.

I redid the code and it works...somewhat now. For some reason when you hit attemp #6 is just ends, It doesn't cout the "END OF GAME". And my formula doesn't seem to be working well. I have attached the new code. If you could take a look at it and tell me what I am doing wrong I would appreciate it.

Thanks
seawolf

```#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
using namespace std;

int attempts = 0;
double computeDistance(double &Velocity , double &Angle); // loop
const double PI = acos(-1.);
double toRadians(double Angle, double PI);
double TargetDistance;
double Velocity;
double Angle;
double YourHit;
double radians;
double TargetRange;
double HiTargetRange;
double LoTargetRange;

int main() {
cout << "Program for The Granbde Pumpkin Shoot.\n\n";
cout << "Program by John M. Grow Cs-07-1.\n\n";

cout<<"Enter the Target Distance.\n";
cin >> TargetDistance;
HiTargetRange = (TargetDistance + (TargetDistance * .001));
LoTargetRange = (TargetDistance - (TargetDistance * .001));

cout<<"\nYou only have 5 chances to get this right.\nSo think carefully before you enter you data.\n\n\n";

while (attempts < 5){

cout <<"\nEnter the velocity (speed) of the pumpkin:   ";
cin >> Velocity;
cout <<"Now enter the angle of the cannon:		   ";
cin >>Angle;
attempts ++;

if (attempts > 5){
cout <<"\a\n\n\aGAME OVER. INSERT ANOTHER QUARTER TO CONTINUE.";
return 2;
}
if (YourHit <= HiTargetRange && YourHit >= LoTargetRange ){
cout <<"\n\n\a\a	  GOOD JOB........YOU WIN\n\n";
return 1;
}

double YourHit = computeDistance(Velocity , Angle); // loop
cout << YourHit;

double angle = toRadians(Angle, PI);
cout << angle;

if (TargetDistance > YourHit){
YourHit = TargetDistance - YourHit;
cout <<"\nYou missed by:  "<<YourHit<<"\a\ntry again.\n";
}
if (TargetDistance < YourHit){
YourHit = YourHit - TargetDistance;
cout <<"\nYou missed by:  "<<YourHit<<"\a\ntry again.\n";
}
}
}

double computeDistance(double &Velocity , double &Angle ){

double YourHit;
double speed;

speed = pow(Velocity, 2);
YourHit = (speed * sin(2*radians))/32.2;
return YourHit;
}

double toRadians(double Angle, double PI){
double radians;
radians = (Angle*PI)/180;
return radians;
}
```

Edit: Added code tags. ~Videege
Was This Post Helpful? 0

#4 Amadeus

• g+ + -o drink whiskey.cpp

Reputation: 249
• Posts: 13,507
• Joined: 12-July 02

Re: Loop Control

Posted 26 October 2006 - 06:16 AM

It's not printing your exit message becasue your while loop runs while the number of attempts is less than 5. Starting from a 0 point for the variable attempt, the value is 5 on the sixth attempt. As such, the loop is not run. The if statement you are using to check to see if the exit message should be displayed is inside the while loop...since the loop is not executed, the check is not performed, and the exit message not displayed. Even if the loop were run, the if statement is checking for a value greater than 5 - at that time, the value would be 5.
Was This Post Helpful? 0

#5 seawolf275

• New D.I.C Head

Reputation: 0
• Posts: 4
• Joined: 24-October 06

Re: Loop Control

Posted 26 October 2006 - 06:15 PM

I finally got the program to work. Thanks for the help. If you are interested in how is looks/works it is in the attached file. Thanks again.

Seawolf
Was This Post Helpful? 0

Page 1 of 1

 .related ul{list-style-type:circle;font-size:12px;font-weight:bold;}.related li{margin-bottom:5px;background-position:left 7px!important;margin-left:-35px;}.related h2{font-size:18px;font-weight:bold;}.related a{color:blue;}