Ich finde die Fehler nicht.



  • Hier ich weis nicht was ich falsch gemacht hab es soll solange der String fertigesPasswort nicht die länge über 20 hat aufhört die buchstaben hinzuzufügen.

    #include <iostream>
    #include <string>
    #include <time.h>
    
    int main() {
    
    	std::string fertigesPasswort = "";
    
    	while (fertigesPasswort.length < 20) {
    
    		srand(time(NULL));
    		int randome = rand() % 24 + 0;
    
    		if (randome == 1) {
    			std::string fertigesPasswort = fertigesPasswort + "a";
    		}
    		else if (randome == 2)
    
    			std::string fertigesPasswort = fertigesPasswort + "b";
    
    		else if (randome == 3) {
    			std::string fertigesPasswort = fertigesPasswort + "c";
    		}
    		else if (randome == 4) {
    			std::string fertigesPasswort = fertigesPasswort + "d";
    
    		}
    		else if (randome == 5) {
    			std::string fertigesPasswort = fertigesPasswort + "e";
    		}
    
    		else if (randome == 6) {
    			std::string fertigesPasswort = fertigesPasswort + "f";
    		}
    		else if (randome == 7) {
    			std::string fertigesPasswort = fertigesPasswort + "g";
    		}
    		else if (randome == 8) {
    			std::string fertigesPasswort = fertigesPasswort + "h";
    		}
    		else if (randome == 9) {
    			std::string fertigesPasswort = fertigesPasswort + "i";
    		}
    		else if (randome == 10) {
    			std::string fertigesPasswort = fertigesPasswort + "bj";
    		}
    		else if (randome == 11) {
    			std::string fertigesPasswort = fertigesPasswort + "k";
    		}
    
    		else if (randome == 12) {
    			std::string fertigesPasswort = fertigesPasswort + "l";
    		}
    		else if (randome == 13) {
    			std::string fertigesPasswort = fertigesPasswort + "m";
    		}
    		else if (randome == 14) {
    			std::string fertigesPasswort = fertigesPasswort + "n";
    		}
    		else if (randome == 15) {
    			std::string fertigesPasswort = fertigesPasswort + "o";
    		}
    		else if (randome == 16) {
    			std::string fertigesPasswort = fertigesPasswort + "p";
    		}
    		else if (randome == 17) {
    			std::string fertigesPasswort = fertigesPasswort + "q";
    		}
    		else if (randome == 18) {
    			std::string fertigesPasswort = fertigesPasswort + "r";
    		}
    		else if (randome == 19) {
    			std::string fertigesPasswort = fertigesPasswort + "s";
    		}
    		else if (randome == 20) {
    			std::string fertigesPasswort = fertigesPasswort + "t";
    		}
    		else if (randome == 21) {
    			std::string fertigesPasswort = fertigesPasswort + "u";
    		}
    		else if (randome == 22) {
    			std::string fertigesPasswort = fertigesPasswort + "v";
    		}
    		else if (randome == 23) {
    			std::string fertigesPasswort = fertigesPasswort + "w";
    		}
    		else if (randome == 24) {
    			std::string fertigesPasswort = fertigesPasswort + "x";
    		}
    		else if (randome == 25) {
    			std::string fertigesPasswort = fertigesPasswort + "y";
    		}
    		else if (randome == 26) {
    			std::string fertigesPasswort = fertigesPasswort + "z";
    
    		}
    		std::cout << fertigesPasswort << std::endl;
    
    		system("pause");
    		return 0;
    
    	}
    

  • Mod

    Du veränderst nie das Original, sondern erzeugst jedes Mal einen neuen String mit Namen fertigesPasswort, der bis zur nächsten } lebt. In diesem Fall also so ungefähr bis zur nächsten Zeile.

    Übrigens:

    string alphabet = "abcdefghijklmnopqrstuvwxyz";
    alphabet[0]; // 'a'
    

    Damit wird dein Code ein kleines bisschen kürzer, einfacher, und erweiterbar.

    PS: Der momentane Wertebereich deiner Zufallszahl ist übrigens 0 bis 23.



  • Danke für die schnelle Antwort



  • SeppJ schrieb:

    Du veränderst nie das Original, sondern erzeugst jedes Mal einen neuen String mit Namen fertigesPasswort, der bis zur nächsten } lebt. In diesem Fall also so ungefähr bis zur nächsten Zeile.

    Übrigens:

    string alphabet = "abcdefghijklmnopqrstuvwxyz";
    alphabet[0]; // 'a'
    

    Damit wird dein Code ein kleines bisschen kürzer, einfacher, und erweiterbar.

    PS: Der momentane Wertebereich deiner Zufallszahl ist übrigens 0 bis 23.

    Das komische ist das die Randome zahl immer die gleiche Zahl generiert was jetzt?

    #include <iostream>
    #include <string>
    #include <time.h>
    
    int main() {
    
    	std::string alphabet = "abcdefghijklmnopqrstuvwxyz";
    	std::string fertigesPasswort = "";
    
    	int i = 0;
    
    	while ( i < 20) {
    
    		i++;
    		srand(time(NULL));
    		int randome = rand() % 25 + 0;
    
    		fertigesPasswort = alphabet[randome] + fertigesPasswort;
    
    		std::cout << randome << std::endl;
    
    		}
    		std::cout << fertigesPasswort << std::endl;
    
    		system("pause");
    		return 0;
    
    	}
    

    Ausgabe:

    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    cccccccccccccccccccc
    Drücken Sie eine beliebige Taste . . .
    


  • srand(time(NULL)); //
        while ( i < 20) { 
    
            i++; 
    //        srand(time(NULL)); 
            int randome = rand() % 25 + 0;
    

Anmelden zum Antworten