我在一台相当不错的Windows 10 PC上使用MS Visual c++ 6.0(根据我的工作需要)。
我在显示通过"cin>>"手动输入的小数后的值时遇到了麻烦,我已经尝试了通过DSKVP的问题回答的修复(在c++中显示小数点后的两位数字),但它仍然不会显示小数值,它只是在小数后下降数字和/或舍入它们。
#include <iostream>
#include <iomanip> //for setprecision and fixed
using namespace std;
int readResults()
{
cout << setprecision(2);
cout << "Scanned Value: ";
double vrrm(0.0);
cin >> vrrm;
return vrrm;
}
int main()
{
double vrrm ( readResults() );
double vrwm ( readResults() );
double vr ( readResults() );
double vrms ( readResults() );
double io ( readResults() );
double vfm ( readResults() );
cout << "nParameters tScanned Values t Limits ttUnit" << endl;
cout << "Vrmm t t" << fixed << setprecision (2) << vrrm << " ttt 45.00 - 50.00 ttV" << endl;
cout << "Vrwm t t" << fixed << setprecision (2) << vrwm << " ttt 45.00 - 50.00 ttV" << endl;
cout << "Vr t t" << fixed << setprecision (2) << vr << " ttt 45.00 - 50.00 ttV" << endl;
cout << "Vrms t t" << fixed << setprecision (2) << vrms << " ttt 33.00 - 37.00 ttV" << endl;
cout << "Io t t" << fixed << setprecision (2) << io << " ttt 00.90 - 1.50 ttA" << endl;
cout << "Vfm t t" << fixed << setprecision (2) << vfm << " ttt 00.95 - 1.05 ttV" << endl;
cout << endl;
if ( vrrm >= 45.00 && vrrm <= 50.00 &&
vrwm >= 45.00 && vrwm <= 50.00 &&
vr >= 45.00 && vr <= 50.00 &&
vrms >= 33.00 && vrms <= 37.00 &&
io >= 0.90 && io <= 1.50 &&
vfm >= 0.95 && vfm <= 1.05 )
cout << "PASS" << endl << endl;
else
cout << "FAIL" << endl << endl;
return 0;
}
当我输入十进制值时,结果如下:
Scanned Value: 49.98
Scanned Value: 49.99
Scanned Value: 49.85
Scanned Value: 35.01
Scanned Value: 0.99
Scanned Value: 1.01
Parameters Scanned Values Limits Unit
Vrmm 49.00 45.00 - 50.00 V
Vrwm 49.00 45.00 - 50.00 V
Vr 49.00 45.00 - 50.00 V
Vrms 35.00 33.00 - 37.00 V
Io 0.00 00.90 - 1.50 A
Vfm 1.00 00.95 - 1.05 V
FAIL
Press any key to continue
我尝试将Vrrm初始化为0.0(如此处所示),甚至尝试将其乘以1.00,如Can't get cout显示小数,但我相信它不适用于我在这里写的内容-仍然不会显示小数点后的值。
我非常希望有完整的值显示和程序正确输出PASS(因为它应该是)。你们谁能好心地给我解释一下我哪里错了/给我推荐一些好的参考/原始材料?
我这周刚开始学习c++, Alex的教程在教育我方面做得非常棒,他的作品是我现在的参考:https://www.learncpp.com/.
我测试了你的代码,我唯一改变的是readResults()
的返回类型从int
到double
。一旦我这样做了,我就得到了我期望的结果!