Problems with UnsortedList

Hello. I am having trouble with the functions in my program, particularly the getChoice function and how to flesh that out to properly follow the guidelines of my Professor. Here is the assignment:

https://imgur.com/o3fVucy

Here's what I have so far...

UnsortedList.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#pragma once
#include <iostream>
#include <string>
using namespace std;

const int MAX_SIZE = 10;

class UnsortedList
{
private:
	int numbers[MAX_SIZE];
	int length;
	char choice;
public:
	UnsortedList();
	~UnsortedList();
	void insertItem(int item);
	void deleteItem(int item);
	bool isFull() const;
	bool isEmpty() const;
	void displayList() const;
	void showMenu();
	void getChoice();
};


UnsortedList.cpp

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
#pragma once
#include <iostream>
#include <string>
#include "UnsortedList.h"
using namespace std;

UnsortedList::UnsortedList()
{
	length = 0;
}

UnsortedList::~UnsortedList() {}

void UnsortedList::insertItem(int item)
{
	numbers[length] = item;
	length++;
}

void UnsortedList::deleteItem(int item)
{
	int location = 0;

	while (item != numbers[location] && location < length)
		location++;
	if (location < length)
	{
		numbers[location] = numbers[length - 1];
		length--;
	}
	else
	{
		cout << "The ID is not in the list." << endl;
	}
}

bool UnsortedList::isFull() const
{
	return (length == MAX_SIZE);
}

bool UnsortedList::isEmpty() const
{
	return (length == 0);
}

void UnsortedList::displayList() const
{
	int location = 0;
	if (location < length)
	{
		while (location < length)
		{
			cout << numbers[location] << endl;
			location++;
		}
	}
	else
		cout << "List is empty." << endl;
}

void UnsortedList::showMenu()
{
	cout << "a. Insert a number into the list." << endl;
	cout << "b. Delete a number from the list." << endl;
}

void UnsortedList::getChoice()
{
	cout << "Enter your choice: ";
	cin >> choice;
	if (choice == 'a')
	{	cout << "Enter a number: ";
	cin >> numbers[length];
	isFull();
	}
	else {
		cout << "Enter the number to be deleted: " << endl;
		cin >> numbers[length];
		isEmpty();
	}
}


main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#pragma once
#include <iostream>
#include <string>
#include "UnsortedList.h"
using namespace std;

int main()
{
	UnsortedList list;

	list.showMenu();
	list.getChoice();
	list.displayList();
}
Last edited on
Are you sure that showMenu and getChoice() should be members of UnsortedList? It makes more sense for the list to manage the data and nothing else.

I think getChoice() should return the letter chosen. The logic to insert or delete should be inside main().

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
char getChoice()
{
	char choice;
	cout << "Enter your choice: ";
	cin >> choice;
	return choice;
}

int main()
{
	UnsortedList list;

	showMenu();
	char choice = getChoice();
	if (choice == 'a') {
		int num;
		cout << "Enter a number: ";
		cin >> num;
		list.insertItem(num);   // TO DO: check for full list
	} else if (choice == 'b') {
		int num;
		cout << "Enter a number to be deleted: ";
		cin >> num;
		list.deleteItem(num);   // TO DO: Check for empty list
	}
}

I'm getting an error under "showMenu();" it says that it's "identifier is undefined"...there are no other redlines. Would you happen to know why this is happening? Thanks.
nevermind. it was supposed to be list.showMenu();
How do I loop the menu instead of looping just choice a? I want to loop the menu and simultaneously check for full and empty lists.

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
int main()
{
	UnsortedList list;

	list.showMenu();
	char choice = getChoice();
	if (choice == 'a') {
		while (!list.isFull()) {
			int num;
			cout << "Enter a number: ";
			cin >> num;
			list.insertItem(num);   // TO DO: check for full list
			if (list.isFull())
				cout << "List is full." << endl;
		}
	}
	else if (choice == 'b') {
		int num;
		cout << "Enter a number to be deleted: ";
		cin >> num;
		list.deleteItem(num);   // TO DO: Check for empty list
		if (list.isEmpty())
			cout << "The list is empty." << endl;
	}

	list.displayList();
}
This worked, for reference.

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
int main()
{
	UnsortedList list;

	list.showMenu();
	while (!list.isFull() || list.isEmpty()) {
		char choice = getChoice();
		if (choice == 'a') {
			int num;
			cout << "Enter a number: ";
			cin >> num;
			list.insertItem(num);
			if (list.isFull())
				cout << "List is full." << endl;
		}
	
		else if (choice == 'b') {
			int num;
			cout << "Enter a number to be deleted: ";
			cin >> num;
			list.deleteItem(num);   
			if (list.isEmpty())
				cout << "The list is empty." << endl;
		}

	}
	
	list.displayList();
}
Topic archived. No new replies allowed.