好吧,所以我應該計算,如果我以一定的速度和持續時間以光速行進,需要多少時間。是的,這是作業。我大部分時間都在計算,但是當我輸入40和60的時候,當我得到50時,我得到了0.008305。怎麼回事?沒有從等式中得到正確的答案。此外,無限循環
此外,do while循環無法正常工作。無論我如何回答並忽略cout和cin語句,都會重複執行整段代碼,導致它在評估v1時給我提供錯誤。我該如何解決?
#include <iostream>
#include <iomanip>
#include <conio.h>
#include <cmath>
const int c = 299792458;
int main()
{
int loopCheck = 'y';
do
{
int tm = 0;
std::cout << "Enter time of travel: " << std::endl;
std::cin >> tm;
if (tm < 0)
{
do
{
std::cout << "You have entered an incorrect value. "
<< "Please enter a value greater than 0."
<< std::endl;
std::cin >> tm;
} while (tm < 0);
}
double v1 = 0;
std::cout << "Enter velocity: " << std::endl;
std::cin >> v1;
if (v1 <= 0 || v1 >= 100)
{
do
{
std::cout << "You have entered an incorrect value. "
<< "Please enter a value between 0 and 100."
<< std::endl;
std::cin >> v1;
} while (v1 <= 0 || v1 >= 100);
}
double v2 = (v1/100) * c;
double ts = tm/(sqrt((1 - (v2 * v2))/(c * c)));
std::cout << v2 << " " << c << std::endl;
std::cout << std::fixed << std::setprecision(6) << ts
<< std::endl << std::endl;
std::cout << "Would you like to enter more data? (y/n)" << std::endl;
std::cin >> loopCheck;
} while (loopCheck == 'y');
_getch();
return 0;
}
gcc給出以下警告:a.cpp:48:52:警告:表達式[-Woverflow]中的整數溢出。它是'double ts = tm ...(c * c)));' –