Funktion langsam
-
Huhu
also ich hab mich mal mit C++ versucht und finds langweilig dass texte blitzschnell erscheinen.
also hab ich mir gedacht dass ich das programm zwischen jedem einzelnen buchstaben mit einer schleife beschäftige um so das ausgaben tempo zu senken.
leider ist das eine ziemliche arbeit und ein freund meinte dass ich es mal mit einer funktion versuchen sollte doch leider fällt mir kein weg ein wie ich das machen soll
also so mache ich es normal:
cout<<" W"; for(int i=0;i<=50000000;i+=1){int x=0;} cout<<"i"; for(int i=0;i<=50000000;i+=1){int x=0;} cout<<"l"; for(int i=0;i<=50000000;i+=1){int x=0;} cout<<"l"; for(int i=0;i<=50000000;i+=1){int x=0;} cout<<"k"; for(int i=0;i<=50000000;i+=1){int x=0;} cout<<"o"; for(int i=0;i<=50000000;i+=1){int x=0;} cout<<"m"; for(int i=0;i<=50000000;i+=1){int x=0;} cout<<"m"; for(int i=0;i<=50000000;i+=1){int x=0;} cout<<"e"; for(int i=0;i<=50000000;i+=1){int x=0;} cout<<"n"; for(int i=0;i<=50000000;i+=1){int x=0;} cout<<" "; for(int i=0;i<=50000000;i+=1){int x=0;} cout<<"i"; for(int i=0;i<=50000000;i+=1){int x=0;} cout<<"n"; for(int i=0;i<=50000000;i+=1){int x=0;} cout<<" "; for(int i=0;i<=50000000;i+=1){int x=0;} cout<<"m"; for(int i=0;i<=50000000;i+=1){int x=0;} cout<<"e"; for(int i=0;i<=50000000;i+=1){int x=0;} cout<<"i"; for(int i=0;i<=50000000;i+=1){int x=0;} cout<<"n"; for(int i=0;i<=50000000;i+=1){int x=0;} cout<<"e"; for(int i=0;i<=50000000;i+=1){int x=0;} cout<<"m"; for(int i=0;i<=50000000;i+=1){int x=0;} cout<<" "; for(int i=0;i<=50000000;i+=1){int x=0;} cout<<"S"; for(int i=0;i<=50000000;i+=1){int x=0;} cout<<"p"; for(int i=0;i<=50000000;i+=1){int x=0;} cout<<"i"; for(int i=0;i<=50000000;i+=1){int x=0;} cout<<"e"; for(int i=0;i<=50000000;i+=1){int x=0;} cout<<"l"; for(int i=0;i<=50000000;i+=1){int x=0;} cout<<"!"<<endl;
das ganze würde ich jetzt aber gerne als funktion haben sodass ich nur noch den string übergeben muss
hoffe mir kann einer helfen
-
Also die Schleife sieht katastrophal aus, schau dich doch am besten nach sleep() (UNIX) oder Sleep() (WinAPI) um, die sind da eleganter und vermutlich auch präziser.
Was dein eigentliches Problem angeht - versuch' doch mal den String in einer Schleife auszugeben:
void slow_out(ostream& out,const string& text,int delay=1/*Wartzeit in Sekunden*/) { for(int i=0;i<text.length();++i) { out<<text[i]; sleep(delay); } }
-
void foo(string DeinWert) { for(int i = 0; i<DeinWert.Length()) { cout<<DeinWert[i]; Sleep(50); } }
So in etwa, hab grad keinen Compiler bei der Hand, aber die Richtung stimmt. Musst noch windows.h fürs Sleep einbinden.
-
Zu langsam, sry.
-
Bonzy schrieb:
also hab ich mir gedacht dass ich das programm zwischen jedem einzelnen buchstaben mit einer schleife beschäftige um so das ausgaben tempo zu senken. .. und ein freund meinte dass ich es mal mit einer funktion versuchen sollte
Hallo Bonzy,
noch eleganter und auf jede beliebige Ausgabe anwendbar ist es, wenn man die Verzögerung direkt in den Ausgabe-Stream einbaut. Das ganze habe ich hier mal als Schnecke bezeichnet.
#include <iostream> #include <streambuf> #include <windows.h> // Sleep class Schnecke : public std::streambuf { public: typedef std::streambuf base_type; typedef base_type::int_type int_type; typedef base_type::traits_type traits_type; Schnecke( std::ostream& out, double secPerChar ) : base_type() , m_secPerChar( secPerChar ) , m_target( out.rdbuf() ) , m_out( out ) { m_out.rdbuf( this ); } ~Schnecke() { m_out.rdbuf( m_target ); } protected: virtual int_type overflow( int_type c = traits_type::eof() ) { if( traits_type::eq_int_type( c, traits_type::eof() ) ) return traits_type::not_eof( c ); int_type ret = m_target->sputc( traits_type::to_char_type( c ) ); // -- hier ggf. die OS-abhängige Warte-Funktion benutzen Sleep( int( m_secPerChar * 1000 ) ); return ret; } private: Schnecke( const Schnecke& ); Schnecke& operator=( const Schnecke& ); // -- Members double m_secPerChar; // Verzögerung in Sekunden pro Zeichen std::streambuf* m_target; std::ostream& m_out; }; int main() { using namespace std; { Schnecke schnecke( cout, 0.1 ); // 0.1s Verzögerung pro Zeichen cout << "Willkommen zu meinem Spiel .. " << endl; for( double x = 1.0; x < 7.0; x += 2.0 ) { cout << x << " geteilt durch 7 = " << x/7.0 << endl; } } cout << ".. und hier ist wieder alles ganz normal" << endl; return 0; }
.. ist wirklich lustig, die Zeichen einzeln am Bildschirm erscheinen zu sehen. Vielleicht könnte man noch einen Ton einbauen, so ein Anschlag wie bei einer Schreibmaschine
Gruß
WernerPS.: die Input/Output Library ist die meist unterschätzte des C++-Standards