<?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[for Schleife mit if, else if und else]]></title><description><![CDATA[<p>hi,<br />
ich schau mir gerade eine Methode eines Konstruktors an und eigentlich sollte nach der Klassenbeschreibung folgendes geschehen:</p>
<pre><code>Class argList
Sequences with &quot;(&quot; ... &quot;)&quot; are transformed into a stringList. For example,
    program -listFiles \( *.txt \)
would create a stringList:
    ( &quot;file1.txt&quot; &quot;file2.txt&quot; ... &quot;fileN.txt&quot; )
The backslash-escaping is required to avoid interpretation by the shell.
</code></pre>
<p>Wenn ich nun aber in den Konstruktor gehe und mir die Methode herausnehme welche oben genannte Beschreibung durchfuehren soll wie ich denke, ist das Ergebnis bei Annahme eines erdachten Terminalbefehls (siehe unter A) bei mir ein anderes:</p>
<pre><code>bool Foam::argList::regroupArgv(int&amp; argc, char**&amp; argv)
  259 {
  260     int nArgs = 0;
  261     int listDepth = 0;
  262     string tmpString;
  263 
  264     // note: we also re-write directly into args_
  265     // and use a second pass to sort out args/options
  266     for (int argI = 0; argI &lt; argc; ++argI)        //A
  267     {
  268         if (strcmp(argv[argI], &quot;(&quot;) == 0)          //B
  269         {
  270             ++listDepth;
  271             tmpString += &quot;(&quot;;
  272         }
  273         else if (strcmp(argv[argI], &quot;)&quot;) == 0)     //C
  274         {
  275             if (listDepth)                         //D
  276             {
  277                 --listDepth;
  278                 tmpString += &quot;)&quot;;
  279                 if (listDepth == 0)
  280                 {
  281                     args_[nArgs++] = tmpString;
  282                     tmpString.clear();
  283                 }
  284             }
  285             else                                   //E
  286             {
  287                 args_[nArgs++] = argv[argI];
  288             }
  289         }
  290         else if (listDepth)                        //F
  291         {
  292             // quote each string element
  293             tmpString += &quot;\&quot;&quot;;
  294             tmpString += argv[argI];
  295             tmpString += &quot;\&quot;&quot;;
  296         }
  297         else                                       //G
  298         {
  299             args_[nArgs++] = argv[argI];
  300         }
  301     }
  302 
  303     if (tmpString.size())                          //H
  304     {
  305         args_[nArgs++] = tmpString;
  306     }
  307 
  308     args_.setSize(nArgs);                          //I
  309 
  310     return nArgs &lt; argc;
  311 }
</code></pre>
<p>mit den Ergebnissen:</p>
<p><strong>A</strong><br />
//Annahme argc = 2 mit Programmaufruf dann (_File.txt_), Leerstelle _ wichtig<br />
<a href="//1.erster">//1.erster</a> Durchgang, argI=0<br />
<a href="//2.zweiter">//2.zweiter</a> Durchgang, argI=1, nArgs=1 <a href="//3.dritter">//3.dritter</a> Durchgang, argI=2,nArgs=2<br />
<strong>B</strong><br />
<a href="//1.nicht">//1.nicht</a> erfuellt da erstes Argument Programmaufruf<br />
<a href="//2.erfuellt">//2.erfuellt</a> da zweites Argument mit ( beginnt sprich nur ( ist, listDepth wird 1, tmpString wird (<br />
//3. nicht erfuellt<br />
<strong>C</strong><br />
<a href="//1.nicht">//1.nicht</a> erfuellt da erstes Argument Programmaufruf weiter mit 289<br />
<a href="//2.nicht">//2.nicht</a> erfuellt weiter mit 289<br />
//3. erfuellt<br />
<strong>D</strong><br />
//3. nochmal erfuellt da listDepth immernoch 1<br />
<a href="//3.listDepth=0">//3.listDepth=0</a> und tmpString wird zu (“argv[1]“)<br />
//3. nochmal erfuellt, da listDepth nun 0<br />
<strong>E</strong><br />
kein Eintrag<br />
<strong>F</strong><br />
<a href="//1.waere">//1.waere</a> 0, also false weiter mit 298<br />
<a href="//2.erfuellt">//2.erfuellt</a> listDepth=1<br />
//2. tmpString wird letzlich (“argv[1]“ , weiter mit 304<br />
<strong>G</strong><br />
//1.args_[1] wird argv[argI] sprich argv[0]<br />
<strong>H</strong><br />
<a href="//1.waere">//1.waere</a> 0, also false<br />
<a href="//2.erfuellt">//2.erfuellt</a>, da groese ungleich 0<br />
//2. args_[2] wird zu (“argv[1]“ , nArgs wird 2<br />
//3. nicht erfuell da tmpString nun leer<br />
<strong>I</strong><br />
<a href="//1.Ergebnis">//1.Ergebnis</a> args_ bekommt kurzzeitig die groese 1, nArgs ist 1, <strong>args_[1] wird argv[argI] sprich argv[0]</strong><br />
<a href="//2.Ergebnis">//2.Ergebnis</a> args_ bekommt kurzzeitig die groese 2, nArgs ist 2, <strong>args_[2] wird zu (“argv[1]“</strong><br />
<a href="//3.Ergebnis">//3.Ergebnis</a>: <strong>args_[3] wird zu (“argv[1]“)</strong>, <strong>args_ bekommt groese 3</strong>, nArgs=3</p>
<p>ich bin jetzt schon mehremals die Schleife durchgegangen und komme immer wieder auf die Ergebnisse unter <strong>I</strong> insbesondere fuer die stringList args_. Diese sind ja recht seltsam, da nach ihnen unter args[2] (“argv[1]“ und unter args[3] (“argv[1]“) abgelegt werden wuerde. Vielleicht kann jemand mal drueber schaun und findet einen Fehler (in meiner Analyse <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/321931/for-schleife-mit-if-else-if-und-else</link><generator>RSS for Node</generator><lastBuildDate>Sat, 18 Jul 2026 15:46:09 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/321931.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 29 Nov 2013 10:28:49 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to for Schleife mit if, else if und else on Fri, 29 Nov 2013 14:38:27 GMT]]></title><description><![CDATA[<p>hi,<br />
ich schau mir gerade eine Methode eines Konstruktors an und eigentlich sollte nach der Klassenbeschreibung folgendes geschehen:</p>
<pre><code>Class argList
Sequences with &quot;(&quot; ... &quot;)&quot; are transformed into a stringList. For example,
    program -listFiles \( *.txt \)
would create a stringList:
    ( &quot;file1.txt&quot; &quot;file2.txt&quot; ... &quot;fileN.txt&quot; )
The backslash-escaping is required to avoid interpretation by the shell.
</code></pre>
<p>Wenn ich nun aber in den Konstruktor gehe und mir die Methode herausnehme welche oben genannte Beschreibung durchfuehren soll wie ich denke, ist das Ergebnis bei Annahme eines erdachten Terminalbefehls (siehe unter A) bei mir ein anderes:</p>
<pre><code>bool Foam::argList::regroupArgv(int&amp; argc, char**&amp; argv)
  259 {
  260     int nArgs = 0;
  261     int listDepth = 0;
  262     string tmpString;
  263 
  264     // note: we also re-write directly into args_
  265     // and use a second pass to sort out args/options
  266     for (int argI = 0; argI &lt; argc; ++argI)        //A
  267     {
  268         if (strcmp(argv[argI], &quot;(&quot;) == 0)          //B
  269         {
  270             ++listDepth;
  271             tmpString += &quot;(&quot;;
  272         }
  273         else if (strcmp(argv[argI], &quot;)&quot;) == 0)     //C
  274         {
  275             if (listDepth)                         //D
  276             {
  277                 --listDepth;
  278                 tmpString += &quot;)&quot;;
  279                 if (listDepth == 0)
  280                 {
  281                     args_[nArgs++] = tmpString;
  282                     tmpString.clear();
  283                 }
  284             }
  285             else                                   //E
  286             {
  287                 args_[nArgs++] = argv[argI];
  288             }
  289         }
  290         else if (listDepth)                        //F
  291         {
  292             // quote each string element
  293             tmpString += &quot;\&quot;&quot;;
  294             tmpString += argv[argI];
  295             tmpString += &quot;\&quot;&quot;;
  296         }
  297         else                                       //G
  298         {
  299             args_[nArgs++] = argv[argI];
  300         }
  301     }
  302 
  303     if (tmpString.size())                          //H
  304     {
  305         args_[nArgs++] = tmpString;
  306     }
  307 
  308     args_.setSize(nArgs);                          //I
  309 
  310     return nArgs &lt; argc;
  311 }
</code></pre>
<p>mit den Ergebnissen:</p>
<p><strong>A</strong><br />
//Annahme argc = 2 mit Programmaufruf dann (_File.txt_), Leerstelle _ wichtig<br />
<a href="//1.erster">//1.erster</a> Durchgang, argI=0<br />
<a href="//2.zweiter">//2.zweiter</a> Durchgang, argI=1, nArgs=1 <a href="//3.dritter">//3.dritter</a> Durchgang, argI=2,nArgs=2<br />
<strong>B</strong><br />
<a href="//1.nicht">//1.nicht</a> erfuellt da erstes Argument Programmaufruf<br />
<a href="//2.erfuellt">//2.erfuellt</a> da zweites Argument mit ( beginnt sprich nur ( ist, listDepth wird 1, tmpString wird (<br />
//3. nicht erfuellt<br />
<strong>C</strong><br />
<a href="//1.nicht">//1.nicht</a> erfuellt da erstes Argument Programmaufruf weiter mit 289<br />
<a href="//2.nicht">//2.nicht</a> erfuellt weiter mit 289<br />
//3. erfuellt<br />
<strong>D</strong><br />
//3. nochmal erfuellt da listDepth immernoch 1<br />
<a href="//3.listDepth=0">//3.listDepth=0</a> und tmpString wird zu (“argv[1]“)<br />
//3. nochmal erfuellt, da listDepth nun 0<br />
<strong>E</strong><br />
kein Eintrag<br />
<strong>F</strong><br />
<a href="//1.waere">//1.waere</a> 0, also false weiter mit 298<br />
<a href="//2.erfuellt">//2.erfuellt</a> listDepth=1<br />
//2. tmpString wird letzlich (“argv[1]“ , weiter mit 304<br />
<strong>G</strong><br />
//1.args_[1] wird argv[argI] sprich argv[0]<br />
<strong>H</strong><br />
<a href="//1.waere">//1.waere</a> 0, also false<br />
<a href="//2.erfuellt">//2.erfuellt</a>, da groese ungleich 0<br />
//2. args_[2] wird zu (“argv[1]“ , nArgs wird 2<br />
//3. nicht erfuell da tmpString nun leer<br />
<strong>I</strong><br />
<a href="//1.Ergebnis">//1.Ergebnis</a> args_ bekommt kurzzeitig die groese 1, nArgs ist 1, <strong>args_[1] wird argv[argI] sprich argv[0]</strong><br />
<a href="//2.Ergebnis">//2.Ergebnis</a> args_ bekommt kurzzeitig die groese 2, nArgs ist 2, <strong>args_[2] wird zu (“argv[1]“</strong><br />
<a href="//3.Ergebnis">//3.Ergebnis</a>: <strong>args_[3] wird zu (“argv[1]“)</strong>, <strong>args_ bekommt groese 3</strong>, nArgs=3</p>
<p>ich bin jetzt schon mehremals die Schleife durchgegangen und komme immer wieder auf die Ergebnisse unter <strong>I</strong> insbesondere fuer die stringList args_. Diese sind ja recht seltsam, da nach ihnen unter args[2] (“argv[1]“ und unter args[3] (“argv[1]“) abgelegt werden wuerde. Vielleicht kann jemand mal drueber schaun und findet einen Fehler (in meiner Analyse <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/2368863</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2368863</guid><dc:creator><![CDATA[Chaka]]></dc:creator><pubDate>Fri, 29 Nov 2013 14:38:27 GMT</pubDate></item></channel></rss>