15,878,430 members
See more:
I have a code which is supposed to output 2 different numbers, and after that output 3 different numbers in 3 columns for 50 rows, in the last else statement. However, the output is just sending one really long number, outputs the title for each column, but either no number or a zero below it, continuously.

```#include <iomanip>
#include <math.h>
#include <iostream>
using namespace std;

const double g_m=32.0;
double sec=0.0, t;
double x, y, z=0.0;

double range(double sph, double g_m, double thetaDegrees)
{
}
double timer(double range, double thetaDegrees, double sph)
{
}

double distance(double sph, double sec, double thetaDegrees)
{
}

double height(double sph, double thetaDegrees, double sec, double z)
{
}

int main()
{
int choice;
bool shapes = true;
while (shapes != false){

cout << " Choose one \n ";
cout << " 1 KPH.\n";
cout << " 2 MPH.\n";
cin >> choice;

switch (choice)
{
case 1:
{
cout << "You chose KPH \n";
cout << "Enter angle(degrees) and velocity(kph) \n";

if ( sph==0 )
{
cin.clear();
cin.ignore(512, '\n');
}

else
{
{
cerr<<"degrees< error! \n";
}
else
{
{
cerr<<"degrees< error! \n";
}
else
{
double myRange = range(sph, g_m, thetaDegrees);
cout<<fixed;
cout << setprecision(3);
cout<< myRange<<"feet(Range)\n"; //R value is sent to screen
cout<<fixed;
cout << setprecision(3);
cout<< timer(myRange, thetaDegrees, sph)<<"secs in air\n";

{
double sec = sec+ (timer(myRange, thetaDegrees, sph)/50.0);
cout<<fixed;
cout<<setprecision(3);
cout<<left<<setw(25)<<"Total Flight Time(secs)";
cout<<left<<setw(25)<<"Horizontal Distance(ft)";
cout<<left<<setw(25)<<"Height(ft)";
cout<< "\n"<<endl;
cout<<left<<setw(25)<<sec;
cout<<left<<setw(25)<<x;
cout<<left<<setw(25)<<y;
cout<< "\n"<<endl;

}
}
}
}
}
break;
case 2:
{
cout << "You chose MPH \n";
cout << "Enter angle(degrees) and velocity(mph) \n";

if ( sph==0 )
{
cin.clear();
cin.ignore(512, '\n');
}
else
{
{
cerr<<"degrees< error! \n";
}
else
{
{
cerr<<"degrees< error! \n";
}
else
{
cout<<myRange<<"feet(Range)\n";

{
double sec = sec+ (timer(myRange, thetaDegrees, sph)/50.0);
cout<<fixed;
cout<<setprecision(3);
cout<<left<<setw(25)<<"Total Flight Time(secs)";
cout<<left<<setw(25)<<"Horizontal Distance(ft)";
cout<<left<<setw(25)<<"Height(ft)";
cout<< "\n"<<endl;
cout<<left<<setw(25)<<sec;
cout<<left<<setw(25)<<x;
cout<<left<<setw(25)<<y;
cout<< "\n"<<endl;

}
}
}
}
}
break;
case 3:
{
cout << "End \n";
shapes = false;
}
break;

}
}
return 0;

}```

What I have tried:

I tried removing and adding set precision, and fixed. I also tried to check if there is anything that would cause an infinite loop, but i couldn't see it.
Posted
Updated 5-Nov-18 16:58pm

## Solution 1

Quote:
I tried removing and adding set precision, and fixed. I also tried to check if there is anything that would cause an infinite loop, but i couldn't see it.

May be it time to learn to use the debugger and watch how your code perform.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]
1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.

## Solution 2

The infinite loop is caused by the variable 'sec' being re-declared inside the while loop and being used in the test condition. Modifying the variable within the loop does not affect the global variable due to variable scope; remove the re-declaration and initialize the value to zero before the loop start.

Also, here are a few other items:
The header line likely does not need to be reprinted on each pass; move it above the loop.
The 'fixed' and 'setprecision(3)' lines do not need to be executed in each pass; move them above the loop.
The lines that contain 'endl' do not need to output '\n' unless you want the extra blank lines in between each output line.
Unless I am missing it, there appears to be no code that sets a value for 'x' and 'y'; those values will always display the initialized value (zero).

```}
double myRange = range(sph, g_m, thetaDegrees);
cout << fixed; //Moved outside of the loop
cout << setprecision(3); //Moved outside of the loop
cout << myRange << " feet(Range)\n";
cout << timer(myRange, thetaDegrees, sph) << " secs in air\n";
cout << left << setw(25) << "Total Flight Time(secs)"; //Moved outside of the loop
cout << left << setw(25) << "Horizontal Distance(ft)"; //Moved outside of the loop
cout << left << setw(25) << "Height(ft)" << endl; //Moved outside of the loop with endl
while (sec <= timer(myRange, thetaDegrees, sph))
{
sec = sec + (timer(myRange, thetaDegrees, sph) / 50.0);
//Double-blank line removed
cout << left << setw(25) << sec;
cout << left << setw(25) << x;
cout << left << setw(25) << y << endl;
//Double-blank line removed
}
}```

v2