Salut, je me suis récemment remis au C++, et pour commencer, rien de mieux que quelques exercices basiques
Je me suis donc attaqué à cet exercice-ci
J'ai fait ça :
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 | #include <iostream> #include <string> #include <vector> #include <fstream> enum class DOOR_STATE { OPENING_MOVING, OPENING_BLOCKED, CLOSING_MOVING, CLOSING_BLOCKED, OPEN, CLOSED}; DOOR_STATE button_clicked(const DOOR_STATE & door) { if(door == DOOR_STATE::OPENING_MOVING) return DOOR_STATE::OPENING_BLOCKED; else if(door == DOOR_STATE::OPENING_BLOCKED) return DOOR_STATE::CLOSING_MOVING; else if(door == DOOR_STATE::CLOSING_MOVING) return DOOR_STATE::CLOSING_BLOCKED; else if(door == DOOR_STATE::CLOSING_BLOCKED) return DOOR_STATE::OPENING_MOVING; else if(door == OPEN) return DOOR_STATE::CLOSING_MOVING; else if(door == DOOR_STATE::CLOSED) return DOOR_STATE::OPENING_MOVING; } DOOR_STATE cycle_completed(const DOOR_STATE & door) { if(door == DOOR_STATE::OPENING_MOVING) return DOOR_STATE::OPEN; else if(door == DOOR_STATE::CLOSING_MOVING) return DOOR_STATE::CLOSED; else if(door == DOOR_STATE::CLOSING_BLOCKED or door == DOOR_STATE::OPENING_BLOCKED or door == DOOR_STATE::OPEN or door == DOOR_STATE::CLOSED) return door; } std::string door_state_tostring(const DOOR_STATE & door) { if(door == DOOR_STATE::OPENING_MOVING) return "OPENING_MOVING"; else if(door == DOOR_STATE::OPENING_BLOCKED) return "OPENING_BLOCKED"; else if(door == DOOR_STATE::CLOSING_MOVING) return "CLOSING_MOVING"; else if(door == DOOR_STATE::CLOSING_BLOCKED) return "CLOSING_BLOCKED"; else if(door == DOOR_STATE::OPEN) return "OPEN"; else if(door == DOOR_STATE::CLOSED) return "CLOSED"; } int main() { DOOR_STATE garage_door { DOOR_STATE::CLOSED }; std::string remote_input_file { "input.txt" }; std::vector<std::string> separated_input; std::ifstream remote_input_stream; if(!remote_input_stream.open(remote_input_file.c_str())) std::cout << "ERROR : Unable to open the file " << remote_input_file << "." << std::endl; else { std::string line { " " }; while(getline(remote_input_stream, line)) { separated_input.push_back(line); } } for(std::string command : separated_input) { std::cout << "Door: " << door_state_tostring(garage_door) << std::endl; if(command == "button_clicked") { std::cout << " > button_clicked" << std::endl; garage_door = button_clicked(garage_door); } else if(command == "cycle_completed") { std::cout << " > cycle_completed" << std::endl; garage_door = cycle_completed(garage_door); } else std::cout << "ERROR : Unknown command " << command << std::endl; } return 0; } |
Mais le compilateur me dit qu'il ne connaît pas mon énumération :
"main.cpp|15|error: ‘DOOR_STATE’ is not a class or namespace|"
Je ne me souviens plus vraiment de comment on les utilise, où est mon erreur s'il vous plaît?
+0
-0