Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi any way to do this without pointer , cuz this isnt working. Basically a linea

ID: 3630419 • Letter: H

Question

Hi any way to do this without pointer , cuz this isnt working. Basically a linear search is implemented to search for a name in the array of objects.
There has to be an easy solution, we have just begun OOP!

#include<iostream>
#include<string>
using namespace std;

class reg
{
private:
char name[20];
float cgpa;
int roll;

public:

void setname()
{
cout<<"Enter name ";
cin>>name;
}

void setcgpa()
{
cout<<"Enter cgpa ";
cin>>cgpa;
}

void setrollno ()
{
cout<<"Enter roll number ";
cin>>roll;
}

char* findname(){return name;}

};


int main()
{

reg student[5];
char names[50];

for(int u=0;u<5;u++)
{
student[u].setname();
student[u].setcgpa();
student[u].setrollno();
}

cout<<"Enter name to search ";
cin>>names;

for (int y=0;y<5;y++)
{
if (strcmp(student[y].findname(),names)==0)
{
cout<<"Name found at index "<<y+1;
}
}

Explanation / Answer

// use string in class than char

#include<iostream>
#include<string>
using namespace std;

class reg
{
private:
string name;
float cgpa;
int roll;

public:

void setname()
{
cout<<"Enter name ";
cin>>name;
}

void setcgpa()
{
cout<<"Enter cgpa ";
cin>>cgpa;
}

void setrollno ()
{
cout<<"Enter roll number ";
cin>>roll;
}

string findname(){return name;}

};


int main()
{

reg student[5];

string names;
for(int u=0;u<5;u++)
{
student[u].setname();
student[u].setcgpa();
student[u].setrollno();
}

cout<<"Enter name to search ";
cin>>names;

for (int y=0;y<5;y++)
{
if (strcmp(student[y].findname(),names)==0)
{
cout<<"Name found at index "<<y+1;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote