Singly Linked List Switch Case Problem

Hello, I am having a problem with my linked list. When I try to run the code, my switch cases all seem to show up when I press a certain key. I don't know what it wrong with this can someone help?

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
  /* Marcin Chmielewski
Date Started: March 14, 2016
Date Finished: March 18, 2016*/

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

using namespace std;

char getAction(string prompt) {
	string answer = "";
	char firstChar = '?';
	cout << endl << prompt;
	getline(cin, answer);
	while (answer.length() == 0) {
		getline(cin, answer);
	}
	firstChar = tolower(answer[0]);
	return firstChar;
}

int main() {
	singleListInt firstRun;
	int value;
	string prompt = "Enter (a)dd, (d)elete, (f)ind, (p)rint, (c)lear list, (s)ize of list or (q)uit: ";
	char action;
	do {
		action = getAction(prompt);
		switch (action) {
		case 'a': 
		{
			cout << "Enter int to add to list: ";
			cin >> value;
			//To Do: Finish
			firstRun.add(value);
		}
		case 'd':
		{
			cout << "Enter int to be deleted from list: ";
			cin >> value;
			//To Do: Finish
			firstRun.remove(value);
		}
		case 'f': 
		{
			cout << "Enter int to be search in list: ";
			cin >> value;
			//To Do: Finish
			firstRun.contains(value);
		}
		case 'p': 
		{
			cout << "The list is\n";
			//To Do: Finish
			firstRun.print();
		}
		case 'c': 
		{
			cout << "The list will be erased.\n";
			//To Do: Finish
			firstRun.clearList();
		}
		case 's': 
		{
			cout << "The number of ints in the list is " << firstRun.size() << endl;
			break;
		}
		case 'q': 
		{
			cout << "Quitting...\n";
			break;
		}
		default: 
		{
			//do nothing
		}
	}
} while (action != 'q');
	cout << "Done.\n";
	return 0;
}


This is my main code up here.

This is my Linked List header file

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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#ifndef __SINGLY_LINKED_LIST__
#define __SINGLY_LINKED_LIST__
#include <iostream>
using namespace std;
struct IntNode
{
	int data;
	IntNode * next;
	IntNode(const int & d = 0, IntNode *n = nullptr) : data(d), next(n) {}
};

class singleListInt
{
public:
	singleListInt() { init(); } //default constructor and only constructor
	~singleListInt() { eraseList(head); } //destructor
	singleListInt(const singleListInt & rhs) { //Copy Constructor {
		eraseList(head);
		init();
		*this = rhs;
}
bool add(int x) {
	IntNode *current;
	IntNode *prev;
	IntNode *temp;
	prev = head;
	current = head->next;

	if (contains(x)) {
		return false;
	} else {
		//To Do: Write code to add x keeping list sorted : Done
		IntNode* myNode = new IntNode(x);

		while (current != nullptr && x > current->data) {
			if (myNode < current) {
				temp = prev->next;
				prev->next = myNode;
				myNode->next = temp;
				current = current->next;
				prev = prev->next;
			}
		}
	}
	return true;
}
bool remove(int x) {
	IntNode *current;
	IntNode *prev;
	IntNode *temp;
	prev = head;
	current = head->next;
	IntNode* node = new IntNode(x);
	if (!contains(x)) {
		return false;
	}
	else {
		//To Do: Write code to remove node containing x
		while (current != nullptr && x > current->data) {
			if (current->data == x) {
				delete node;
			}
		}
	}
	return true;
}
int size() { //To Do: Implement : Done
	IntNode *n;
	int i = 0;

	n = head;
	while (n != head) {
		n = n->next;
		i++;
	}
	return i;

}
void print() {
		IntNode *ptr = head->next;
		//To Do: Print list in ascending order with one space between values : Done.
		if (head == nullptr) {
			cout << "The list is" << endl;
		}
		while (ptr != nullptr) {
			cout << ptr;
			ptr = ptr->next;
		}
		cout << endl;

		return;
}
bool contains(const int & x) {
	IntNode *current;
	current = head;
	IntNode* myNode = new IntNode(x);
		//To Do: return true x is in list or return false if x is not in list : Done.
	while (current != nullptr) {
		if (current == myNode) {
			return true;
		}
		else {
			return false;
		}
	}
}
void clearList() {
		eraseList(head);
		init();
		return;
}
private:
	void init() {
		theSize = 0;
		head = new IntNode;
		head->next = nullptr;
	}
	void eraseList(IntNode * h) {
		//To Do: remove all the nodes in the list : Done.
		for (int i = 0; i < theSize; i++) {
			remove(i);
		}
		return;
	};
	IntNode *head;
	int theSize;
};
#endif 
After each case in your switch statements add a break; statement to break out of the switch statement after one of your cases has finished executing.
Hi,

You should have a break; at the end of each case, otherwise execution falls through to the next one :+)

Instead of the do loop, consider using a bool controlled while loop, look at my example near the end of this post:
http://www.cplusplus.com/forum/beginner/186137/#msg907712

Consider splitting your header file into header (hpp) and implementation cpp. The class declaration is in the header, the functions are in the cpp file.

Don't have using namespace std; it will bite you one day :+) Just put std:: before each std thing, believe me it's the best way. Google it :+)

Good Luck!!
Topic archived. No new replies allowed.