c++ - 我的语法在这里正确吗? C++

标签 c++ vector syntax call procedure

所以我基本上一整天都在写这个程序,经历了许多迭代和问题,最后在完成它之后我回去运行它,发现我一开始工作的最简单的部分现在不再起作用了.

#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/

相关文章:

c++ - 如何从随机值范围中删除某个元素?

math - 向量数量不同的两个图节点之间的余弦相似度计算

c++ - 我可以在 C++ 的映射结构中使用 vector 作为索引吗?

c++ - 非逻辑代码流/无效 vector 操作

python - for 循环中的复合条件

css - 多个css伪类

c++ - 具有虚拟析构函数的类的未使用 const 对象会导致段错误

c++ - 编写代码使年利率翻倍

c++ - 这个typedef是什么意思?

c# - 为什么 C# 需要条件句周围的括号?