-1

I tried this code but when I am calling the member function inside the loop it is giving the garbage value of the details and when I am calling the member function outside the loop it is giving me error.

#include<iostream>
#include<string.h>

using namespace std;
class student
{   
  char name[10];
  int id,rollno;
  public:
  student(char name[10],int id,int rollno)
{
  strcpy(this->name,name);
  this->id=id;
  this->rollno=rollno;
    cout<<"the name of the student is:"<<name<<endl;
    cout<<"the id of the student is:"<<id<<endl;
    cout<<"the roll no of the student is:"<<rollno<<endl;
}

};

int main()
 {

 int id1,rollno1;
 char name1[10];
 for(int i=1;i<=2;i++)
{  
    cout<<"             enter the detail of the student "<<i<<"                       "<<endl;
    cout<<"enter the name of the student:";
    cin>>name1;
    cout<<"enter the id of the student:";
    cin>>id1;  
    cout<<"enter the roll no of the student:";
    cin>>rollno1;
    student d[]={student(name1,id1,rollno1)};
   d[i].print();
}

return 0;
}
Mat
  • 202,337
  • 40
  • 393
  • 406

1 Answers1

0

Here's your code review.

#include <iostream> 
#include <string.h>

using namespace std; // it is strongly suggested that you don't use the whole header unless your intent is speeding up coding
class Student // capitalize classes
{
    char _name[10];// it is ok, however, if you use the <string> header, why would you go back to char type?
    int _id, _rollno;
public:
    Student(char name[10] /* this is just the array's first item passed! */, int id, int rollno)
    {
        // Don't use this->, use an underscore for one of the names. 
        // I use underscores for the encapsulated data
        strncpy_s(_name, name, 9);// a safer version
        _id = id; 
        _rollno = rollno;
        cout << "\nCtor says: ";
        cout << "the name of the student is: " << _name << endl;
        cout << "the id of the student is: " << _id << endl;
        cout << "the roll no of the student is: " << _rollno << endl;
        cout << '\n';
    }
    const char* getName() { return _name; }
    const int getId() { return _id; }
    const int getRollNo() { return _rollno; }
    // unless you overload operator<< , you have to make getters for your info, or make it public
};

int main()
{
    int id, rollno;// why 1? they won't intersect with the function
    char name[10];
    Student *s[2];//you must make that on the heap; otherwise you need a default constructor

    for (int i{ 0 }; i < 2; i++)// start with 0 and make it just <
    {
        cout << "enter the details of the student " << i << endl;// Why so many spaces?
        cout << "enter the name of the student: "; // add space after the colon
        cin >> name;// remember, you're limited to 9 chars + \n !!!
        cout << "enter the id of the student: ";
        cin >> id;
        cout << "enter the roll no of the student: ";
        cin >> rollno;
        //student d[] = { student(name,id,rollno) }; // you can't do this. It's not flexible.
        // You either assign enough space for the intended number of students in the stack, statically, or 
        // you lett it find more space in the heap = dynamically

        //this goes to the heap
        s[i]= new Student( name,id,rollno );// parentheses are well
        //s[i] = new Student{ name,id,rollno };// initializer list may also do

        //d[i].print();// what's to print here? It's not POD, cout needs instructions
        cout << "Stored data -> Id: " << 
            s[i]->getId() << ", Name: "  // -> dereferences the pointer, and s is a pointer now
            << s[i]->getName() << ", Roll no: " 
            // or you can dereference it
            << (*s[i]).getRollNo() 
            << endl << endl; // make some extra space here
        
    }

    return 0;
}
Mykola Tetiuk
  • 153
  • 1
  • 13
  • The `` header declares `std::string`, which OP's code does _not_ use. OP's code _does_, however, use `strcpy(3)`, which is declared in ``; the "canonical C++" way to refer to `` is ``. – Vainstein K Nov 13 '21 at 18:17
  • @VainsteinK you're right. I didn't pay attention to it. – Mykola Tetiuk Nov 14 '21 at 05:00