Home > Blockchain >  How to initialize array of objects with user defined values and take input from user?
How to initialize array of objects with user defined values and take input from user?

Time:12-03

#include <iostream>
using namespace std;

class car{

string owner;
string car_num;
string issue_date;

car(string o, string cn, string id)
{
    owner = o;
    car_num  = cn;
    issue_date = id;
}

void getInfo()
{
    cout << "Car's Owner's Name : " << owner << endl;
    cout << "Cars' Number : " << car_num << endl;
    cout << "Car's Issue Date : " << issue_date << endl;
}

};

int main()
{
    
    int n;
    cout << "Enter total number of cars stored in your garage : \n";
    cin >> n;
    car c1[n]; //incomplete code due to the issue


    return 0;
}

Here I want to take the total car numbers from user. And also want to take the car properties from user by using a loop. But how Can I do that while using a constructor?

CodePudding user response:

My advice is not to over use the constructor. It supposed to construct, and really should only construct. In your case, you don't even really need a constructor. Instead add a new function to do initialization. Traditional is to use an operator >> which is often an external function.
As for the loop...

car c1[n]; //incomplete code due to the issue

is not legal C (although it's allowed in C, and many compilers that are also C compilers)
It's better to use a vector. So...

    vector<car> c1(n);
    for (auto& c : c1) 
       cin >> c;

An advanced technique is to use a istream iterator, which will allow you to use algorithms like std::copy, calling the input operator for each member of the vector. However, it's really not required, just a "nicety"

CodePudding user response:

Use pointer array instead.e.g.

car* c1[n];

//incomplete part
for(int i = 0; i < n; i  ){
    //take input and process
    c1[i] = new car(//put processed inputs here);
    }

PS: I feel like I made a mistake somewhere but can't test it now. If it doesn't work, put a comment here.

CodePudding user response:

You can use loop with std::cin, likefor(int i=0;i<n; i){std::cin<<num;}. The 'n' I mentioned in the code can also be assigned by std::cin

int n;
std::cin>>n;
car* cars=new car[n];
for(int i=0;i<n;  i)
{
   std::getline(cars[i].owner,std::cin);
   // and something other you'd like to do, like to test its validity
}

CodePudding user response:

car c1[n]; //incomplete code due to the issue

In fact, you have 2 issues here:

  1. Variable-Length Arrays (VLA) are not allowed in standard C . They are optionally allowed in standard C, and are supported by some C compilers as an extension.
  2. You can't have an array of objects w/o default constructor (unless you fully initialize it).

Assuming you don't want to change the class (other than insert public: after the data members), the modern solution should use std::vector:

    std::vector<car> c;

    //incomplete part
    for(int i = 0; i < n; i  ){
        std::string owner, car_num, issue_date;
        //TODO: get the strings from the user here ...
        c = c.emplace_back(owner, car_num, issue_date);
        }

CodePudding user response:

#include <iostream>
using namespace std;

class car{
public:
string owner;
string car_num;
string issue_date;

void cars(string o, string cn, string id)
{
    owner = o;
    car_num  = cn;
    issue_date = id;
    getInfo();
}

void getInfo()
{
    cout << "Car's Owner's Name : " << owner << endl;
    cout << "Cars' Number : " << car_num << endl;
    cout << "Car's Issue Date : " << issue_date << endl;
}

};

int main()
{
    
    int n;
    string a,b,c;
    cout << "Enter total number of cars stored in your garage : \n";
    cin >> n;
    car cas[n]; //incomplete code due to the issue
    for(int i=0;i<n;  i)
    {
     cout<<"value1:";
     cin>>a;
     cout<<"value2:";
     cin>>b;
     cout<<"value3:";
     cin>>c;
     cas[i].cars(a,b,c);
    }

    return 0;
}
  • Related