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.