<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[args[1] prüfen]]></title><description><![CDATA[<p>Hallo zusammen,</p>
<p>ich habe einen kleinen Passwortgenerator geschrieben und habe nun ein Problem mit den Argumenten, die man main() übergeben kann.</p>
<p>Wie man im Code sehen kann, möchte ich einen Hilfetext ausgeben, wenn der Benutzer --help oder -h als Argument angibt.</p>
<p>Allerdings klappt das nicht. Statt dem erwünschten Hilfetext bekomme ich nur eine leere Zeile und danach beendet sich das Programm.</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;string&gt;

#define DEFAULT_LENGTH 8

using namespace std;

void help();

void help() {
	cout &lt;&lt; &quot;hilfetext&quot; &lt;&lt; endl;
}

int main(int argc, char* args[]) {
	string chars = &quot;ABCDEFGHIJKLMNOPQRSTUVWXYZ&quot;;
	chars += &quot;abcdefghijklmnopqrstuvwxyz&quot;;
	chars += &quot;0123456789_#&quot;;
	string password;	// used to store the password
	int index;	// used to iterate through 'string chars'
	int password_length;	// the length of the password

	srand(time(NULL));

	if(argc &lt; 2) {
		// set all settings to default
		password_length = DEFAULT_LENGTH;
	}
	else if(argc &gt;= 2) {
		if(args[1]==&quot;--help&quot;||args[1]==&quot;-h&quot;) {
			help();
			return 0;
		}		
		else {
			password_length = atoi(args[1]);
		}
	}

	for(int j=0; j&lt;password_length; ++j) {
		index = rand() % chars.length();
		password += chars[index];
	}

	cout &lt;&lt; password &lt;&lt; endl;

	return 0;
}
</code></pre>
<p>Wenn ich ausgeben lasse, welchen Wert args[1] hat, wird korrekterweise &quot;--help&quot; oder &quot;-h&quot; angezeigt (je nach dem, was ich übergebe beim Programmstart). Hat jemand von euch eine Idee warum das nicht funktioniert? Vielleicht sehe ich auch nur den Wald vor lauter Bäumen nicht.</p>
<p>Bin dankbar für jeden Tipp <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/topic/222386/args-1-prüfen</link><generator>RSS for Node</generator><lastBuildDate>Mon, 05 Oct 2026 03:09:35 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/222386.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 11 Sep 2008 20:45:38 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to args[1] prüfen on Thu, 11 Sep 2008 20:45:38 GMT]]></title><description><![CDATA[<p>Hallo zusammen,</p>
<p>ich habe einen kleinen Passwortgenerator geschrieben und habe nun ein Problem mit den Argumenten, die man main() übergeben kann.</p>
<p>Wie man im Code sehen kann, möchte ich einen Hilfetext ausgeben, wenn der Benutzer --help oder -h als Argument angibt.</p>
<p>Allerdings klappt das nicht. Statt dem erwünschten Hilfetext bekomme ich nur eine leere Zeile und danach beendet sich das Programm.</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;string&gt;

#define DEFAULT_LENGTH 8

using namespace std;

void help();

void help() {
	cout &lt;&lt; &quot;hilfetext&quot; &lt;&lt; endl;
}

int main(int argc, char* args[]) {
	string chars = &quot;ABCDEFGHIJKLMNOPQRSTUVWXYZ&quot;;
	chars += &quot;abcdefghijklmnopqrstuvwxyz&quot;;
	chars += &quot;0123456789_#&quot;;
	string password;	// used to store the password
	int index;	// used to iterate through 'string chars'
	int password_length;	// the length of the password

	srand(time(NULL));

	if(argc &lt; 2) {
		// set all settings to default
		password_length = DEFAULT_LENGTH;
	}
	else if(argc &gt;= 2) {
		if(args[1]==&quot;--help&quot;||args[1]==&quot;-h&quot;) {
			help();
			return 0;
		}		
		else {
			password_length = atoi(args[1]);
		}
	}

	for(int j=0; j&lt;password_length; ++j) {
		index = rand() % chars.length();
		password += chars[index];
	}

	cout &lt;&lt; password &lt;&lt; endl;

	return 0;
}
</code></pre>
<p>Wenn ich ausgeben lasse, welchen Wert args[1] hat, wird korrekterweise &quot;--help&quot; oder &quot;-h&quot; angezeigt (je nach dem, was ich übergebe beim Programmstart). Hat jemand von euch eine Idee warum das nicht funktioniert? Vielleicht sehe ich auch nur den Wald vor lauter Bäumen nicht.</p>
<p>Bin dankbar für jeden Tipp <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1580505</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1580505</guid><dc:creator><![CDATA[Klip]]></dc:creator><pubDate>Thu, 11 Sep 2008 20:45:38 GMT</pubDate></item><item><title><![CDATA[Reply to args[1] prüfen on Thu, 11 Sep 2008 22:51:58 GMT]]></title><description><![CDATA[<p>Ich kommentiere mal deinen Code.</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;sstream&gt; // wir brauchen stringstream für später

#define DEFAULT_LENGTH 8 // GAAANZ übel, man sollte nicht Makros für Konstante verwenden.
// Verwende lieber C++ Mittel:
int const DEFAULT_LENGTH = 8;

// Präprozessor ist nur ein dummer Textersetzer. Damit macht man Fehler, ohne das man es bemerkt!
// Hände weg vom Präprozessor, wo es nur geht!

// using namespaces mit Vorsicht geniessen! Ja nicht in einen Header damit!
using namespace std;

// Unnötig:
//void help();

// Deklaration und Definition kann auch gleichzeitig stattfinden!
void help() {
	cout &lt;&lt; &quot;hilfetext&quot; &lt;&lt; endl;
}

int main(int argc, char* args[]) {

	// Man kann String literale über mehrere Zeilen zusammenhängen.
	string chars = &quot;ABCDEFGHIJKLMNOPQRSTUVWXYZ&quot;//;
	/*chars += */&quot;abcdefghijklmnopqrstuvwxyz&quot;//;
	/*chars += */&quot;0123456789_#&quot;;
	string password;	// used to store the password
	int index;	// used to iterate through 'string chars'
	int password_length;	// the length of the password

	// Ich empfehle die Verwendung von 0 statt NULL. Oder nullptr. Siehe dazu auch die FAQ.
	// srand(time(NULL));
	srand(time(0));

	if(argc &lt; 2) {
		// set all settings to default
		password_length = DEFAULT_LENGTH;
	}
	else if(argc &gt;= 2) {
		// if(args[1]==&quot;--help&quot;||args[1]==&quot;-h&quot;) { // Du vergleichst die Zeiger und nicht den Inhalt!
		// Korrekt wäre z.B. sowas:
		string arg1 = args[1];

		if(arg1.compare(&quot;--help&quot;) == 0 ||
		   arg1.compare(&quot;-h&quot;) == 0) {
			help();
			return 0;
		}		
		else {
			// password_length = atoi(args[1]);
			// atoi ist C, in C++ benutzt man stringstreams.
			stringstream ss(args[1]);

			if(!(ss &gt;&gt; password_length))
			{ /* Fehler! */ }
		}
	}

	for(int j=0; j&lt;password_length; ++j) {
		index = rand() % chars.length();
		password += chars[index];
	}

	cout &lt;&lt; password &lt;&lt; endl;

	return 0;
}
</code></pre>
<p>Ist denke ich das Gröbste und wenn du anstatt den Zeiger den Inhalt der Strings vergleichst, sollte auch das mit dem &quot;help&quot; gehen.</p>
<p>Grüssli</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1580538</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1580538</guid><dc:creator><![CDATA[Dravere]]></dc:creator><pubDate>Thu, 11 Sep 2008 22:51:58 GMT</pubDate></item><item><title><![CDATA[Reply to args[1] prüfen on Fri, 12 Sep 2008 06:36:53 GMT]]></title><description><![CDATA[<p>Hi,</p>
<p>nur noch eine Kleinigkeit als Ergänzung: Wer (wie ich) lieber mit strings als mit char* arbeitet (u.A. um sie direkt vergleichen zu können), kann das mit einer einzigen Zeile hinkriegen:</p>
<pre><code class="language-cpp">int main(int argc, char* argv[]) { // &lt;-- übrigens: wird üblichweise argv genannt und nicht args
   vector&lt;string&gt; args(argv, argv+argc);
...
</code></pre>
<p>Weiterer Vorteil IMHO: Da ich auf Kopien arbeite, kann ich beliebig Argumente ändern und sogar welche hinzufügen/löschen ...</p>
<p>Gruß,</p>
<p>Simon2.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1580624</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1580624</guid><dc:creator><![CDATA[Simon2]]></dc:creator><pubDate>Fri, 12 Sep 2008 06:36:53 GMT</pubDate></item><item><title><![CDATA[Reply to args[1] prüfen on Fri, 12 Sep 2008 14:08:48 GMT]]></title><description><![CDATA[<p>Oh, vielen Dank für die Tipps!</p>
<p>Wieder eine Menge gelernt <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
<p>stringstreams kannte ich noch gar nicht. Klingt gut <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /></p>
<p>EDIT: Habe bei mir jetzt auch die &quot;using namespace&quot;-Anweisung rausgenommen. Klappt alles wunderbar, vielen Dank!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1580775</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1580775</guid><dc:creator><![CDATA[Klip]]></dc:creator><pubDate>Fri, 12 Sep 2008 14:08:48 GMT</pubDate></item></channel></rss>