我的代码有什么问题?没有得到 C++ 程序的正确输出

What is the issue in my code? Not getting correct output of C++ program

我的 elab 问题没有得到正确的输出。 这是问题:

There are faculties in the department, you have to arrange the list of faculties depending upon ID numbers.

TEST CASE 1

INPUT

5
Ram 101
Rahul 95
Ashwin 75
Ahamed 106
Saurav 110

OUTPUT

After Sorting
Name ID
Ashwin 75
Rahul 95
Ram 101
Ahamed 106
Saurav 110

这是我使用结构实现的代码:

#include<bits/stdc++.h>
#include<iostream>

using namespace std;

struct faculty{
  string name;
  int id;
};

int main(){
  int n;
  struct faculty arr[n];
  for(int i=0;i<n;i++){
    cin>>arr[i].name;
    cin>>arr[i].id;
  }
  cout<<"After Sorting"<<endl;
  cout<<"Name ID"<<endl;
  
  //insertion sort
  for(int i=1;i<n;i++){
    struct faculty key=arr[i];
    int j=i-1;
    while(j>=0 && arr[j].id>key.id)
    {
      arr[j+1]=arr[j];
      j--;
    }
    arr[j+1]=key;
  }
  
  //printing
  for(int i=0;i<n;i++){
    cout<<arr[i].name<<" "<<arr[i].id;
  }
  return 0;
}

我的输出:

After Sorting
Name ID
5 0

有人可以帮助我吗?我不知道是什么错误。

首先,对您的代码进行一些分析:

  • 永远不要使用 #include<bits/stdc++.h>。这是不兼容的 C++ 代码。它不适用于其他编译器
  • 不要使用 using namespace std;。总是喜欢显式使用范围
  • 始终初始化所有变量。应该不会有任何例外
  • 不要在 C++ 代码中使用 C 风格的数组。绝不。没有理由
  • VLA(可变长度数组)在 C++ 中是非标准的。 (arr[n])
  • 为您的编译器启用所有警告,例如 -std=c++14 -Wall -Wextra -Wpedantic
  • 使用标准库中的现有容器,例如 std::vector

具体错误:

  • 你忘了看 n
  • 您必须用 std::vector
  • 替换 VLA
  • 输出一条记录后需要加行

因此,您的固定软件将如下所示:

#include<iostream>
#include <string>
#include <vector>

struct faculty {
    std::string name;
    int id;
};

int main() {
    int n;
    std::cin >> n;
    std::vector<faculty> arr(n);
    for (int i = 0; i < n; i++) {
        std::cin >> arr[i].name;
        std::cin >> arr[i].id;
    }
    std::cout << "After Sorting" << std::endl;
    std::cout << "Name ID" << std::endl;

    //insertion sort
    for (int i = 1; i < n; i++) {
        faculty key = arr[i];
        int j = i - 1;
        while (j >= 0 && arr[j].id > key.id)
        {
            arr[j + 1] = arr[j];
            j--;
        }
        arr[j + 1] = key;
    }

    //printing
    for (int i = 0; i < n; i++) {
        std::cout << arr[i].name << " " << arr[i].id << '\n';
    }
    return 0;
}

但这不是首选解决方案。

您应该以不同的方式解决您的问题,以获得更好的解决方案。

首先,我们需要初始化WHAT要做什么。

  1. 我们需要处理由“姓名”和“ID”组成的数据
  2. 从流中读取此数据,例如 std::cin 必须是可能的
  3. 需要输出数据
  4. 从用户那里读取name和id的成员数
  5. 应存储所有数据以供进一步评估
  6. 应读取如上所述的条目数
  7. 数据必须按 ID 排序
  8. 结果应打印在std::cout

接下来,我们需要考虑如何满足要求。我们还没有编写任何代码。好的,让我们看看:

  1. 为了存储该数据,我们将使用一个结构,名称为 std::string,ID 为 unsigned int,因为我们假设 ID 永远不会为负数。
  2. 在面向对象的方法中,我们将数据和对该数据进行操作的方法放在一起。所以读写都会被定义为一个struct中的一个方法。为了节省工作,我们只需覆盖提取器运算符 >>。所以,我们现在可以从任何流中读取数据了。
  3. 出于输出目的,我们还覆盖了插入器运算符<<
  4. 我们需要读取成员数量。我们读取 n,然后使用 if - 语句并检查它是否有效。
  5. 元素个数由用户给定,可以任意。所以,我们需要一个可以变大的动态容器。我们将为此使用 std::vector
  6. 现在,我们必须读取指定数量的条目。这将调用提取器运算符
  7. 排序数据
  8. 通过从矢量输出所有数据,在屏幕上显示排序后的数据。这将调用插入器操作员。

好的,我们阐明了内容和方式。现在(不是之前),我们可以开始编码了。

不幸的是,有数百万种可能的解决方案。我会展示一个更高级的版本,但是你可以根据你的需要实现任何东西。

请看下面的例子:

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>

struct Faculty {
    // 1. Our data. Initiliaze with default
    std::string name{};
    unsigned int id{};
    // 2. Define extractor operator for this data
    friend std::istream& operator >> (std::istream& is, Faculty& f) {
        return is >> f.name >> f.id;
    }
    // 3. Define inserter operator for this data
    friend std::ostream& operator << (std::ostream& os, const Faculty& f) {
        return os << f.name << '\t' << f.id;
    }
};

int main() {
    // 4. Get the number of members that we should read, and check, if input worked
    size_t numberOfMembers{};
    if (std::cin >> numberOfMembers) {

        // 5. Here we will store all data
        std::vector<Faculty> data{};

        // 6. Copy all data from std::cin into our data vector
        std::copy_n(std::istream_iterator<Faculty>(std::cin), numberOfMembers, std::back_inserter(data));

        // 7. Sort according to ID
        std::sort(data.begin(), data.end(), [](const Faculty& f1, const Faculty& f2) { return f1.id < f2.id; });

        // 8. Output
        std::copy(data.begin(), data.end(), std::ostream_iterator<Faculty>(std::cout, "\n"));
    }
    return 0;
}

要在启用 C++14 的情况下进行编译。