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

Bha*_*rdy 0 c++ c++14

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

系里有院系,你必须根据身份证号来安排院系的名单。

测试案例 1

输入

5
Ram 101
Rahul 95
Ashwin 75
Ahamed 106
Saurav 110
Run Code Online (Sandbox Code Playgroud)

输出

After Sorting
Name ID
Ashwin 75
Rahul 95
Ram 101
Ahamed 106
Saurav 110
Run Code Online (Sandbox Code Playgroud)

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

#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;
}
Run Code Online (Sandbox Code Playgroud)

我的输出:

#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;
}
Run Code Online (Sandbox Code Playgroud)

有人能帮我吗?我无法弄清楚错误是什么。

Arm*_*gny 5

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

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

具体错误:

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

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

#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;
}
Run Code Online (Sandbox Code Playgroud)

但这不是首选的解决方案。

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

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

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

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

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

好的,我们澄清了 WHAT 和 HOW。现在(不是之前),我们可以开始编码了。

不幸的是,有数百万种可能的解决方案。我将展示一个更高级的版本,但您可以根据自己的需要实现任何内容。

请看下面的例子:

#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;
}
Run Code Online (Sandbox Code Playgroud)

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