1

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;

}

future95
  • 11
  • 1
  • Don't just say "errors", please show us *exactly* what errors you got. – tadman Nov 28 '20 at 09:00
  • Tip: Use `std::vector` to store your data, avoid using fixed-length C arrays for dynamic input. C++ does not support variable length arrays. Some compilers do. Maybe Visual C++ is one, but clang isn't. – tadman Nov 28 '20 at 09:01
  • **WARNING**: Using [`rand()` can be highly problematic](https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful) and you’re strongly encouraged to use an appropriate [random number generator facility in the Standard Library](http://en.cppreference.com/w/cpp/numeric/random) that produces high-quality random values. Using `time(NULL)` as a random number will produce identical results if run in the same second, and on many platforms `rand()` is *barely* random at all. – tadman Nov 28 '20 at 09:02
  • [Same issue as this question](https://stackoverflow.com/questions/65045859/expression-must-have-a-constant-value-vs-2019). -- *This is just a little exercise I've been doing to practice C++* -- You should be using good C++ to learn from, as no good C++ book shows declaring arrays using a runtime variable to denote the number of entries. – PaulMcKenzie Nov 28 '20 at 09:09

1 Answers1

0
struct contestantStats people[contestSize];

is not legal C++ and Visual Studio is correct to reject it (and Xcode was wrong to accept it).

In C++ array bounds must be compile time constants. contestSize is a variable.

The C++ solution to this problem is to use a vector instead of an array

#include <vector>

std::vector<contestantStats> people(contestSize);

I haven't properly checked but based on a quick look I don't think the rest of your code needs to change after you've switched to using a vector. (That's not to say that the rest of your code doesn't have problems.)

john
  • 85,011
  • 4
  • 57
  • 81