【发布时间】:2020-06-27 19:21:25
【问题描述】:
我正在尝试创建 C++ 代码以获取用逗号分隔的用户输入并将其输入到 int 数组中,然后使用偶数列对其进行排序。出于某种原因,当我将它输入数组时,程序关闭了我。我什至不能让它打印数组,我不太清楚为什么。
样本:
Welcome to Please Help Me Sort Grades!!! :)
Please enter student grades:10,20,30,10,20,30,50,90,10,50
Unsorted:
Grade ID
10 1
20 2
30 3
10 4
20 5
30 6
50 7
90 8
10 9
50 10
Sorted:
Grade ID
10 1
10 4
10 9
20 2
20 5
30 3
30 6
50 7
50 10
90 8
Process finished with exit code 0
代码:
#include <iostream>
#include <cmath>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
using namespace std;
// Global Variables and Libraries are declared
using namespace std;
int main() {
cout << setprecision(20) << fixed;
int size = 30;
int StudentID[size];
int StudentGrades[size];
string commaList;
vector<int> grades;
int MinGrade;
int MinID;
//char * token is a char pointer. We will discuss them in a later chapter
char *token = NULL;
cout << "Please enter a comma separated list of grades: ";
getline(cin, commaList);
//character array that will hold comma seperated list after its copied over
char gradesCharArr[commaList.size()];
strcpy(gradesCharArr, commaList.c_str());
//gives you the location of the first ',' thats encountered in the array
token = strtok(gradesCharArr, ",");
//Iterate through each "," that is found until there are none left
while(token != NULL){
//convert the grade from a string to an int and push it into the vector
grades.push_back(stoi(token));
//find next occurrence of ","
token = strtok(NULL, ",");
}
for(int i = 0; i < size; i++){
if(i > grades.size()){
StudentGrades[i] =0;
StudentID[i] = 0;
}
else {
StudentGrades[i] = grades.at(i);
StudentID[i] = i + 1;
}
}
cout << "Unsorted: " << endl;
//Print grades back to show they are properly tokenized
for(int i = 0; i < grades.size(); i++){
cout << StudentGrades[i] << setw(10) << StudentID[i] << endl;
}
for(int i = 0; i < size; i++) // for the first initial round where we will put the official
{ // max number when found (i)
for(int j = i + 1; j < size; j++) // for the second subround (j) here the code is comparing the max
{ // value of the array at j and the array at i until we get
if(StudentGrades[j] < StudentGrades[i]) // the ultimate max number. In order to do that though we will
{ // shift the values up one place in the Array and place the
MinGrade = StudentGrades[i]; // max number in it's proper place in MyArray.
MinID = StudentID[i];
StudentGrades[i] = StudentGrades[j];
StudentID[i] = StudentID[j];
StudentGrades[j] = MinGrade;
StudentID[j] = MinID;
}
}
}
cout << "Sorted:" << endl;
for(int i = 0; i < grades.size(); i++){
cout << StudentGrades[i] << setw(20) << StudentID[i] << endl;
}
return 0;
}
提前非常感谢你:)
【问题讨论】:
-
他们的意思是:
int StudentID[size];在标准 c++ 中是不允许的。当您需要动态大小的数组时,请使用std::vector -
RE:
For some reason when I feed it into the array the program shuts on me... 调试器产生了什么结果? -
它是一些编译器的扩展。您需要阅读您的编译器手册以了解它是如何工作的,以及您可以使用它们做什么以及不可以做什么。您既没有指定您正在使用的编译器,也没有使用 C++ 的每个人都知道可变长度数组的所有细节(但他们确实知道
std::vector)。详情见这里:Why aren't variable-length arrays part of the C++ standard? -
嗯...另外,您提供的代码甚至无法在 MVCC 中编译...
-
由于不安全的编程实践,显示的代码至少有一个与缓冲区/数组溢出相关的明显错误。由于混合了 C 和 C++ 风格的字符串处理,并且未能考虑 C-样式终止
\0。这会导致内存损坏和未定义的行为。你有a good C++ textbook 有如何正确处理 C++ 中的字符串的例子吗?