Dear C++ heroes,
Today I seek your help with my program for university, first of all here is the assignment:
----------------------------------------------------------------
The university decided to make a new program to keep track of which employees are in which room. This digital address book can be managed from the command line.
A room number always consists of a letter followed by 3 digits. If multiple employees are in the same room, the output of list should be sorted by room number first, and then sorted by name (alphabetically). Implement this program. The following restrictions apply:
You are not allowed to use any class that you have not programmed yourself (including std::vector), except for std::string, std::cin, std::cout, and std::cerr.
You are not allowed to create an array or a pointer to a part of memory that is larger than the amount of entries you need to store.
A correct execution of this program is shown below:
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
|
> remove Thilo
error: entry does not exist
> add Thilo P322
> add Matty F153
> list
Matty is in room F153
Thilo is in room P322
> move Matty T427
> list
Thilo is in room P322
Matty is in room T427
> find Thilo
Thilo is in room P322
> move Thilo T427
> list
Matty is in room T427
Thilo is in room T427
> clear
> list
> add Jorg T437
> add Femke T447
> remove Jorg
> list
Femke is in room T447
> exit
|
------------------------------------------------------------
So I managed to do everything but I still struggle against two parts:
- sorting in my list function
- error handling, checking whether or not a name is not in the list when trying to remove.
so here is my code:
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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
|
#include <iostream>
struct Combo{
std::string teacher;
std::string roomNumber;
};
class Data{
private:
int size;
Combo *elements;
public:
Data(): size{0}, elements{new Combo [0]}{
}
~Data(){
delete []elements;
}
Combo at(int index){
return elements[index];
}
void push_back(Combo value){
Combo *save = new Combo[size + 1];
if(size == 0){
save[0] = value;
}
if(size > 0 ){
for(int i = 0 ; i < size ; i++){
save[i] = elements[i];
if(i == (size - 1)){
save[i + 1] = value;
}
}
}
delete[] elements;
elements = save;
size ++;
}
void list(){ // So I struggle with this function.
bool sort = false; // I try to use bubble-sort, but failed.
while(sort == false){
Combo *save = new Combo[size];
for(int i = 0; i < size - 1; i++){
bool sortCheck = false;
if(elements[i].roomNumber > elements[i + 1].roomNumber){
save[i] = elements[i + 1];
elements[i] = save[i + 1];
sortCheck = true;
}
else if(elements[i].roomNumber < elements[i + 1].roomNumber){
save[i] = elements[i];
save[i + 1] = elements[i + 1];
sortCheck = true;
}
else if(sortCheck == false){
sort = true;
}
}
}
}
std::string return_teacher(int index){
return elements[index].teacher;
}
std::string return_roomNumber(int index){
return elements[index].roomNumber;
}
void delete_teacher(std::string teacher){ // and with this one.
Combo *save = new Combo[(size - 1)];
bool nextElement = false;
int teacherCheck = 0;
for(int i = 0 ; i < (size - 1) ; i++){
teacherCheck ++;
if (teacher == elements[i].teacher || nextElement == true){
nextElement = true;
save[i] = elements[i+1];
}
else if(teacher != elements[i].teacher && nextElement == false){
save[i] = elements[i];
}
else if(teacherCheck == (size - 1)){
throw std::runtime_error ("entry does not exist");
} // If every element has been checked, throw error (what I try).
}
delete[] elements;
elements = save;
size --;
}
void move(std::string teacher, std::string roomNumber){
Combo *save = new Combo[size];
for(int i = 0 ; i < size ; i++){
save[i] = elements[i];
if(teacher == elements[i].teacher){
save[i].roomNumber = roomNumber;
}
}
delete[] elements;
elements = save;
}
void delete_elements(){
Combo *save = new Combo[0];
delete[] elements;
elements = save;
size = 0;
}
int length(){
return size;
}
};
int main (){
Combo objectCombo;
Data vector;
std::string choice, teacher, roomNumber;
while(std::cin){
try{
std::cout << "> ";
std::cin >> choice;
if(choice == "add"){
std::cin >> teacher;
std::cin >> roomNumber;
if(!isalpha(roomNumber[0]) || !isdigit(roomNumber[1,2,3]) || roomNumber.length() != 4){
throw std::runtime_error("Invalid roomnumber");
}
objectCombo.teacher = teacher;
objectCombo.roomNumber = roomNumber;
vector.push_back(objectCombo);
}
else if(choice == "remove"){
std::cin >> teacher;
vector.delete_teacher(teacher);
}
else if(choice == "find"){
std::cin >> teacher;
for(int i = 0 ; i < vector.length() ; i++){
if(teacher == vector.return_teacher(i)){
std::cout << vector.return_teacher(i) << " is in room " << vector.return_roomNumber(i) << std::endl;
}
}
}
else if(choice == "move"){
std::cin >> teacher;
std::cin >> roomNumber;
vector.move(teacher, roomNumber);
}
else if(choice == "list"){
for(int i = 0 ; i < vector.length() ; i++){
vector.list();
std::cout << vector.return_teacher(i) << " is in room " << vector.return_roomNumber(i) << std::endl;
}
}
else if(choice == "exit"){
return 1;
}
else if(choice == "clear"){
vector.delete_elements();
}
}
catch(std::runtime_error e){
std::cerr << "Error: " << e.what() << std::endl;
}
}
return 0;
}
|
1 month ago I started programming, I already struggled with writing a hello-world program in this programming language haha. the deadline for my assignment is 23/10/2017 23:59. It kind of decides whether or not I pass this course, so a lot of pressure is on me.
I hope anyone can see what I did wrong in this case and might help a brother out! I hope to do the same thing for this community once I get more advanced.
Cheers!