程序保持无限,输出非常大的数字 [英] Program keep going infinitely, outputting very large numbers

查看:73
本文介绍了程序保持无限,输出非常大的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代码,它应该输出2个不同的数字,然后在最后一个else语句中输出3列50行的3个不同的数字。但是,输出只是发送一个非常长的数字,输出每列的标题,但是连续不输出数字或零。



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 thetaDegrees, sph;
double sec=0.0, t;
double x, y, z=0.0;

 
double range(double sph, double g_m, double thetaDegrees) 
  {
  return (pow(sph,2.0))/g_m*sin(2*thetaDegrees);
  } 
double timer(double range, double thetaDegrees, double sph) 
 {
 return range/sph*cos(thetaDegrees);	
 }
 
double distance(double sph, double sec, double thetaDegrees) 
 {
 return sph*sec*cos(thetaDegrees);
 }
 
double height(double sph, double thetaDegrees, double sec, double z) 
 {
   return ((sph*sec*sin(thetaDegrees))-(1.0/2.0*pow(sec,2.0))+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";
           cin >> thetaDegrees >> sph;

          if ( sph==0 )
          {
             cin.clear();
             cin.ignore(512, '\n');
         	 cout<< "Bad data \n";
      	  }	   
          
		  else 
      	  {
		      if(thetaDegrees<0.0) 
		      {
                 cerr<<"degrees< error! \n";
              }
                 else 
				 {
                     if(thetaDegrees>90)
				     {
                        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"; 
				 
                       while(sec<=timer(myRange, thetaDegrees, sph))
					   {
				          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";
           cin >> thetaDegrees >> sph;

          if ( sph==0 )
          {
             cin.clear();
             cin.ignore(512, '\n');
         	 cout<< "bad data \n";
      	  }	   
		  else 
      	  {
		      if(thetaDegrees<0.0) 
		      {
                 cerr<<"degrees< error! \n"; 
              }
                 else 
				 {
                     if(thetaDegrees>90)
				     {
                        cerr<<"degrees< error! \n"; 
					 }
		             else 
					 {
					    double myRange=range(sph, g_m, thetaDegrees);
                        cout<<myRange<<"feet(Range)\n"; 
                        cout<<timer(myRange, thetaDegrees, sph)<<"secs in air\n"; 
				 
                       while(sec<=timer(myRange, thetaDegrees, sph))
					   {
				          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.

推荐答案

Quote:

我尝试删除并添加set precision,并修复。我还试图检查是否有任何会导致无限循环的东西,但我看不到它。

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.



可能是时候学会使用调试器并观察你的代码执行。



你的代码没有你想象的那样,或者你不明白为什么!



有一个几乎通用的解决方案:逐步在调试器上运行代码,检查变量。

调试器在这里向您展示您的代码正在做什么,您的任务是与什么进行比较它应该这样做。

调试器中没有魔法,它不知道你的代码应该做什么,它没有找到bug,它只是通过向你展示什么是帮助你继续当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。

调试器 - 维基百科,免费的百科全书 [ ^ ]

1.11 - 调试程序(步进和断点)|学习C ++ [ ^ ]

调试器仅显示您的代码正在执行的操作,并且您的任务是与应该执行的操作进行比较。


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.


无限循环是由变量'sec'在while循环内重新声明并在测试条件中使用引起的。由于范围可变,修改循环中的变量不会影响全局变量;在循环开始之前删除重新声明并将值初始化为零。



此外,还有一些其他项目:

标题行可能不需要在每次传递时重印;将它移到循环之上。

每次传递都不需要执行'fixed'和'setprecision(3)'行;将它们移到循环上方。

包含'endl'的行不需要输出'\ n',除非你想在每个输出行之间加上额外的空行。

除非我遗漏它,否则似乎没有代码为'x'和'y'设置值;这些值将始终显示初始值(零)。





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
	}
}


这篇关于程序保持无限,输出非常大的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆