【问题标题】:Is my syntax correct here? C++我的语法在这里正确吗? C++
【发布时间】: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


【解决方案1】:

您确定要初始化i = 1 吗?而不是使用索引,你为什么不直接使用thisVector.back()?或者更好的是,只需将input 传递给Determine_Output()。您可以完全消除变量i,至少在您向我们展示的代码中是这样。

此外,您无需为thisVector 声明大小,因为push_back() 会根据需要增大向量。

【讨论】:

    【解决方案2】:
    vector<double> thisVector(10);
    

    创建一个包含 10 个双精度的向量,全部初始化为 0。所以 thisVector[0]thisVector[1]、...、thisVector[9] 都是 0.0。

    你的测试是:

    thisVector.push_back(input);
    Determine_Output(thisVector[i]);
    

    其中i1 开头,并且每次递增。第一次通过循环,thisVector.push_back(input);input 添加到向量的末尾,使其成为第十一个元素,thisVector[10]。但是DetermineOutput 是用元素thisVector[1] 调用的,它的值仍然是0。

    下一次循环时,添加thisVector[11]i 现在是2,所以你检查thisVector[2],当然也是0.0。等等。只有在最后一次迭代中,您才会检查thisVector[10],它不为零(因为它是您读取的 first 元素)。

    所以,简化:

    1. 不要将向量初始化为 10 个元素。当你开始时,你希望它是空的。

    2. 摆脱i。这是完全没有必要的。使用thisVector.back() 来引用您刚刚push_backed 的元素。 (或者直接使用input。)

    3. 摆脱z。这也是不必要的。您需要运行循环,直到 thisVector 有 10 个元素。

    【讨论】:

      【解决方案3】:

      您可以通过颠倒比较的顺序来消除else if 结构:

      void Determine_Output(double output)
      {
        char letter = 'U';
        if (output > 50)
          letter = 'P';
        if (output > 60)
          letter = 'C';
        if (output > 70)
          letter = 'B';
        if (output > 90)
          letter = 'A';
        cout << letter << " will be assigned to this result.\n";
      }
      

      上面的结构可能不那么令人困惑,尽管它确实多次覆盖了字母。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多