I was just writing little OOP app and got crash while running (not compiling) the app on setting class's private string variable through the setter, here's the header file :
class Car
{
private:
int year;
std::string brand;
std::string model;
int price;
std::string currency;
public:
int setYear(int x){this->year = x;}
std::string setBrand(std::string x){this->brand = x;}
std::string setModel(std::string x){this->model = x;}
int setPrice(int x){this->price = x;};
std::string setCurrency(std::string x){this->currency = x;}
};
and here's the main: n - number of objects temp - temporary variable for passing integers temp1 - temporary variable for passing strings
ifstream fd("input.in");
int n;
fd >> n;
int temp;
string temp1;
Car A[n];
for(int i = 0; i < 3; i++)
{
fd >> temp;
A[i].setYear(temp);
fd >> temp1;
A[i].setBrand(temp1); //Crashes Here
fd >> temp1;
A[i].setModel(temp1);
fd >> temp;
A[i].setPrice(temp);
fd >> temp1;
A[i].setCurrency(temp1);
}
after little test i figured out that it crashes then code tries to set "brand" variable. What's the problem?
i < 3? Where did 3 come from?