所以我基本上一整天都在写这个程序,经历了许多迭代和问题,最后在完成它之后我回去运行它,发现我一开始工作的最简单的部分现在不再起作用了.
#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 或更低)。 我实际上已经在这一部分工作了两个半小时,这让我很困惑。
最佳答案
你确定要初始化i = 1
吗?为什么不使用索引,而不仅仅是使用 thisVector.back()
?或者更好的是,只需将 input
传递给 Determine_Output()
。您可以完全消除变量 i
,至少在您向我们展示的代码中是这样。
此外,您无需为 thisVector
声明大小,因为 push_back()
会根据需要增大 vector 。
关于c++ - 我的语法在这里正确吗? C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19897489/