I'm trying to create C++ code to take user input separated by a comma and feed it into an int array to then sort it all with even columns. For some reason when I feed it into the array the program shuts on me. I can't even have it print the array, and I'm not quite sure why.
sample:
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
code:
#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;
}
Thank you so much in advance :)