Need to do a project for school using arrays of class objects. We can't use vectors, so any answers suggesting them will not help. Ive been trying to create such and array, then put a name in each object. Ive tried running a for loop to do this, and it keeps skipping the first object in the array. Help?
#include <iostream>
#include <cstring>
using namespace std;
class Car
{
private:
char* driver;
public:
void setDriver(char* name)
{
driver = name;
}
void getDriver()
{
cout<<driver;
}
};
int main()
{
int numDrivers;
cout<<"How many drivers would you like?";
cin>>numDrivers;
Car* roster = new Car[numDrivers];
for(int i=0;i<numDrivers;i++)
{
char* name;
name = new char[20];
cout<<"name:";
cin.getline(name, 20);
roster[i].setDriver(name);
}
for(int i=0;i<numDrivers;i++)
{
roster[i].getDriver();
cout<<".\n";
}
return 0;
}
Ive toyed with the ranges of my for loops, it still always does the same thing when it hits the loop to set the names for the drivers. looks like this
How many drivers would you like?: 4
name:name: name1
name: name2
name: name3
.
name1.
name2.
name3.
any help is greatly appreciated.