Linked List actions

I am stuck on a small portion of this stupid assignment. I dont need exact answers, just guidance.

The file that I need to open has multiple lines consisting of a character, and a name. The character determines what I do with that name, ex. a = add that string, R = remove the string, f = find the string. I am fairly new to this and cannot figure it out.

no code relevant to this issue has been written.
getline doesn't accomplish what i need.
Last edited on
pick something, and do it.
for example, try reading the file, sounds like you already started that.
what is not working with getline? getline will get the line, whether its one character or a full name or the entire world book encyclopedia.

if you getline and look at the first letter, IF the file is correct, it will be the 1 char.
getline again and you have the name.

try just reading that and printing 'got this action code with this name' and verify it against the file's contents, did you get the last record, etc? If that all works, then you can start the list part.

hint:
a linked list requires a pointer to yourself
class llist
{
llist *next; // a pointer to the same type.
}

How is the character separated from the name ?
Well without more info, as a starter then possibly something like:

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
#include <string>
#include <fstream>
#include <iostream>
#include <list>
#include <algorithm>
#include <cctype>

int main() {
	std::ifstream ifs("list.txt");

	if (!ifs)
		return (std::cout << "File not found\n"), 1;

	char act {};
	std::list<std::string> data;

	do {
		std::string str;

		if ((ifs >> act) && ((act = static_cast<char>(std::tolower(static_cast<unsigned char>(act)))) != 'e') && (act != 'd')) {
			ifs.ignore();
			std::getline(ifs, str);
		}

		switch (act) {
			case 'a':
				std::cout << str;
				if (std::find(data.begin(), data.end(), str) == data.end()) {
					data.push_back(str);
					std::cout << " inserted OK\n";
				} else
					std::cout << " Already exists\n";

				break;

			case 'r':
				std::cout << str;
				if (const auto itr {std::find(data.begin(), data.end(), str)}; itr != data.end()) {
					data.erase(itr);
					std::cout << " removed OK\n";
				} else
					std::cout << " not found\n";
				break;

			case 'f':
				std::cout << str << (std::find(data.begin(), data.end(), str) == data.end() ? " not" : " found") << '\n';
				break;

			case 'd':
				for (const auto& s : data)
					std::cout << s << '\n';
				break;

			case 'e':
				std::cout << "finished\n";
				break;

			default:
				std::cout << act << " - invalid command\n";
				break;
		}
	} while (act != 'e');
}


Given file as:


a qwe rty
a asd fgh
d
a qwe rty
a asd
d
r asd
r asd fgh
r asd
d
e


displays:


qwe rty inserted OK
asd fgh inserted OK
qwe rty
asd fgh
qwe rty Already exists
asd inserted OK
qwe rty
asd fgh
asd
asd removed OK
asd fgh removed OK
asd not found
qwe rty
finished

Last edited on
Topic archived. No new replies allowed.