#include <iostream>
#include <cmath>
#include <string.h>
#include <limits.h>
#include <iomanip>
usingnamespace std;
constdouble gravity = 9.8;
int main()
{
//Declaration
{
double angleDeg = 0.0, v = 0.0, angleRad = 0.0, R = 0.0, x = 0.0, y = 0.0, T = 0.0, t = 0.0;// v = velocity
bool done = false;
//input
cout <<"What's the angle?" <<endl;
cin >> angleDeg;
while (angleDeg < 0 || angleDeg > 90){ // between 0 to 90
cout << "Between 0~90 angle please :)" << endl; // Seems like this line is ignoring the value..?
while(!(cin >> angleDeg)) // Run this loop as long as the cin value fails.
{
cin.clear(); // if fails, clear the failed flag.
while(cin.get() != '\n'){} // A loop the cleans out anything that is left over in the input stream.
cout << "Only numbers please: " << endl << "What's the angle again?" <<endl;// Output failed prompt.
}
}
cout << "Velocity?" <<endl;
cin >> v;
while (v < 0){ // between 0 to 90
cout << "You know you can't go negative.. :)" << endl;
while(!(cin >> v)) // Run this loop as long as the cin value fails.
{
cin.clear(); // if fails, clear the failed flag.
while(cin.get() != '\n'){} // A loop the cleans out anything that is left over in the input stream.
cout << "Only numbers please: " << endl << "What's the velocity again?" <<endl;// Output failed prompt.
}
}
//processing
angleRad = angleDeg*M_PI/180.0;
R = (pow(v, 2.0)/gravity)*sin(2.0*angleRad); // = (v^2/g)sin(2x) Trajectory formula
T = (2.0*v*sin(angleRad))/gravity; // Total time
x = (v*cos(angleRad)); // x coordinate
y = (v*sin(angleRad)); // y coordinate
{
//output
cout << "Your total distance: " << R << "meters" << endl << endl;
cout << "Your travel time is: " << T << "s" << endl << endl;
cout << "Max X coordinate: " << x << "meters" << endl << endl;
cout << "Max Y coordinate: " << y << "meters" << endl << endl;
}
double a = R/20;
double b = x/20;
double c = y/20;
for (double i = R/20; i <= R; i = i + a)// gives me constant numbers..
{
for (double o = x/20; o < x; o = o + b)// gives me 3 of 10 constant numbers
for (double p = y/20; p < y; p = p + c)// give ms 3 of 10 different numbers(what i want)
cout << i << " " << o << " " << p << endl;
cout << "Thank you for using the program!" << endl;
return 0;
}
}
}
If you look at for loops part, I do not get errors, how ever I do not get values that i wanted.
For example i suppose to get:
for (double i = R/20; i <= R; i = i + a)// gives me constant numbers..
{
for (double o = x/20; o < x; o = o + b)// gives me 3 of 10 constant numbers
for (double p = y/20; p < y; p = p + c)// give ms 3 of 10 different numbers(what i want)
cout << i << " " << o << " " << p << endl;
cout << "Thank you for using the program!" << endl;
return 0;
}
Do you really intend to end the program during your first iteration of the outer for loop?
Because the outer loop will never execute more than once, i will never be anything but one value. So you tell me.. are you not supposed to? What's the point of having a loop that.. cannot loop?
Hmmm... So do i gotta use string in here to store the datas for three lines? I thought I can use three different variable in for loops/nested loops, guess not.