【发布时间】:2013-11-11 01:27:08
【问题描述】:
所以我基本上整天都在写这个程序,经历了许多迭代和问题,最后完成它后我回去运行它,发现我一开始工作的最简单的部分现在不再起作用了.
#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;
void Determine_Output (double);
int main()
{
vector<double> thisVector(10);
double input=-2;
int i=1;
double average = 0.00;
double highest;
double lowest;
cout<<setprecision(3);
for (unsigned z=0; z<10; z++)
{
cout<<"Please enter result \"" <<i<< "\": ";
cin>> input;
if ((input <= 100)&&(input >= 0))
{
thisVector.push_back(input);
Determine_Output(thisVector[i]); //Offending procedure call
i++;
}
else if (input == -1)
break;
else
{
cout<<"Invalid input, must be between 0 and 100\n";
z--;
}
}
void Determine_Output (double output) { //Offending procedure
if (output > 90)
cout<<"A will be assigned to this result\n";
else if (output > 70)
cout<<"B will be assigned to this result\n";
else if (output > 60)
cout<<"C will be assigned to this result\n";
else if (output > 50)
cout<<"P will be assigned to this result\n";
else
cout<<"U will be assigned to this result\n";
}
当我第一次编写程序时,它可以正常工作(即 99 返回 A,77 返回 B,66 返回 C 等等)
现在我已经完成了其余的代码(由于篇幅原因省略),这部分总是返回 U(输入为 50 或更低),无论实际输入是什么。 我已经在这部分工作了两个半小时了,这让我很难过。
【问题讨论】:
-
如果你的语法不正确,编译器会报错。
-
由于您使用
double作为变量,您应该使用double常量,例如90.0、70.0、60.0、50.0和100.0。目前,编译器正在为您转换它们。 -
您标记了“违规程序”,但我认为您找错地方了。通过使用一些已知值运行
Determine_Output来检查它,看看你会得到什么。 -
另外,在 C++ 中,索引从 0 开始,而不是 1。
-
你使用双精度而不是整数有什么原因吗?如果您不需要浮点语义,我建议您避免使用它们,因为浮点值意味着您必须担心舍入错误导致的问题。
标签: c++ vector syntax call procedure