Passwortabfrage
-
Falls es den OP noch interessiert...
Im übrigen ist den Code eine grausames mischmasch aus C und C++. Wenn man schon in C++ Programmiert, dann bitte auch durchgängig:
#include <vector> #include <string> #include <fstream> #include <iostream> using namespace std; string get_logon_type_info( bool admin_mode ); int main () { const string codes[ ] = { "enter", "earth", "air", "fire", "water" }; const string ordinal[ ] = { "first", "second", "third", "fourth", }; const size_t admin_code_index = 0; const size_t first_user_code = 1; const size_t num_codes = sizeof( codes ) / sizeof( string ); bool admin_mode = false; size_t current_code = first_user_code; string code; do { cout << "Please enter the " << ordinal[ current_code - 1 ] << " code: "; getline( cin, code ); if( code == codes[ admin_code_index ] ) { admin_mode = true; } } while( ( code == codes[ current_code ] ) && ( ++current_code < num_codes ) ); if( !admin_mode && ( current_code != num_codes ) ) { cout << "You failed at the " << ordinal[ current_code - 1 ] << " code!" << endl; cout << "aborting..." << endl; return EXIT_FAILURE; } cout << endl << "You are " << get_logon_type_info( admin_mode ) << "." << endl << endl; cout << "Please enter some text (to end the input mode, type the number" << endl; cout << "Zero at a single line):" << endl; vector< string > input; string current_input; getline( cin, current_input ); while( current_input != "0" ) { input.push_back( current_input ); getline( cin, current_input ); } if( !input.size( ) ) { cout << "There is no text to save!" << endl; return EXIT_FAILURE; } string file_name = ""; fstream file; const string different = "different "; bool different_filename = false; bool overwrite = false; while( file_name.empty( ) || different_filename ) { cout << endl << "Please enter a "; if( different_filename ) { cout << different; } cout << "filename to save your text to (simply press enter to abort):" << endl; getline( cin, file_name ); different_filename = false; if( file_name.empty( ) ) { cout << "aborting..." << endl; return EXIT_FAILURE; } file.open( file_name.c_str( ), ios_base::in ); if( file.is_open( ) ) { file.close( ); cout << endl << "The File \"" << file_name << "\" does already exist!" << endl; cout << "Do you want to overwrite it? (y/n)" << endl; char ch = 1; do { if( !ch ) { cout << "Wrong input!" << endl << endl; } cin >> ch; if( cin.fail( ) ) { cin.clear( ); cin.ignore( cin.rdbuf( )->in_avail( ) ); ch = 0; } else if( ( toupper( ch ) != 'Y' ) && ( toupper( ch ) != 'N' ) ) { ch = 0; } } while( !ch ); cin.clear( ); cin.ignore( cin.rdbuf( )->in_avail( ) ); if( toupper( ch ) == 'N' ) { different_filename = true; } else { different_filename = false; overwrite = true; } } if( !different_filename ) { ios_base::open_mode open_mode = ios_base::out; open_mode = ( overwrite ) ? ( open_mode | ios_base::trunc ) : ( open_mode ); file.open( file_name.c_str( ), open_mode ); if( !file.is_open( ) ) { cout << "The file " << file_name << " could not be created!" << endl; different_filename = true; } } } cout << endl << "\"" << file_name << "\" has been opened for writing." << endl; for( vector< string >::iterator i = input.begin( ); i != input.end( ); ++i ) { file.write( ( *i ).c_str( ), ( *i ).length( ) ); file.put( '\n' ); } cout << "The text has been written to \"" << file_name << "\"." << endl; file.close( ); } string get_logon_type_info( bool admin_mode ) { string logon_type_info = "logged in as a"; logon_type_info += ( admin_mode ) ? "n administrator" : " user"; return logon_type_info; }Greetz, Swordfish
-
Hallo,
Man kann auch alles in Klassen kapseln. Das ist dann auch C++ :).
MFG winexec*
-
@Swordfish : Ich habe die ios_base::open_mode in ios_base::openmode umgeändert, sonst gehts nicht ;).
MfG, C++Progger