Jump to content


Photo

Odd issue with C++ program


  • Please log in to reply
3 replies to this topic

#1 Copaman

Copaman

    Ryan

  • Project Team
  • 2,144 posts
  • Location:Lehigh University
  • Projects:Winning.
  •  Slowly becoming a Veteran

Posted 24 September 2010 - 05:33 PM

So I have a program assessment due on Monday at 5PM EST.

Assignment is:

Write a C++ program that does the following:

a. Prompts the user for the distance (in meters) to a target.

b. Prompts the user for an angle (in degrees) to fire the cannon. (To use the equations given below, an angle of zero is straight up.)

c. Calculates, for the angle entered by the user, the distance traveled, the maximum height reached, and the time traveled by the cannon ball (see equations below), for initial velocities of 15, 25, 35, and 45 m/s.

d. Checks whether, for any of the initial velocities and the given angle, the cannon ball will land within 10 meters of the target distance.

e. Writes a table to the screen and to a file named result.txt using the following format:

A table title listing the angle (in degrees) and the target distance (in meters), with the table having the following five columns, with appropriate labels:

Column 1: the initial velocity in m/s.

Column 2: the maximum height reached in meters.

Column 3: the time traveled in seconds.

Column 4: the final distance in meters.

Column 5: the word HIT if the ball lands within 10 meters of the target, and MISS if it does not.

Note: The table will have four rows (not counting the headings), one for each value of initial velocity. Allow two digits to the right of the decimal point for all results.

f. The program repeats steps b, c, d, and e for two more angles, and then terminates. Your final result.txt file will therefore have three tables, one for each angle entered by the user.




I've got the solid majority of the code written but for some odd reason my "Distance" column is printing wrong. None of the other variable are having issues with outputting to screen. EDIT: Apparently Height and Time columns print the same last three values every time (the values are different by row but not column). The first value in each column changes as it should. My code is as follows:

#include<iostream>
#include<cmath>
#include<fstream>
#include<iomanip>

using namespace std;
int main(){
	
	ofstream toText("result.txt");
	
	double target, angle, distance, height, time, velocity;
	
	int count,;
	
	cout << "Please set the distance (in meters) of your target from the cannon." << endl;
	cout << "Distance: ";
	cin >> target;
	
	for(count=1; count!=4; count++){
				 
				 cout << endl<<endl;
				 cout << "At what angle (from the vertical) would you like to set the cannon?" << endl;
				 cout << "Angle: ";
				 cin >> angle;
				 cout << endl<<endl;
				 
				 cout << "Your cannon is set to "<<angle<<" degrees from the vertical."<<endl;
				 cout << "Your target is "<<target<<" meters away."<<endl<<endl;
				 
				 cout<<fixed<<setw(18)<<"Velocity (m/s)";
				 cout<<setw(14)<<"Height (m)";
				 cout<<setw(12)<<"Time (s)";
				 cout<<setw(16)<<"Distance (m)";
				 cout<<setw(12)<<"Hit/Miss"<<endl<<endl;
				 
				 for(velocity=15.; velocity !=55.; velocity = velocity + 10.){
								  
								  angle = ((3.141592)/180.)*angle;
								  
								  height = (pow(cos(angle),2.)*velocity)/9.81;
								  
								  time = (2*cos(angle)*velocity)/9.81;
								  
								  distance = (velocity*sin(angle))/time; //distance calculation
								  
								  cout<<setprecision(2)<<setw(13)<<velocity;
								  cout<<setprecision(2)<<setw(16)<<height;
								  cout<<setprecision(2)<<setw(13)<<time;
								  cout<<setprecision(2)<<setw(16)<<distance; //output to screen of distance
								  
								  if(distance>=target-10 && distance<=target+10){
														 cout<<setw(12)<<"Hit"<<endl;
														 }
								  else{
									   cout<<setw(13)<<"Miss"<<endl;
									   }
								  
								  }
				 
				 
				 
				 
				 }
  
  
system("pause");
return 0;
}


Where's the issue?

Edited by Copaman, 24 September 2010 - 06:37 PM.

Posted Image

 

If you meet me:

Have some courtesy,

Have some sympathy,

And some taste.

Use all your well-learned politesse,

Or I'll lay your soul to waste.


#2 Beowulf

Beowulf

    Shipgirl

  • Advisors
  • 7,219 posts
  •  Azur Lane Fangirl

Posted 24 September 2010 - 10:59 PM

It may not be an issue, but I suggest using different variable names for your input variables. Something about setting angle equal to something and angle doesn't seem right. Might just be me though.

NZ.org | BBPCG
Discord: The Astronomer#1314
Steam


#3 Copaman

Copaman

    Ryan

  • Project Team
  • 2,144 posts
  • Location:Lehigh University
  • Projects:Winning.
  •  Slowly becoming a Veteran

Posted 24 September 2010 - 11:46 PM

It may not be an issue, but I suggest using different variable names for your input variables. Something about setting angle equal to something and angle doesn't seem right. Might just be me though.


Yeah that was the issue. By simply creating a new double "radian" and changing that line to radian = angle * ([pi]/180) I fixed everything.

Edited by Copaman, 25 September 2010 - 01:07 AM.

Posted Image

 

If you meet me:

Have some courtesy,

Have some sympathy,

And some taste.

Use all your well-learned politesse,

Or I'll lay your soul to waste.


#4 Beowulf

Beowulf

    Shipgirl

  • Advisors
  • 7,219 posts
  •  Azur Lane Fangirl

Posted 25 September 2010 - 02:47 AM

Excellent. :p

NZ.org | BBPCG
Discord: The Astronomer#1314
Steam





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users