help in writing a class

Hello,

I am having issue in writing the showName() function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//CS 540, Final Exam, Fadi AL Sheikh Aleais. Problem2.

#include<iostream>
#include<string>

using namespace std;

class Student{
public:
	Student(const string & name, unsigned number);
	string getName() const;
	unsigned getNumber() const;
	Student & setName(const string & name);
	Student & setNumber(unsigned number);

private:
	string myName;
	unsigned myNumber;
}; // class Student

bool die(const string & msg);
void showName(const Student ar[], unsigned els, unsigned number);

int main()
{
Student cs[] = { { "Nick", 13 }, { "Chris", 10 }, { "Fadi", 8 }, { "Hely", 4 }, { "Brown", 3 } };
	showName(cs, 5, 3);
}//main

Student::Student(const string & name, unsigned number) :myName(name), myNumber(number)
{
	if (number > 10000 && number == 10000) die("Unaccepted input.");
}

string Student::getName() const
{
	return myName;
}
unsigned Student::getNumber() const
{
	return myNumber;
}
Student & Student::setName(const string & name)
{
	myName = name;
	return *this;
}
Student & Student::setNumber(unsigned number)
{
	if (number > 10000 && number == 10000) die("Unaccepted input.");
	myNumber = number;
	return *this;
}

void showName(const Student ar[], unsigned els, unsigned number)
{
	for (unsigned x = 0; x < els; x++)
	{
		if (ar[x].getNumber() == number)
		{
			cout << ar[x].getName() << endl;
		}
		else
		cout << die("not found") << endl;
	}
}

bool die(const string & msg)
{
	cerr << "Fatal error: " << msg << endl;
	exit(EXIT_FAILURE);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//CS 540, Final Exam, Fadi AL Sheikh Aleais. Problem2.

#include<iostream>
#include<vector>
#include<string>
#include<algorithm>

using namespace std;

class Student{
public:
	Student(const string name, unsigned number);
	string getName() const;
	unsigned getNumber() const;
	void setName(string name);
	void setNumber(unsigned number);

private:
	string myName;
	unsigned myNumber;
}; // class Student

void die(const string & msg);
void showName(vector<Student> &ar , unsigned els, unsigned number);

int main()
{
	// storage for students
	vector<Student> students;

	Student cs1("Nick", 13);
	Student cs2("Chris", 10);
	Student cs3("Fadi", 8);
	Student cs4("Hely", 4);
	Student cs5("Brown" ,3);

	students.push_back(cs1);
	students.push_back(cs2);
	students.push_back(cs3);
	students.push_back(cs4);
	students.push_back(cs5);
	
	showName(students, 5, 3);

	int x;
	cin >> x;
	return 0;
}//main

Student::Student(const string name, unsigned number) :myName(name), myNumber(number)
{
	if (number > 10000 && number == 10000) 
	{
		die("Unaccepted input.");

	}
}

string Student::getName() const
{
	return myName;
}
unsigned Student::getNumber() const
{
	return myNumber;
}
void Student::setName(string name)
{
	myName = name;
	
}
void Student::setNumber(unsigned number)
{
	if (number > 10000 && number == 10000) 
	{
		die("Unaccepted input.");
	}

	myNumber = number;	
}

void showName(vector<Student> &ar , unsigned els, unsigned number)
{	
	try
	{
		// look at position - 1 because start index is 0
		if(ar.at(els-1).getNumber() == number)
		{
			std::cout << ar.at(els-1).getName() << endl;
		}
	}
	catch(std::exception &e)
	{
		// if error occurs
		die("not found");
	}
		
}

void die(const string & msg)
{
	cerr << "Fatal error: " << msg << endl;
}
Last edited on
how can I do it without vector array
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//CS 540, Final Exam, Fadi AL Sheikh Aleais. Problem2.
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>

using namespace std;

class Student{
public:
	Student(const string name, unsigned number);
	string getName() const;
	unsigned getNumber() const;
	void setName(string name);
	void setNumber(unsigned number);

private:
	string myName;
	unsigned myNumber;
}; // class Student

void die(const string & msg);
void showName(Student cs[] , unsigned els, unsigned number);

int main()
{
	// storage for students
	Student cs[] = { Student("Nick", 13 ), Student( "Chris", 10 ), Student( "Fadi", 8 ), Student( "Hely", 4 ), Student( "Brown", 3 ) };
	
	showName(cs, 5, 3);

	int x;
	cin >> x;
	return 0;
}//main

Student::Student(const string name, unsigned number) :myName(name), myNumber(number)
{
	if (number > 10000 && number == 10000) 
	{
		die("Unaccepted input.");

	}
}

string Student::getName() const
{
	return myName;
}
unsigned Student::getNumber() const
{
	return myNumber;
}
void Student::setName(string name)
{
	myName = name;
	
}
void Student::setNumber(unsigned number)
{
	if (number > 10000 && number == 10000) 
	{
		die("Unaccepted input.");
	}

	myNumber = number;	
}

void showName(Student cs[], unsigned els, unsigned number)
{	
	try
	{
		// look at position - 1 because start index is 0
		if(cs[els-1].getNumber() == number)
		{
			std::cout << cs[els-1].getName() << endl;
		}
	}
	catch(std::exception &e)
	{
		// if error occurs
		die("not found");
	}
		
}

void die(const string & msg)
{
	cerr << "Fatal error: " << msg << endl;
}
Last edited on
Still not working, and I am wondering why you got rid of the FOR loop
you don't really need the for loop you are only searching for 1 number.

you are already passing the index of where the name should be.

it works

http://cpp.sh/92z7
closed account (SECMoG1T)
your for loop was not the problem, the problem was in your if , else// control you only permitted your loop to iterate once since your else part was true on the first iteration the die function always caused your funtion to exit.
here av got the bug free version of your prog.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include<iostream>
#include<string>
#include <cstdlib>

using namespace std;

class Student{
public:
	Student(const string & name, unsigned number);
	string getName() const;
	unsigned getNumber() const;
	Student & setName(const string & name);
	Student & setNumber(unsigned number);

private:
	string myName;
	unsigned myNumber;
}; // class Student

bool die(const string & msg);
void showName(const Student ar[], unsigned els, unsigned number);

int main()
{
Student cs[] = { { "Nick", 13 }, { "Chris", 10 }, { "Fadi", 8 }, { "Hely", 4 }, { "Brown", 3 } };
	showName(cs, 5, 3);
}//main

Student::Student(const string & name, unsigned number) :myName(name), myNumber(number)
{
	if (number > 10000 && number == 10000) die("Unaccepted input.");
}

string Student::getName() const
{
	return myName;
}
unsigned Student::getNumber() const
{
	return myNumber;
}
Student & Student::setName(const string & name)
{
	myName = name;
	return *this;
}
Student & Student::setNumber(unsigned number)
{
	if (number > 10000 && number == 10000) die("Unaccepted input.");
	myNumber = number;
	return *this;
}

void showName(const Student ar[], unsigned els, unsigned number)
{
    if(number>=0&&number<els)
	{
	    for (unsigned x = 0; x < els; x++)
	    {
		   if (ar[x].getNumber() == number)
		    {
			  cout << ar[x].getName() << endl;
		    }

                  else if(x==(els-1))  ///if my "if" part is still not true by now i should exit
                        die("not found");

            }
	}

    else
      cout << die("not found") << endl;
}

bool die(const string & msg)
{
	cerr << "Fatal error: " << msg << endl;
	exit(EXIT_FAILURE);
}
Last edited on
the error checking for over 10000 is also not working, what's wrong with it
Mostly because it can never be true. you can't have 100000 be greater than itself and there is no other number that equals to 100000 other than itself.
Last edited on
So what is the alternative code
closed account (SECMoG1T)
I found that this patr surely missed some logic if(number>=0&&number<els) but then am getting confused because i don't seem to realize what this number really represent. I also found that your constructor appears to completely ignore the check in its body, to bad .. however av slightly altered the function to do what you intend.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include<iostream>
#include<string>
#include <cstdlib>

using namespace std;

class Student{
public:
	Student(const string & name, unsigned number);
	string getName() const;
	unsigned getNumber() const;
	Student & setName(const string & name);
	Student & setNumber(unsigned number);

private:
	string myName;
	unsigned myNumber;
}; // class Student

bool die(const string & msg);
void showName(const Student ar[], unsigned els, unsigned number);

int main()
{
Student cs[] = { { "Nick", 13 }, { "Chris", 10000 }, { "Fadi", 8 }, { "Hely", 4 }, { "Brown", 3 } };
	showName(cs, 5, 10000);
}//main

Student::Student(const string & name, unsigned number) :myName(name), myNumber(number)
{
	if (number > 10000 && number == 10000) die("Unaccepted input.");
}

string Student::getName() const
{
	return myName;
}
unsigned Student::getNumber() const
{
	return myNumber;
}
Student & Student::setName(const string & name)
{
	myName = name;
	return *this;
}
Student & Student::setNumber(unsigned number)
{
	if (number > 10000 && number == 10000) die("Unaccepted input.");
	myNumber = number;
	return *this;
}

void showName(const Student ar[], unsigned els, unsigned number)
{

   bool found(false);/// flag just to avoid extra iteration , alternatively you could use break.
   size_t index(0);

   while(index<els&&!found)
   {
       if(ar[index].getNumber()==number)
       {
           cout<<ar[index].getName()<<endl;
           found=true;
       }

       else if(index==(els-1)&&!found)
            die("Not found!");

       else
        ++index;
   }
}




bool die(const string & msg)
{
	cerr << "Fatal error: " << msg << endl;
	exit(EXIT_FAILURE);
}


Note that the function will only show the first person holding the
number if you had multiple students with the same number you'll need to remove the fleg but break immediately index==(els-1) is reached, certainly you dont want to get out of range.
Last edited on

The Student ctor(s) and the setNumber mutator all make sure that number is < 10000, otherwise they c&d.

showName's job is to search the Student array for an element whose myNumber field is equal to the number arg. If there is no such element in the array, showName outputs
not found
If there is such an element, in the array, showName outputs the myName field of that element.
showName may assume without checking that there will not be more than one element in the array with the desired number.
Topic archived. No new replies allowed.