1 c++ syntax procedure vector call
所以我基本上整天都在编写这个程序,经历了许多迭代和问题,最后在完成之后我回去运行它,发现我开始工作的最简单的部分现在不再起作用了.
#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";
}
Run Code Online (Sandbox Code Playgroud)
当我第一次编写程序时,这应该正常工作(即99返回A,77返回B,66返回C,依此类推)
现在我已经完成了其余的代码(由于空间原因而省略),无论实际输入是什么,此部分总是返回U(输入为50或更低).我现在已经在这一部分工作了两个半小时,而且让我难过.
你确定要初始化i = 1吗?而不是使用索引,为什么你不只是使用thisVector.back()?或者更好,只是传递input给Determine_Output().您可以i完全消除变量,至少在您向我们展示的代码中.
此外,您不需要声明大小thisVector,因为push_back()将根据需要增长向量.