why the function doesn't work correctly ?

November 16, 2010 at 19:49:59
Specs: Macintosh
i wrote a simple program, but the search function doesn't work well??????????????PLZ

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

void find_person(char* name);
// 1
class person
{
private:
char name[31];
public:
virtual void get_data()
{
cout << " Enter name : ";
cin >> name;
}
virtual void put_data()
{
cout << "\n Name is : " << name;
}

virtual void isOutstanding()
{
}
char* name_per() {
return name;
}
char* get_name() {
return name;
}
};
// 2
class student : public person
{
private:
float gpa;
public:
void get_data()
{
person::get_data();
cout << " Enter Student's GPA : ";
cin >> gpa;
}
void put_data()
{
person::put_data();
cout << "\n GPA = : " << gpa;
}
void isOutstanding()
{
if (gpa > 3.5)
cout << "(This person is outstanding!!!)";
}
};
// 3
class teacher : public person
{
private:
int num_pups;
public:
void get_data()
{
person::get_data();
cout << " Enter number of pups : ";
cin >> num_pups;
}
void put_data()
{
cout << "\n Number of Pups = " << num_pups;
}
void isOutstanding()
{
if (num_pups > 100)
cout << " This person is outstanding!!!" << endl;
}
};
//////////////////////////////////////////////////////////////

person* persptr[100];
int n =0;
int main (int argc, char * const argv[]) {
char ch;
char name[21];

do {
cout << "\nEnter Person / Student / Teacher / or 'S' to search (p/s/t/S) : ";
cin >> ch;

if (ch == 's')
persptr[n] = new student;
else if (ch == 't')
persptr[n] = new teacher;
else if (ch == 'p')
persptr[n] = new person;
else {
cout << "Enter name to search : ";
cin >> name;
find_person(name);
}

persptr[n++]->get_data();
cout << "\nDo you wnat to enter another person(y/n)? : ";
cin >> ch;
} while (ch != 'n');
cout << "------------------------------------------------\n";
cout << "------------------------------------------------\n";
for (int j=0; j<n; j++) {
persptr[j]->put_data();
persptr[j]->isOutstanding();
}

return 0;
}

//////////////////// this is my problem /////////////////////

void find_person(char* name) {
for (int j=0; j<n; j++) {

if ( strcmp (persptr[j]->get_name(), name) ==0 ) {

persptr[j]->put_data();
//break;
}
else {
cout << "Person name is not found!!!" << endl;
//break;
}
}
}
///////////////////////////////////////////////////////////////////////


See More: why the function doesnt work correctly ?

Report •

#1
November 16, 2010 at 22:07:24
void find_person(char* name) {
for (int j=0; j<n; j++) {

if ( strcmp (persptr[j]->get_name(), name) ==0 ) {

persptr[j]->put_data();
//break;
}
else {
cout << "Person name is not found!!!" << endl;
//break;
}
}
}
........................................................................................................................
1 : uncomment break in if statement.
2 : cout should be out side for loop with if statement or add another if statement.
........................................................................................................................
this line will give access violation after find_person
.......................................................................................................................
_Adnan_


Report •

#2
November 16, 2010 at 22:10:33
.................................................................
persptr[n++]->get_data();

this line will give access violation......
.................................................................
sorry forgot that.
.................................................................

_Adnan_


Report •

#3
November 17, 2010 at 18:54:06
hey i tried it and looks better but it still doesn't work,
the problem is : " the result of the search process only shows the first person I entered
this is the program when I run it :
//////////////////////////////////////////////////

Last login: Wed Nov 17 21:45:40 on ttys002
/Users/nawaryoussef/Documents/C++\ Language/Person_S_T/build/Debug/Person_S_T ; exit;
nawar-youssef-MacBook-Pro:~ nawaryoussef$ /Users/nawaryoussef/Documents/C++\ Language/Person_S_T/build/Debug/Person_S_T ; exit;

Enter Person / Student / Teacher / or 'S' to search (p/s/t/S) : s
Enter name : nawar
Enter Student's GPA : 3.2

Do you wnat to enter another person(y/n)? : y

Enter Person / Student / Teacher / or 'S' to search (p/s/t/S) : t
Enter name : youssef
Enter number of pups : 109

Do you wnat to enter another person(y/n)? : y

Enter Person / Student / Teacher / or 'S' to search (p/s/t/S) : S
Enter name to search : youssef
Person name is not found Here!!! //if i wrote nawar to search it works because it the first person

no more iformation in this ducoment !!!
Segmentation fault
logout

[Process completed]


Report •

Related Solutions

#4
November 17, 2010 at 23:40:00
Show me the modified code......................

............................................................................................
_Adnan_


Report •

#5
November 18, 2010 at 00:23:14
it works. you just need to modify this function.
....................................................................................................................
void put_data()
{
cout << "\n Number of Pups = " << num_pups;
}
.......................................................................................................................

and try cin.getline(...).........................

.....................................................................................................................
_Adnan_


Report •

#6
November 18, 2010 at 21:15:22
1- i forgot to write the person::put_data() thank you
2- the wrong still, i think it is with the loop or something in the store data codes
and about cin.getline() idon't know how to use it or where ... Sorry , this is the code if you have time
////////////////////////////////////////////////////////////////
#include <iostream.h>
#include <string.h>

void find_person(char* name);
// 1
class person
{
private:
char name[31];
public:
virtual void get_data()
{
cout << " Enter name : ";
cin >> name;
}
virtual void put_data()
{
cout << "\n Name is : " << name;
}

virtual void isOutstanding()
{
}
char* name_per() {
return name;
}
char* get_name() {
return name;
}
};
// 2
class student : public person
{
private:
float gpa;
public:
void get_data()
{
person::get_data();
cout << " Enter Student's GPA : ";
cin >> gpa;
}
void put_data()
{
person::put_data();
cout << "\n GPA = : " << gpa;
}
void isOutstanding()
{
if (gpa > 3.5)
cout << "(This person is outstanding!!!)";
}
};
// 3
class teacher : public person
{
private:
int num_pups;
public:
void get_data()
{
person::get_data();
cout << " Enter number of pups : ";
cin >> num_pups;
}
void put_data()
{
person::put_data();
cout << "\n Number of Pups = " << num_pups;
}
void isOutstanding()
{
if (num_pups > 100)
cout << " This person is outstanding!!!" << endl;
}
};
//////////////////////////////////////////////////////////////

person* persptr[100];
int n =0;
int main (int argc, char * const argv[]) {
char ch;
char name[21];

do {
cout << "\nEnter Person / Student / Teacher / or 'S' to search (p/s/t/S) : ";
cin >> ch;
if (ch == 's')
persptr[n] = new student;
else if (ch == 't')
persptr[n] = new teacher;
else if (ch == 'p')
persptr[n] = new person;
else {
cout << "Enter name to search : ";
cin >> name;
find_person(name); }

persptr[n++]->get_data();
cout << "\nDo you wnat to enter another person(y/n)? : ";
cin >> ch;
} while (ch != 'n');
cout << "------------------------------------------------\n";
cout << "------------------------------------------------\n";
for (int j=0; j<n; j++) {
persptr[j]->put_data();
persptr[j]->isOutstanding();
}

return 0;
}

//////////////////// this is my problem /////////////////////

void find_person(char* name) {
for (int j=0; j<n; j++) {

if ( strcmp (persptr[j]->get_name(), name) ==0 ) {
persptr[j]->put_data();
break;
}
else if ( strcmp (persptr[j]->get_name(), name) !=0 ) {
cout << "Person name is not found Here!!!" << endl;
break;
}
}
cout << "\nno more iformation in this ducoment !!!\n";
}
////////////////////////////////////////////////////////////


Report •

#7
November 19, 2010 at 01:08:07
Modify it where needed.

........................................................................................................................

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

void find_person(char* name);
// 1
class person
{
private:
char name[31];
public:
virtual void get_data()
{
cout << " Enter name : ";
cin >> name;
}
virtual void put_data()
{
cout << "\n Name is : " << name;
}

virtual void isOutstanding()
{
}

char* name_per()
{
return name;
}

char* get_name()
{
return name;
}
};
// 2
class student : public person
{
private:
float gpa;
public:
void get_data()
{
person::get_data();
cout << " Enter Student's GPA : ";
cin >> gpa;
}

void put_data()
{
person::put_data();
cout << "\n GPA = : " << gpa;
}

void isOutstanding()
{
if (gpa > 3.5)
cout << "(This person is outstanding!!!)";
}
};
// 3
class teacher : public person
{
private:
int num_pups;
public:
void get_data()
{
person::get_data();
cout << " Enter number of pups : ";
cin >> num_pups;
}

void put_data()
{
person::put_data();
cout << "\n Number of Pups = " << num_pups;
}

void isOutstanding()
{
if (num_pups > 100)
cout << " This person is outstanding!!!" << endl;
}
};
//////////////////////////////////////////////////////////////

person* persptr[100];
int n =0;
int main (int argc, char * const argv[])
{
char ch;
char name[21];

do
{
cout << "\nEnter Person / Student / Teacher / or 'S' to search (p/s/t/S) : ";
cin >> ch;

if (ch == 's')
persptr[n] = new student;
else
if (ch == 't')
persptr[n] = new teacher;
else
if (ch == 'p')
persptr[n] = new person;
else
{
cout << "Enter name to search : ";
cin >> name;
find_person(name);
continue;
}

persptr[n++]->get_data();
cout << "\nDo you wnat to enter another person(y/n)? : ";
cin >> ch;
} while (ch != 'n');

cout << "------------------------------------------------\n";
cout << "------------------------------------------------\n";

for (int j=0; j<n; j++)
{
persptr[j]->put_data();
persptr[j]->isOutstanding();
}

return 0;
}

//////////////////// this is my problem /////////////////////

void find_person(char* name)
{
for (int j=0; j<n; j++)
{
if ( strcmp (persptr[j]->get_name(), name) ==0 )
{
persptr[j]->put_data();
break;
}
else
{
if ( j == (n-1) )
cout << "Person name is not found!!!" << endl;
//break;
}
}
}

....................................................................................................................
_Adnan_


Report •

#8
November 19, 2010 at 20:49:17
ok thats better thank you so much,

Report •

Ask Question