I'm running this on Xcode and it complies just fine. But when I run it on Visual Studios 2019 I get errors. Can anyone tell me how I can change my code for it to work with both? also, my bubble sort isn't working correctly and I don't know why. Any help would be appreciated. This is just a little exercise I've been doing to practice C++
#include <iostream>
#include <time.h>
#include <cstdlib>
#include <ctime>
#include <limits>
#include <string>
using namespace std;
// using this allows us to avoid using std:: before each line of code
int contestSize;
int randomStats(){
return(rand() % 100 + 1);
};
struct contestantStats{
string name;
int attack;
int defense;
int speed;
int average;
};
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
int main(int argc, const char * argv[]) {
cout << " Enter number of contestants: ";
while(!(cin>>contestSize)){
cout<< "Invalid Input, Please Enter an integer: ";
cin.clear();
cin.ignore(100, '\n');
}
while ( contestSize < 2){
cout<< "Invalid Input, Please Enter at least 2 contestants: ";
cin.clear();
cin.ignore(100, '\n');
cin>>contestSize;
}
int arrayconSize= contestSize;
cout<<"There are "<< arrayconSize <<" Contestants!"<< endl;
struct contestantStats people[contestSize];
srand(static_cast<unsigned int>(time(0)));
for(int i = 0; i<contestSize; i++){
cout<< "Enter Contestant Number " << i+1 <<"'s name:";
getline(cin>>ws,people[i].name);
people[i].attack = randomStats();
people[i].defense = randomStats();
people[i].speed = randomStats();
people[i].average = ((people[i].attack + people[i].defense + people[i].speed)/3);
cout<<"Name: "<<people[i].name<< endl;
cout<<"Attack: "<< people[i].attack << endl;
cout<<"Defense: "<< people[i].defense<< endl;
cout<<"Speed: "<<people[i].speed << endl;
cout<<"Average Power: " <<people[i].average<< endl;
cout<<" "<< endl;
};
// scans the array for the largest number
int max = people[0].average;
for (int i = 1; i < contestSize; i++){
if (people[i].average > max)
max = people[i].average;
}
//displays all average power
for (int i = 0; i < contestSize; i++){
cout <<people[i].name<<" "<<people[i].average<<" Average Power | "<< endl;
}
// finds the name connected to the highest integer. Max is constant.
for(int i = 0; i < contestSize; i++){
if(people[i].average == max){
if (i < contestSize){
cout<<"The Winner is "<< people[i].name<<" with "<< max << " Power!"<< endl;
string winner = people[i].name;
}
break;
}
}//bubble sort to sort from smallest to largest
for(int i = 0 ; i < contestSize; i++){
for(int j = 0; j<contestSize-i-1; j++){
if(people[j].average < people[j+1].average){
swap(&people[j].average, &people[j+1].average);
}
}
cout<<people[i].average<<endl;
}
return 0;
}