Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
C-/C++-Quelltext |
|
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 |
#include <iostream> #include <string> #include <fstream> void login(const std::string&, const std::string&); void register_user(const std::string&, const std::string&); int main() { std::cout << "Calculator V0.1 Beta" << std::endl; unsigned short sel = 0; while (sel != 3) { std::cout << "(1) Login" << std::endl; std::cout << "(2) Registrieren" << std::endl; std::cout << "(3) Beenden" << std::endl; do { std::cout << "Eingabe: " << std::flush; } while (!(std::cin >> sel) || sel > 3 || sel < 1); switch (sel) { case 1: { std::cout << ".Login" << std::endl; std::cout << "- Benutzername: " << std::flush; std::string user_name; std::getline(std::cin, user_name); std::cout << "- Passwort: " << std::flush; std::string password; std::getline(std::cin, password); try { login(user_name, password); } catch (std::exception e) { std::cout << e.what() << std::endl; } } break; case 2: { std::cout << ".Registrieren" << std::endl; std::cout << "- Benutzername: " << std::flush; std::string user_name; std::getline(std::cin, user_name); std::string password; std::string password_rev; do { std::cout << "- Passwort: " << std::flush; std::getline(std::cin, password); std::cout << "- Passwort wiederholen: " << std::flush; std::getline(std::cin, password_rev); } while (password != password_rev); register_user(password, user_name); } break; default: break; } } } void login(const std::string& user_name, const std::string& password) { std::ifstream file_stream("user.txt", std::ios::in); if (!file_stream) throw "Could not open file!"; std::string read_name; std::string read_pw; while ((std::getline(file_stream, read_name)) && (std::getline(file_stream, read_pw))) if (read_name == user_name) if (read_pw != password) throw "Invalid password!"; else return; throw "Could not find user!"; } void register_user(const std::string& user_name, const std::string& password) { std::ofstream file_stream("user.txt", std::ios::out); if (!file_stream) return; file_stream << user_name << std::endl; file_stream << password << std::endl; } |
Quellcode |
|
1 2 3 4 |
Passwort: Passwort wiederholen: Passwort: Passwort wiederholen: |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 |
try { login(user_name, password); } catch (std::exception e) { std::cout << e.what() << std::endl; } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 |
try { call( irgendwas ); // wirft eine exception vom typ int } catch ( int i ) // fängt alle ausnahmen vom typ int { // behandeln } catch ( ... ) // fängt alle nicht gefangenen ausnahmen { } |
C-/C++-Quelltext |
|
1 2 3 4 |
try { ... } catch ( std::exception& e ) { ... } |
C-/C++-Quelltext |
|
1 2 |
float foobar( const char* xyz ) throw ( int ) {} |
C-/C++-Quelltext |
|
1 2 |
float foobar(const char* xyz ) throw() {} |
C-/C++-Quelltext |
|
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 |
#include <iostream> #include <string> #include <fstream> void login(const std::string&, const std::string&); void register_user(const std::string&, const std::string&); int main() { std::cout << "Calculator V0.1 Beta" << std::endl; unsigned short sel = 0; while (sel != 3) { std::cout << "(1) Login" << std::endl; std::cout << "(2) Registrieren" << std::endl; std::cout << "(3) Beenden" << std::endl; do { std::cout << "Eingabe: " << std::flush; } while (!(std::cin >> sel) || sel > 3 || sel < 1); std::cin.clear(); std::cin.ignore(std::cin.rdbuf()->in_avail()); switch (sel) { case 1: { std::cout << ".Login" << std::endl; std::cout << "- Benutzername: " << std::flush; std::string user_name; std::getline(std::cin, user_name); std::cout << "- Passwort: " << std::flush; std::string password; std::getline(std::cin, password); try { login(user_name, password); } catch (std::exception& e) { std::cout << e.what() << std::endl; } } break; case 2: { std::cout << ".Registrieren" << std::endl; std::cout << "- Benutzername: " << std::flush; std::string user_name; std::getline(std::cin, user_name); std::string password; std::string password_rev; do { std::cout << "- Passwort: " << std::flush; std::getline(std::cin, password); std::cout << "- Passwort wiederholen: " << std::flush; std::getline(std::cin, password_rev); } while (password != password_rev); register_user(user_name, password); } break; } } } void login(const std::string& user_name, const std::string& password) { std::ifstream file_stream("user.txt", std::ios::in); if (!file_stream) throw std::exception("Could not open file!"); std::string read_name; std::string read_pw; while ((std::getline(file_stream, read_name)) && (std::getline(file_stream, read_pw))) if (read_name == user_name) if (read_pw != password) throw std::exception("Invalid password!"); else return; throw std::exception("Could not find user!"); } void register_user(const std::string& user_name, const std::string& password) { std::ofstream file_stream("user.txt", std::ios::out); if (!file_stream) return; file_stream << user_name << std::endl; file_stream << password << std::endl; } |
C-/C++-Quelltext |
|
1 2 |
std::cin.clear(); std::cin.ignore(std::cin.rdbuf()->in_avail()); |
Zitat von »"David_pb"«
Anmerkung:
Sinniger wäre es Exceptions per Referenz zu fangen, nicht als Kopie:
C-/C++-Quelltext
1 2 3 4 try { ... } catch ( std::exception& e ) { ... }
C-/C++-Quelltext |
|
1 |
catch ( const std::exception& e ) |
C-/C++-Quelltext |
|
1 2 3 4 |
// code ... throw 10; // Wirft eine Ausnahme vom Typ int throw std::string( "Hallo Welt" ); // Wirft eine Ausnahme von Typ std::string throw std::logic_error( "Foobar" ); // Wirft eine Ausnahme vom Typ std::logic_error |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 |
try { throw std::logic_error( "fehler" ); } catch ( const std::logic_error& e ) { std::cout << e.what() << std::endl; } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 |
try { throw xyz(); } catch ( ... ) { } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 |
try { throw std::logic_error( "fooabar" ); } catch ( ... ) // <- fängt alle exceptions { throw; // leitet die exception weiter } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 |
void foo() throw( bar_exception ) // foo wirft evtl eine Exception vom typ bar_exception, aber sonst keine andere { } void foo() // foo kann jeden Ausnahmetyp werfen { } void foo() throw() // foo wirft definitiv niemals eine Ausnahme { } |
Werbeanzeige