Error in last output
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Error in last output

  1. #1
    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<<"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)<<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. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,091

    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. #3
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,882

    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. #4
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,367

    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.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  5. #5
    Join Date
    Apr 1999
    Posts
    27,427

    Re: Error in last output

    Quote Originally Posted by Guy Kanbar View Post
    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. #6
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,526

    Re: Error in last output

    Quote Originally Posted by Guy Kanbar View Post
    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?
    Quote Originally Posted by Guy Kanbar View Post
    while(D>3.500) cout<<"Target is out of Range";
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinViewer - an integrated GDI objects viewer for Visual C++ Debugger, and more...

  7. #7
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,367

    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
    Enter your actual coordinates
    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
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  8. #8
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,526

    Re: Error in last output

    I would like to suggest further improvements.
    Quote Originally Posted by 2kaud View Post
    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)
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinViewer - an integrated GDI objects viewer for Visual C++ Debugger, and more...

  9. #9
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,367

    Re: Error in last output

    Agreed. I only cleaned up the OP code minimally, keeping the original constructs as far as possible.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center