I am currently working on a project which is a ticket storage system that stores each ticket holder as an object.
To input the values, I am using a parameterized constructor. In the main function I have declared a dynamic memory block for an array of these objects.
The main issue I face is that in the for loop that initializes each object, the loop only runs once, then terminates. Here is the code:
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;
class node
{
string holder_name;
int age;
public:
node(string a, int b)
{
holder_name = a;
age = b;
}
void view()
{
cout << "Name: " << holder_name << endl;
cout << "Age: " << age << endl;
}
};
int main()
{
int count, i;
cout << "Enter no of nodes" << endl;
cin >> count;
node *arr = (node *)malloc(sizeof(node) * count);
for (i = 0; i < count; i++)
{
int b;
string str;
cout << "Enter name" << endl;
cin >> str;
cout << "Enter age" << endl;
cin >> b;
arr[i] = node(str, b);
arr[i].view();
}
return 0;
}