incrementing strings



  • Hi, ich hab eine kleine Funktion geschrieben, die einen String erhöhen soll. Leider stürzt das Prgramm immer ab, wenn es zum beispiel zz erhöhen soll. Eigentlich füge ich dafür extre ein char am anfang hinzu. Wär nett wenn sich das jemand mal anschauen könnte. Vielleicht seh ich ja nur den Wald vor lauter Bäumen nicht 😞

    using namespace std;
    	unsigned short back = 1; //wievielte Nummer von hinten
    	string firstPart, lastPart;
    	char element;
    	bool debug = true;
    
    	while(1)
    	{
    		firstPart 	= pwd.substr(0,pwd.size()-back);
    		element = (char)pwd[pwd.size()-back];
    		lastPart	= pwd.substr(back+1,
    		                      pwd.size()-firstPart.size()-1);
    
    		element++;
    
    		if( isdigit(element) or isalpha(element) )
    		{
    			if( debug )
    			{
    				cout<<"INCREASING"<<endl;
    				cout<<"back: "<<back<<endl;
    			}
    
    			pwd = firstPart+element+lastPart;
    			if( debug )
    			{
    				cout<<"pwd: "<<pwd<<endl;
    			}
    			return pwd;
    		}
    		else if(element >= 123)
    		{
    			if( debug )
    			{
    				cout<<"CHANGING BACK"<<endl;
    			}
    
    			element = '0';
    
    			pwd = firstPart+element+lastPart;
    			if( debug )
    			{
    				cout<<"pwd: "<<pwd<<endl;
    				cout<<"back: "<<back<<endl;
    				cout<<"pwd.size(): "<<pwd.size()<<endl;
    			}
    			if( back == pwd.size() )
    			{
    				pwd = string("/")+pwd;
    				cout<<pwd<<endl;
    			}
    			back++;
    		}
    		else
    		{
    			element++;
    		}
    	}
    	return pwd;
    

    Danke 🙂



  • Wo ist hier ein zz und was ist pwd?



  • Da sehe ich schonmal das Problem, daß Buchstaben und Zahlen nicht dicht hintereinander in der ASCII-Tabelle stehen (nach der 9 kommt ':' - den Bereich müsstest du evt. "überspringen").

    (übrigens kannst du statt dem "firstpart+element+lastpart"-Gebastel auch direkt im String schreiben)



  • @Braunstein
    sry, hab ich doch glatt vergessen zu sagen. Dies ist eine Methode einer Klasse, pwd ist ein string, welcher erhöht werden soll. Das Programm schlägt fehl, wenn pwd == "zz"

    @CStoll
    überspring ich doch:

    if(isdigit(element) or isalpha(element) { ... }
    else { element++; }
    

    Hab es jetzt aber folgendermaßen abgewandelt. Das Problem bleibt das gleich:

    using namespace std;
    	unsigned short back = 1; //wievielte Nummer von hinten
    	char element = ' ';
    	bool debug = true;
    
    	while(1)
    	{
    		element = pwd[pwd.size() - back];
    
    		if( isdigit(element) or isalpha(element) )
    		{
    			if( debug )
    			{
    				cout<<"INCREASING"<<endl;
    				cout<<"back: "<<back<<endl;
    			}
    
    			pwd[pwd.size() - back] = element;
    			if( debug )
    			{
    				cout<<"pwd: "<<pwd<<endl;
    			}
    			return pwd;
    		}
    		else if(element >= 123)
    		{
    			if( debug )
    			{
    				cout<<"CHANGING BACK"<<endl;
    			}
    
    			element = '0';
    
    			pwd[pwd.size() - back] = element;
    			if( debug )
    			{
    				cout<<"pwd: "<<pwd<<endl;
    				cout<<"back: "<<back<<endl;
    				cout<<"pwd.size(): "<<pwd.size()<<endl;
    			}
    			if( back == pwd.size() )
    			{
    				pwd = string("/")+pwd;
    				cout<<pwd<<endl;
    			}
    			back++;
    		}
    		else
    		{
    			element++;
    		}
    	}
    	return pwd;
    


  • Bei mir stürzt hier nichts ab, wenn pwd = "zz" ist. Genau gesagt geht er gleich beim ersten return raus.
    Der Code ist aber auch etwas merkwürdig.
    Was soll das element++ da am Ende? Du überschreibst das doch sowieso gleich wieder.
    Was soll die Zuweisung im ersten if? An der Stelle steht doch schon element.
    Kannst du mal genau erklären was du machen willst?



  • Ok, ich habs. Thx. Sollte folgendes machen:

    using namespace std;
    	unsigned short back = 1; //wievielte Nummer von hinten
    	char element = ' ';
    	bool debug = true;
    
    	while(1)
    	{
    		//get the last char that can be changed (in the first round
    		//the last char, if this is z, get the char before it
    		element = pwd[pwd.size() - back];
    		//and increase it
    		element++;
    
    		//if the char is a number or a letter, try to increase it
    		if( isdigit(element) or isalpha(element) )
    		{
    			if( debug )
    			{
    				cout<<"INCREASING"<<endl;
    				cout<<"back: "<<back<<endl;
    			}
    			//if it was posible to increase the char, write it in
    			//the string
    			pwd[pwd.size() - back] = element;
    
    			if( debug )
    			{
    				cout<<"pwd: "<<pwd<<endl;
    			}
    			return pwd;
    		}
    		//if the char is not a valid number or letter anymore,
    		//reset the char and increase back to look in the next
    		//round on the char before this one
    		else if(element >= 123)
    		{
    			if( debug )
    			{
    				cout<<"CHANGING BACK"<<endl;
    			}
    
    			element = '0';
    
    			pwd[pwd.size() - back] = element;
    
    			if( debug )
    			{
    				cout<<"pwd: "<<pwd<<endl;
    				cout<<"back: "<<back<<endl;
    				cout<<"pwd.size(): "<<pwd.size()<<endl;
    			}
    
    			//if this char was already the first one, put an
    			//aditional one in front of it
    			if( back == pwd.size() )
    			{
    				pwd = string("/")+pwd;
    				cout<<pwd<<endl;
    			}
    
    			back++;
    		}
    		//if the char is a char bettween the valid letters and
    		//numbers, just go in the next round. Maybe there
    		//it will be one
    	}
    
    	return pwd;
    }
    

Log in to reply