CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

1. Junior Member
Join Date
Dec 2013
Posts
1

Error in last output

#include"stdafx.h"
#include <iomanip>
#include <iostream>
#include <math.h>

using std::cout;
using std::cin;
using std::endl;
using std::fixed;
using std::setprecision;
using std::setiosflags;

int main()
{
const double Pi = 3.1415926;
double d;
double degreeAngle;
double D;
double degreeAngle2;
double Xm;
double Ym;
double Xt;
double Yt;
double g;
double degreeAngle3;

cout<<"Enter Angle in degree"<<endl;
cin>>degreeAngle;

while(degreeAngle<=0 || degreeAngle>=90) cout<<"Enter a value between 0 and 90"<<endl;
if (degreeAngle>0 && degreeAngle<90) cout<<"Enter value of d in Km"<<endl;
cin>>d;

while(d<=0) cout<<"Enter positive vale for d in Meters";
if(d>=0) {
D= d/(tan (((90-degreeAngle)*3.141592)/180));
cout<<"The distance between you and your target in Km is "<<D<<endl;}

while(D>3.500) cout<<"Target is out of Range";
if(D<=3.500)

{cout<<"Target is in Range"<<endl<<"Enter value of Alfa 2 in degree"<<endl;

cin>>degreeAngle2;}

{if(degreeAngle2>90) cout<<"Enter a value of Alfa lesser than 90";
else if(degreeAngle2<=0) cout<<"Enter a positive value of Alfa 2";
else

{cout<<"Xm=";
cin>>Xm;
cout<<"Ym=";
cin>>Ym;
Xt=Xm+(D*cos(degreeAngle2));
Yt=Ym+(D*sin(degreeAngle2));
cout<<"Your Target's current position is x="<<Xt<<"y="<<Yt<<endl;
cout<<"Enter the value of g (the gravitation)"<<endl;

cin>>g;

degreeAngle3=(asin((g*D*1000)/(820*820)))/2;
cout<<"L'angle de tir est "<<setprecision(2)<<std::fixed<<degreeAngle3<<endl

;}

return 0;}

So i've run the program and its juat fine, tho when I reach the last output. It puts on screen something like -1#N so not the answer I was looking for. Anything you guys can think of?

2. Elite Member Power Poster
Join Date
Nov 2003
Location
Florida
Posts
12,518

Re: Error in last output

Formatting dude, formatting. That code is unreadable.

Use Code Tags, and put each statement on a separate line. Use appropriate indentation and white space.

3. Elite Member Power Poster
Join Date
Apr 2000
Location
Belgium (Europe)
Posts
4,626

Re: Error in last output

you're getting a "NAN" (not a number) value as result of your calculation.

That means you're making a calculation error somewhere.

4. Re: Error in last output

Anything you guys can think of?
What debugging of the code using the debugger have you done to see where the the variable values deviate from those expected? Using the debugger to trace run-time problems in a program is something that every programmer needs to be familiar with.

5. Elite Member Power Poster
Join Date
Apr 1999
Posts
27,449

Re: Error in last output

Originally Posted by Guy Kanbar
So i've run the program and its juat fine, tho when I reach the last output. It puts on screen something like -1#N so not the answer I was looking for. Anything you guys can think of?
What have you done to debug your program? Programming is much more than writing and running code. You need to debug your program using the debugger to see where it goes wrong.

Regards,

Paul McKenzie

6. Elite Member Power Poster
Join Date
Aug 2000
Location
New York, NY, USA
Posts
5,656

Re: Error in last output

Originally Posted by Guy Kanbar
if(D<=3.500)

{cout<<"Target is in Range"<<endl<<"Enter value of Alfa 2 in degree"<<endl;

cin>>degreeAngle2;}
I have never in my 30+ years in software development seen this original style of curly braces placement!

Also, what would that code do if D is greater than 3.500?
Originally Posted by Guy Kanbar
while(D>3.500) cout<<"Target is out of Range";

7. Re: Error in last output

I've tidied and cleaned up the code so that it's readable and deals with incorrect entries. I don't know what values are required for input, but for the ones I've used I get I value for the last output.

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

int main()
{
const double Pi = 3.1415926;

double d;
double degreeAngle;
double D;
double degreeAngle2;
double Xm;
double Ym;
double Xt;
double Yt;
double g;
double degreeAngle3;

cout << "Enter Angle in degree" << endl;
cin >> degreeAngle;

while (degreeAngle <= 0 || degreeAngle >= 90)
{
cout << "Enter a value between 0 and 90" << endl;
cin >> degreeAngle;
}

cout << "Enter value of d in Km" << endl;
cin >> d;

while (d <= 0)
{
cout << "Enter positive vale for d in Meters";
cin >> d;
}

D = d / (tan(((90 - degreeAngle) * Pi) / 180));
cout << "The distance between you and your target in Km is " << D << endl;

if (D > 3.500)
cout << "Target is out of Range";
else
{
cout << "Target is in Range" << endl << "Enter value of Alfa 2 in degree" << endl;
cin >> degreeAngle2;
while (degreeAngle2 <= 0 || degreeAngle2 > 90)
{
cout << "Enter a value of Alfa 2 greater than 0 lesser than 90";
cin >> degreeAngle2;
}

cout << "Enter your actual coordinates" << endl;
cout << "Xm=";
cin >> Xm;
cout << "Ym=";
cin >> Ym;

Xt = Xm + (D * cos(degreeAngle2));
Yt = Ym + (D * sin(degreeAngle2));
cout << "Your Target's current position is x=" << Xt << "y=" << Yt << endl;
cout << "Enter the value of g (the gravitation)" << endl;
cin >> g;

degreeAngle3 = (asin((g * D * 1000) / (820 * 820))) / 2;
cout << "L'angle de tir est " << setprecision(2) << fixed << degreeAngle3 << endl;
}

return 0;
}```
Sample run is
Code:
```Enter Angle in degree
67
Enter value of d in Km
.5
The distance between you and your target in Km is 1.17793
Target is in Range
Enter value of Alfa 2 in degree
13
Xm=23
Ym=78
Your Target's current position is x=24.0689y=78.4949
Enter the value of g (the gravitation)
9.82
L'angle de tir est 0.01```

8. Elite Member Power Poster
Join Date
Aug 2000
Location
New York, NY, USA
Posts
5,656

Re: Error in last output

I would like to suggest further improvements.
Originally Posted by 2kaud
Code:
```	cout << "Enter Angle in degree" << endl;
cin >> degreeAngle;

while (degreeAngle <= 0 || degreeAngle >= 90)
{
cout << "Enter a value between 0 and 90" << endl;
cin >> degreeAngle;
}```
This and a few more similar fragments may be called "entrapment"
If you know the valid range of values, why not to include it in the FIRST prompt?
Then you would notice that two lines of code are repeated twice. This is not good for multiple reasons.
The typical implementation of input with validation may look like that:
Code:
```	do
{
cout << "Enter Angle in degree between 0 and 90" << endl;
cin >> degreeAngle;
} while (degreeAngle <= 0 || degreeAngle >= 90)```

9. Re: Error in last output

Agreed. I only cleaned up the OP code minimally, keeping the original constructs as far as possible.

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•