[英]What is the issue in my code? Not getting correct output of C++ program
I'm not getting the correct output of my elab question.我的 elab 问题没有得到正确的 output 。 Here's the question:
这是问题:
There are faculties in the department, you have to arrange the list of faculties depending upon ID numbers.
系里有院系,你要根据身份证号来安排院系名单。
TEST CASE 1
测试用例 1
INPUT
输入
5 Ram 101 Rahul 95 Ashwin 75 Ahamed 106 Saurav 110
OUTPUT
OUTPUT
After Sorting Name ID Ashwin 75 Rahul 95 Ram 101 Ahamed 106 Saurav 110
Here's the code I implemented using structures:这是我使用结构实现的代码:
#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;
}
My output:我的 output:
After Sorting
Name ID
5 0
Can someone help me?有人能帮我吗? I can't figure out what the mistake is.
我无法弄清楚错误是什么。
First, some analysis for your code:首先,对您的代码进行一些分析:
#include<bits/stdc++.h>
.#include<bits/stdc++.h>
。 This is non-compliant C++ code.using namespace std;
using namespace std;
. arr[n]
) arr[n]
)-std=c++14 -Wall -Wextra -Wpedantic
-std=c++14 -Wall -Wextra -Wpedantic
std::vector
std::vector
Concrete errors:具体错误:
std::vector
std::vector
替换 VLASo, your fixed software would look like this:因此,您的固定软件将如下所示:
#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;
}
But this is not the preferred solution.但这不是首选的解决方案。
You should tackle your problem in a different way to come to a better solution.您应该以不同的方式解决您的问题,以获得更好的解决方案。
First, we need to initialize WHAT to do.首先,我们需要初始化要做什么。
std::cin
must be possiblestd::cin
必须是可能的std::cout
std::cout
Next, we need to think on HOW we can fullfill the requirements.接下来,我们需要考虑如何满足需求。 We do not write any code yet.
我们还没有编写任何代码。 Ok, lets's see:
好的,让我们看看:
std::string
for the name and an unsigned int
for the ID, because, we assume that the ID will never be negative.std::string
,ID 为unsigned int
,因为我们假设 ID 永远不会是负数。>>
.>>
。 So, we can read the data now from any stream.<<
.<<
。if
- statement and check, if that worked.if
- 语句并检查是否有效。std::vector
for that.std::vector
。 OK, we clarified the WHAT and the HOW.好的,我们澄清了 WHAT 和 HOW。 Now (not before), we can start coding.
现在(不是之前),我们可以开始编码了。
Unfortunately, there are millions of possible solutions.不幸的是,有数百万种可能的解决方案。 I will show a more advanced version, but you can implement anything according to your needs.
我将展示一个更高级的版本,但您可以根据需要实现任何内容。
Please see the below example:请看下面的例子:
#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;
}
To be compiled with C++14 enabled.编译时启用 C++14。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.