0

i'm trying to create array of object and display it but for some reason it's giving wrong out put i think i'm using the new operator wrong way but i'not sure what's wrong

#include<bits/stdc++.h>
using namespace std;
class X {
    string s;
    int b;
public:

    void set(string s,int b ) {
        this->s = s;
        this->b = b;
    }
    void display() {
        cout << this->s << ' ' << this->b << '\n';
    }

};

int main() 
{
    int t;
    cin >> t;
    for (int i = 0; i < t; i++) {
        int n;
        cin >> n;
        X* a = new X[n];
        for (int j = 0; j < n; j++) {
            string s; int b;
            cin >> s >> b;
            a[i].set(s, b);
        }

        for (int i = 0; i < n; i++) {
            a[i].display();
        }

    }
}

input: 1 3 abhi 1 omkar 2 ritesh 3

output: ritesh 3 0 0

  • 1
    Did you try to use `std::endl` instead of `\n` in your display() method ? Otherwise the program is not forced to display all the contents. – Arne J Dec 06 '19 at 08:22
  • *it's giving wrong out put* -- We have no idea what the input is, thus the output you're getting cannot be determined, given what you've posted. – PaulMcKenzie Dec 06 '19 at 08:24
  • 1
    Memory leak alert. `delete[] a;`... or use `std::unique_ptr`.... or `std::vector`. – JHBonarius Dec 06 '19 at 08:26
  • You might want to take a look at: https://stackoverflow.com/questions/5131647/why-would-we-call-cin-clear-and-cin-ignore-after-reading-input – Vinícius Dec 06 '19 at 08:33
  • Take a closer look at your indexing until you see the problem. (Lesson to learn: use visually distinct and meaningful identifiers.) – molbdnilo Dec 06 '19 at 09:21
  • Since you now have two (identically) wrong answers, I'm going to point out that you have written `a[i]` where you should have `a[j]` in the inner loop, and vote to close this as a typo. – molbdnilo Dec 06 '19 at 14:47

2 Answers2

1

You have to instantiate each and every object in the array of objects. And try to use getline function to get the input of the string.

#include<bits/stdc++.h>
using namespace std;
class X {
    string s;
    int b;
public:

    void set(string s,int b ) {
        this->s = s;
        this->b = b;
    }
    void display() {
        cout << this->s << ' ' << this->b << '\n';
    }

};

int main() 
{
    int t;
    cin >> t;
    for (int i = 0; i < t; i++) {
        int n;
        cin >> n;
        X *a[n];
        for (int j = 0; j < n; j++) {
            a[j] = new X;
            string s; int b;            
            cin >> s >> b;
            a[j]->set(s, b);
        }

        for (int i = 0; i < n; i++) {
            a[i]->display();
        }

    }
}

Hope this might helps : )

VJAYSLN
  • 473
  • 4
  • 12
  • They are instantiated (default-constructed) in OP's code. (Note that OP does not have an array of pointers, but of `X`s.) Also, your variable-length array is non-standard. – molbdnilo Dec 06 '19 at 10:45
0

The error is in the initialization of the array. You use X* a = new X[n]; as initialization of pointer to class X. For a correct initialization you have to use the following lines :

X* a[n];
for(int k = 0 ; i < n ; ++i){
    a[k] = new X; 
}

Pay attention to the use of pointer. Probably you don't need it. In the case you don't need it, you can use the following line to initialize the array: X a[n]; Now you can use the array without pointer so you can substitute the ->(arrow) with the .(point) to call functions or access variables of the class.

Another tips: use std::endl instead of '\n' in the display function or in general in the std::cout.

Zig Razor
  • 3,381
  • 2
  • 15
  • 35