<?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[Zahlenratespiel von Stroustrup]]></title><description><![CDATA[<p>Hallo<br />
Ich bin ein ganz frischer C++ Anfänger und Arbeite gerade B. Stroustrup's &quot;Einführung in die Programmierung mit C++ (dt. Ausgabe)&quot; durch.</p>
<p>Ich hoffe jemand zu finden der die Übung auf Seite 153, Nr.4 seiner Meinung<br />
nach erfolgreich gelöst hat und/oder jemand der mir evtl. einen &quot;<em>Wink mit dem Zaunpfahl</em>&quot; zur Codereduzierung geben kann. :p</p>
<p>Es geht konkret um folgende Übung:</p>
<blockquote>
<p>Schreiben Sie ein Programm für ein Zahlenratespiel. Der Benutzer denkt sich eine Zahl zwischen<br />
1 und 100 und Ihr Programm stellt Fragen, um herauszufinden, wie die gesuchte Zahl lautet (z.B.<br />
&quot;Ist die gedachte Zahl kleiner als 50?&quot;). Ihr Programm sollte in der Lage sein, die Zahl nach nur sieben<br />
Fragen erfolgreich zu raten. Hinweis: Verwenden Sie die Operatoren &lt; und &lt;= sowie das if-else-Konstrukt.</p>
</blockquote>
<p>Hier stellt sich mir schon mal die Frage &quot;zwischen 1-100&quot;?. Also ist von 2 bis 99 gemeint?. Ich gehe davon aus das es schlecht übersetzt wurde oder mein Verständnis gerade Urlaub macht und nehme &quot;von 1 bis 100&quot;.</p>
<p>Nach drei Tagen knuspern an der Aufgabe funktioniert es immerhin soweit das ich mit sieben oder weniger Fragen alles von 1-50 erraten kann. Das sind jetzt aber schon 500 Zeilen Code. Die restlichen 500 Zeilen Fleißarbeit der Zahlen 51-100 habe ich mir jetzt mal gespart da es ja im Prinzip funktioniert.</p>
<p>Mein Ansatz ist:<br />
Ist die Zahl:<br />
1-50/51-100<br />
gerade/ungerade<br />
und dann immer im Ausschlussverfahren die Hälfte der restlichen<br />
mit &quot;&lt;=&quot; herauszufiltern.</p>
<p>Die Frage für mich ist, war das ganze eine Fleißaufgabe oder<br />
gibt es doch noch eine Möglichkeit das ganze kürzer zu machen.<br />
Da in dem Buch die &quot;Funtktion&quot; kurz angerissen wurde denke ich<br />
man könnte zumindest die Ausgabe &quot;Falsche eingabe&quot; abkürzen.<br />
Aber um das eigentliche if-else,else Konstrukt kommt man in dem<br />
Umfang wohl nicht vorbei.</p>
<p>Hier ist der Code:</p>
<pre><code>/*
Schreiben Sie ein Programm für ein Zahlenratespiel. Der Benutzer denkt sich eine Zahl zwischen
1 und 100 und Ihr Programm stellt Fragen, um herauszufinden, wie die gesuchte Zahl lautet (z.B.
&quot;Ist die gedachte Zahl kleiner als 50?&quot;). Ihr Programm sollte in der Lage sein, die Zahl nach nur sieben
Fragen erfolgreich zu raten. Hinweis: Verwenden Sie die Operatoren &lt; und &lt;= sowie das if-else-Konstrukt.
*/

#include &quot;std_lib_facilities.h&quot;     // vorgefertigte Datei für das Buch

int main() 				            // C++-Programme beginnen mit der Ausführung von main
{
    int zahl =0;
    char qst = 'n';

    cout &lt;&lt; &quot;Ich bin der schlaue Computer, bitte denken Sie sich eine Zahl! \n&quot;;
    cout &lt;&lt; &quot;1. Ist die Zahl zwischen 1 und 50 (j,n)\n&quot;;
    cin &gt;&gt; qst;

    if (qst == 'j')
    {
        cout &lt;&lt; &quot;2. Ist die Zahl 1 bis 25?\n&quot;;
        cin &gt;&gt; qst;

// -----------------------------------------------------------------------------------------------
        if (qst == 'j')                 // ist 1-25
        {
            cout &lt;&lt; &quot;3. Ist die Zahl gerade?:\n&quot;;
            cin &gt;&gt; qst;
            if (qst == 'j')             // ist 2,4,6,8,10,12,14,16,18,20,22,24
            {
                cout &lt;&lt; &quot;4. Ist die Zahl 12 oder kleiner?&quot;;
                cin &gt;&gt; qst;
                if (qst == 'j')         // ist 2,4,6,8,10,12
                {
                    cout &lt;&lt; &quot;5. Ist die Zahl 6 oder kleiner?&quot;;
                    cin &gt;&gt; qst;
                    if (qst == 'j')     // ist 2,4,6
                    {
                        cout &lt;&lt; &quot;6. Ist die Zahl 4 oder kleiner?&quot;;
                        cin &gt;&gt; qst;
                        if (qst == 'j')     // ist 4,2
                        {
                            cout &lt;&lt; &quot;7. Ist die Zahl 4 ?&quot;;
                            cin &gt;&gt; qst;
                            if (qst == 'j')     // ist 2
                            {
                                cout &lt;&lt; &quot;Die Zahl ist 2!\n&quot;;
                            }
                            else if (qst == 'n')    // ist 4
                            {
                                cout &lt;&lt; &quot;Die Zahl ist 4!\n&quot;;
                            }
                            else
                                cout &lt;&lt; &quot;Falsche Eingabe!&quot;;
                        }
                        else if (qst == 'n')
                        {
                            cout &lt;&lt; &quot;Die Zahl ist 6!&quot;;
                        }
                        else
                            cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;
                    }
                    else if (qst == 'n')    // ist 8,10,12
                    {
                        cout &lt;&lt; &quot;6. Ist die Zahl 10 oder kleiner?&quot;;
                        cin &gt;&gt; qst;
                        if (qst == 'j')     // ist 8,10
                        {
                            cout &lt;&lt; &quot;7. Ist die Zahl 10?\n&quot;;
                            cin &gt;&gt; qst;
                            if (qst == 'j')     // ist 10
                            {
                                cout &lt;&lt; &quot;Die Zahl ist 10!&quot;;
                            }
                            else if (qst == 'n')    // ist 8
                            {
                                cout &lt;&lt; &quot;Die Zahl ist 8!\n&quot;;
                            }
                            else
                                cout &lt;&lt; &quot;Falsche Eingabe!&quot;;
                        }
                        else if (qst == 'n')    // ist 12
                        {
                            cout &lt;&lt; &quot;Die Zahl ist 12!\n&quot;;
                        }
                        else
                            cout &lt;&lt; &quot;falsche Eingabe!&quot;;
                    }
                    else
                        cout &lt;&lt; &quot;falsche Eingabe!&quot;;

                }
                else if (qst == 'n')    // ist 14,16,18,20,22,24
                {
                    cout &lt;&lt; &quot;5. Ist die Zahl 18 oder kleiner?&quot;;
                    cin &gt;&gt; qst;
                    if (qst == 'j')     // ist 14,16,18
                    {
                        cout &lt;&lt; &quot;6. Ist die Zahl 16 oder kleiner?\n&quot;;
                        cin &gt;&gt; qst;
                        if (qst == 'j')     // ist 14,16
                        {
                            cout &lt;&lt; &quot;7. Ist die Zahl 16?\n&quot;;
                            cin &gt;&gt; qst;
                        }
                        else if (qst == 'n')
                        {
                            cout &lt;&lt; &quot;Die Zahl ist 18!\n&quot;;
                        }
                        else
                            cout &lt;&lt; &quot;Falsche Eingabe!&quot;;
                    }
                    else if (qst == 'n')    // ist 20,22,24
                    {
                        cout &lt;&lt; &quot;6. Ist die Zahl 22 oder kleiner?\n&quot;;
                        cin &gt;&gt; qst;
                        if (qst == 'j')     // ist 20,22
                        {
                            cout &lt;&lt; &quot;Ist die Zahl 22?\n&quot;;
                            cin &gt;&gt; qst;
                            if (qst == 'j')
                            {
                                cout &lt;&lt; &quot;Die Zahl ist 22 !\n&quot;;
                            }
                            else if (qst == 'n')
                            {
                                cout &lt;&lt; &quot;Die Zahl ist 20!\n&quot;;
                            }
                            else
                                cout &lt;&lt; &quot;Falsche Eingabe!&quot;;
                        }
                        else if (qst == 'n') // ist 24
                        {
                            cout &lt;&lt; &quot;Die Zahl ist 24!\n&quot;;
                        }
                        else
                            cout &lt;&lt; &quot;Falsche Eingabe!&quot;;
                    }
                    else
                        cout &lt;&lt; &quot;Falsche Eingabe!&quot;;
                }
                else
                    cout &lt;&lt; &quot;Falsche Eingabe!&quot;;
            }
// ------------------------------------------------------------------------------------------
            else if (qst == 'n')    // ist 1,3,5,7,9,11,13,15,17,19,21,23,25
            {
                cout &lt;&lt; &quot;4. Ist die Zahl 13 oder kleiner?\n&quot;;
                cin &gt;&gt; qst;
                if (qst == 'j')     // ist 1,3,5,7,9,11,13
                {
                    cout &lt;&lt; &quot;5. Ist die Zahl 5 oder kleiner?\n&quot;;
                    cin &gt;&gt; qst;
                    if (qst == 'j')     // ist 1,3,5,
                    {
                        cout &lt;&lt; &quot;6. Ist die Zahl 3 oder kleiner?\n&quot;;
                        cin &gt;&gt; qst;
                        if (qst == 'j')     // ist 1,3
                        {
                            cout &lt;&lt; &quot;7. Ist die Zahl 3 ?\n&quot;;
                            cin &gt;&gt; qst;
                            if (qst == 'j')     // ist 3
                            {
                                cout &lt;&lt; &quot;Die Zahl ist 3! \n&quot;;
                            }
                            else if (qst == 'n')
                            {
                                cout &lt;&lt; &quot;Die Zahl ist 1 !\n&quot;;
                            }
                            else
                                cout &lt;&lt; &quot;Falsche Eingabe!&quot;;

                        }
                        else if (qst == 'n')    // ist 5
                        {
                            cout &lt;&lt; &quot;Die Zahl ist 5\n&quot;;
                        }
                        else
                            cout &lt;&lt; &quot;Falsche Eingabe!&quot;;
                    }
                    else if (qst == 'n')    // ist 7,9,11,13
                    {
                        cout &lt;&lt; &quot;6. Ist die Zahl 7 oder 9?\n&quot;;
                        cin &gt;&gt; qst;
                        if (qst == 'j')     // ist 7,9
                        {
                            cout &lt;&lt; &quot;7. Ist die Zahl 7 ?\n&quot;;
                            cin &gt;&gt; qst;
                            if (qst == 'j')     // ist 7
                            {
                                cout &lt;&lt; &quot;Die Zahl ist 7 ! \n&quot;;
                            }
                            else if (qst == 'n')    // ist 9
                            {
                                cout &lt;&lt; &quot;Die Zahl ist 9&quot;;
                            }
                            else
                                cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;
                        }
                        else if (qst == 'n')    // ist 11,13
                        {
                            cout &lt;&lt; &quot;Ist die Zahl 11 ?\n&quot;;
                            cin &gt;&gt; qst;
                            if (qst == 'j')     // ist 11
                            {
                                cout &lt;&lt; &quot;Die Zahl ist 11 ! \n&quot;;
                            }
                            else if (qst == 'j')    // ist 13
                            {
                                cout &lt;&lt; &quot;Die Zahl ist 13 !\n&quot;;
                            }
                            else
                                cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;
                        }
                        else
                            cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;
                    }
                    else
                        cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;

                }
                else if (qst == 'n')    // ist 15,17,19,21,23,25
                {
                    cout &lt;&lt; &quot;5. Ist die Zahl 19 oder kleiner?\n&quot;;
                    cin &gt;&gt; qst;
                    if (qst == 'j')     // ist 15,17,19
                    {
                        cout &lt;&lt; &quot;6. Ist die Zahl 17 oder kleiner?\n&quot;;
                        cin &gt;&gt; qst;
                        if (qst == 'j')     // ist 15,17
                        {
                            cout  &lt;&lt; &quot;6. Ist die Zahl 17?\n&quot;;
                            cin &gt;&gt; qst;
                            if (qst == 'j')     // ist 17
                            {
                                cout &lt;&lt; &quot;Die Zahl ist 17! \n&quot;;
                            }
                            else if (qst == 'n')    // ist 15
                            {
                                cout &lt;&lt; &quot;Die Zahl ist 15! \n&quot;;
                            }
                            else
                                cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;

                        }
                        else if (qst == 'n')    // ist 19
                        {
                            cout &lt;&lt; &quot;Die Zahl ist 19!&quot;;
                        }
                        else
                            cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;
                    }
                    else if (qst == 'n')        // ist 21,23,25
                    {
                        cout &lt;&lt; &quot;6. Ist die Zahl 23 oder kleiner?&quot;;
                        cin &gt;&gt; qst;
                        if (qst == 'j')         // ist 21,23
                        {
                            cout &lt;&lt; &quot;7. Ist die Zahl 23? \n&quot;;
                            cin &gt;&gt; qst;
                            if (qst == 'j')     // ist 23
                            {
                                cout &lt;&lt; &quot;Die Zahl ist 23! \n&quot;;
                            }
                            else if (qst == 'n')    // ist 21
                            {
                                cout &lt;&lt; &quot;Die Zahl ist 21! \n&quot;;
                            }
                            else
                                cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;
                        }
                        else if (qst == 'n')    // ist 25
                        {
                            cout &lt;&lt; &quot;Die Zahl ist 25!\n&quot;;
                        }
                        else
                            cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;
                    }
                    else
                        cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;
                }
                else
                    cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;
            }

        }
// ---------------------------------------------------------------------------------------------
        else if (qst == 'n')        // ist 26-50
        {
            cout &lt;&lt; &quot;3. Ist die Zahl gerade?:\n&quot;;
            cin &gt;&gt; qst;
            if (qst == 'j')         // ist 26,28,30,32,34,36,38,40,42,44,46,48,50
            {
                cout &lt;&lt; &quot;4. Ist die Zahl 36 oder kleiner\n&quot;;
                cin &gt;&gt; qst;
                if (qst == 'j')     // ist 26,28,30,32,34,36
                {
                    cout &lt;&lt; &quot;5. Ist die Zahl 30 oder kleiner?\n&quot;;
                    cin &gt;&gt; qst;
                    if (qst == 'j') // ist 26,28,30
                    {
                        cout &lt;&lt; &quot;6. Ist die Zahl 28 oder kleiner?\n&quot;;
                        cin &gt;&gt; qst;
                        if (qst == 'j')     // ist 26,28
                        {
                            cout &lt;&lt; &quot;7. Ist die Zahl 28?\n&quot;;
                            cin &gt;&gt; qst;
                            if (qst == 'j')     // ist 28
                            {
                                cout &lt;&lt; &quot;Die Zahl ist 28! \n&quot;;
                            }
                            else if (qst == 'n')    // ist 26
                            {
                                cout &lt;&lt; &quot;Die Zahl ist 26!\n&quot;;
                            }
                            else
                                cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;
                        }
                        else if (qst == 'n')    // ist 30
                        {
                            cout &lt;&lt; &quot;Die Zahl ist 30\n&quot;;
                        }
                        else
                            cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;
                    }
                }
                else if (qst == 'n') // ist 38,40,42,44,46,48,50
                {
                    cout &lt;&lt; &quot;5. Ist die Zahl 42 oder kleiner?\n&quot;;
                    cin &gt;&gt; qst;
                    if (qst == 'j')     // ist 28,40,42
                    {
                        cout &lt;&lt; &quot;6. Ist die Zahl 40 oder kleiner?\n&quot;;
                        cin &gt;&gt; qst;
                        if (qst == 'j')     // ist 28,40
                        {
                            cout &lt;&lt; &quot;7. Ist die Zahl 40?\n&quot;;
                            cin &gt;&gt; qst;
                            if (qst == 'j')     // ist 40
                            {
                                cout &lt;&lt; &quot;Die Zahl ist 40!\n&quot;;
                            }
                            else if (qst == 'n')    // ist 28
                            {
                                cout &lt;&lt; &quot;Die Zahl ist 28!\n&quot;;
                            }
                            else
                                cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;
                        }
                        else if (qst == 'n')    // ist 42
                        {
                            cout &lt;&lt; &quot;Die Zahl ist 42!\n&quot;;
                        }
                        else
                            cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;
                    }
                }
                else if (qst == 'n')    // ist 44,46,48,50
                {
                    cout &lt;&lt; &quot;6. &lt;=46 ?&quot;;
                    cin &gt;&gt; qst;
                    if (qst == 'j')     // 44,46
                    {
                        cout &lt;&lt; &quot;7. 46?\n&quot;;
                        cin &gt;&gt; qst;
                        if (qst == 'j')     // ist 46
                        {
                            cout &lt;&lt; &quot;Ist 46!\n&quot;;
                        }
                        else if (qst == 'n')    // ist 44
                        {
                            cout &lt;&lt; &quot;Ist 44!\n&quot;;
                        }
                        else
                            cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;
                    }
                    else if (qst == 'n')   // ist 48,50
                    {
                        cout &lt;&lt; &quot;6. 48?\n&quot;;
                        cin &gt;&gt; qst;
                        if (qst == 'j')     // ist 48
                        {
                            cout &lt;&lt; &quot;Ist 48!\n&quot;;
                        }
                        else if (qst == 'n')    // ist 50
                        {
                            cout &lt;&lt; &quot;Ist 50!\n&quot;;
                        }
                        else
                            cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;
                    }

                }

                else
                    cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;
            }
// -----------------------------------------------------------------------------------
            else if (qst == 'n')    // ist 27,29,31,33,35,37,39,41,43,45,47,49
            {
                cout &lt;&lt; &quot;4. &lt;=37 ?\n&quot;;
                cin &gt;&gt; qst;
                if (qst == 'j')     // ist 27,29,31,33,35,37
                {
                    cout &lt;&lt; &quot;5. &lt;=31 ?\n&quot;;
                    cin &gt;&gt; qst;
                    if (qst == 'j')     // ist 27,29,31
                    {
                        cout &lt;&lt; &quot;6. &lt;=29 ?\n&quot;;
                        cin &gt;&gt; qst;
                        if (qst == 'j')     // ist 27,29
                        {
                            cout &lt;&lt; &quot;7. =29 ?&quot;;
                            cin &gt;&gt; qst;
                            if (qst == 'j')     // 29
                            {
                                cout &lt;&lt; &quot;Ist 29!\n&quot;;
                            }
                            else if (qst == 'n')    // 27
                            {
                                cout &lt;&lt; &quot;Ist 27!\n&quot;;
                            }
                        }
                        else if (qst == 'n')    // 31
                        {

                        }
                        else
                            cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;
                    }
                    else if (qst == 'n')    // ist 33,35,37
                    {
                        cout &lt;&lt; &quot; &lt;=35 ?\n&quot;;
                        cin &gt;&gt; qst;
                        if (qst == 'j')     // 33,35
                        {
                            cout &lt;&lt; &quot; =35?\n&quot;;
                            cin &gt;&gt; qst;
                            if (qst == 'j')     // 35
                            {
                                cout &lt;&lt; &quot; =35!\n&quot;;
                            }
                            else if (qst == 'n')
                                cout &lt;&lt; &quot; =33\n&quot;;
                            else
                                cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;
                        }
                        else if (qst == 'n')    // 37
                            cout &lt;&lt; &quot; =37!\n&quot;;
                        else
                            cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;
                    }
                    else
                        cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;
                }
                else if (qst == 'n')    // ist 39,41,43,45,47,49
                {
                    cout &lt;&lt; &quot; &lt;=43 ?\n&quot;;
                    cin &gt;&gt; qst;
                    if (qst == 'j')     // 39,41,43
                    {
                        cout &lt;&lt; &quot; &lt;=41 ?\n&quot;;
                        cin &gt;&gt; qst;
                        if (qst == 'j')     // 39,41
                        {
                            cout &lt;&lt; &quot; =41?\n&quot;;
                            cin &gt;&gt; qst;
                            if (qst == 'j')     // 41
                                cout &lt;&lt; &quot; =41!\n&quot;;
                            else if (qst == 'n')    // 39
                                cout &lt;&lt; &quot; =39!\n&quot;;
                            else
                                cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;
                        }
                        else if (qst == 'n')    // 43
                            cout &lt;&lt; &quot; =43!\n&quot;;
                        else
                            cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;
                    }
                    else if (qst == 'n')    // 45,47,49
                    {
                        cout &lt;&lt; &quot; &lt;=47 ?\n&quot;;
                        cin &gt;&gt; qst;
                        if (qst == 'j')     // 45,47
                        {
                            cout &lt;&lt; &quot; =47 ?\n&quot;;
                            cin &gt;&gt; qst;
                            if (qst == 'j')     // 47
                                cout &lt;&lt; &quot; =47!\n&quot;;
                            else if (qst == 'n')    // 45
                                cout &lt;&lt; &quot; =45!\n&quot;;
                            else
                                cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;
                        }
                        else if (qst == 'n')    // 49
                            cout &lt;&lt; &quot; =49!\n&quot;;
                        else
                            cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;
                    }
                }
                else
                    cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;

            }
            else
                cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;
        }
        else
            cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;

    }

//--------------------------------------------------------------------------------

    else if (qst == 'n')            // ist zwischen 50-100
    {
        cout &lt;&lt; &quot;Ist die Zahl zwischen 50 und 75?\n&quot;;
        cin &gt;&gt; qst;

        if (qst == 'j')             // ist zwischen 50-75
        {
            cout &lt;&lt; &quot;Ist die Zahl gerade?:\n&quot;;
            cin &gt;&gt; qst;
        }
        else if (qst == 'n')        // ist zwischen 75-100
        {
            cout &lt;&lt; &quot;Ist die Zahl gerade?:\n&quot;;
            cin &gt;&gt; qst;
        }
        else
            cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;
    }

    else
// TODO (astro#1#): test
        cout &lt;&lt; &quot;falsche Eingabe!&quot;;

    return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/331172/zahlenratespiel-von-stroustrup</link><generator>RSS for Node</generator><lastBuildDate>Fri, 01 May 2026 20:59:05 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/331172.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 14 Feb 2015 11:18:22 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Zahlenratespiel von Stroustrup on Sat, 14 Feb 2015 11:18:22 GMT]]></title><description><![CDATA[<p>Hallo<br />
Ich bin ein ganz frischer C++ Anfänger und Arbeite gerade B. Stroustrup's &quot;Einführung in die Programmierung mit C++ (dt. Ausgabe)&quot; durch.</p>
<p>Ich hoffe jemand zu finden der die Übung auf Seite 153, Nr.4 seiner Meinung<br />
nach erfolgreich gelöst hat und/oder jemand der mir evtl. einen &quot;<em>Wink mit dem Zaunpfahl</em>&quot; zur Codereduzierung geben kann. :p</p>
<p>Es geht konkret um folgende Übung:</p>
<blockquote>
<p>Schreiben Sie ein Programm für ein Zahlenratespiel. Der Benutzer denkt sich eine Zahl zwischen<br />
1 und 100 und Ihr Programm stellt Fragen, um herauszufinden, wie die gesuchte Zahl lautet (z.B.<br />
&quot;Ist die gedachte Zahl kleiner als 50?&quot;). Ihr Programm sollte in der Lage sein, die Zahl nach nur sieben<br />
Fragen erfolgreich zu raten. Hinweis: Verwenden Sie die Operatoren &lt; und &lt;= sowie das if-else-Konstrukt.</p>
</blockquote>
<p>Hier stellt sich mir schon mal die Frage &quot;zwischen 1-100&quot;?. Also ist von 2 bis 99 gemeint?. Ich gehe davon aus das es schlecht übersetzt wurde oder mein Verständnis gerade Urlaub macht und nehme &quot;von 1 bis 100&quot;.</p>
<p>Nach drei Tagen knuspern an der Aufgabe funktioniert es immerhin soweit das ich mit sieben oder weniger Fragen alles von 1-50 erraten kann. Das sind jetzt aber schon 500 Zeilen Code. Die restlichen 500 Zeilen Fleißarbeit der Zahlen 51-100 habe ich mir jetzt mal gespart da es ja im Prinzip funktioniert.</p>
<p>Mein Ansatz ist:<br />
Ist die Zahl:<br />
1-50/51-100<br />
gerade/ungerade<br />
und dann immer im Ausschlussverfahren die Hälfte der restlichen<br />
mit &quot;&lt;=&quot; herauszufiltern.</p>
<p>Die Frage für mich ist, war das ganze eine Fleißaufgabe oder<br />
gibt es doch noch eine Möglichkeit das ganze kürzer zu machen.<br />
Da in dem Buch die &quot;Funtktion&quot; kurz angerissen wurde denke ich<br />
man könnte zumindest die Ausgabe &quot;Falsche eingabe&quot; abkürzen.<br />
Aber um das eigentliche if-else,else Konstrukt kommt man in dem<br />
Umfang wohl nicht vorbei.</p>
<p>Hier ist der Code:</p>
<pre><code>/*
Schreiben Sie ein Programm für ein Zahlenratespiel. Der Benutzer denkt sich eine Zahl zwischen
1 und 100 und Ihr Programm stellt Fragen, um herauszufinden, wie die gesuchte Zahl lautet (z.B.
&quot;Ist die gedachte Zahl kleiner als 50?&quot;). Ihr Programm sollte in der Lage sein, die Zahl nach nur sieben
Fragen erfolgreich zu raten. Hinweis: Verwenden Sie die Operatoren &lt; und &lt;= sowie das if-else-Konstrukt.
*/

#include &quot;std_lib_facilities.h&quot;     // vorgefertigte Datei für das Buch

int main() 				            // C++-Programme beginnen mit der Ausführung von main
{
    int zahl =0;
    char qst = 'n';

    cout &lt;&lt; &quot;Ich bin der schlaue Computer, bitte denken Sie sich eine Zahl! \n&quot;;
    cout &lt;&lt; &quot;1. Ist die Zahl zwischen 1 und 50 (j,n)\n&quot;;
    cin &gt;&gt; qst;

    if (qst == 'j')
    {
        cout &lt;&lt; &quot;2. Ist die Zahl 1 bis 25?\n&quot;;
        cin &gt;&gt; qst;

// -----------------------------------------------------------------------------------------------
        if (qst == 'j')                 // ist 1-25
        {
            cout &lt;&lt; &quot;3. Ist die Zahl gerade?:\n&quot;;
            cin &gt;&gt; qst;
            if (qst == 'j')             // ist 2,4,6,8,10,12,14,16,18,20,22,24
            {
                cout &lt;&lt; &quot;4. Ist die Zahl 12 oder kleiner?&quot;;
                cin &gt;&gt; qst;
                if (qst == 'j')         // ist 2,4,6,8,10,12
                {
                    cout &lt;&lt; &quot;5. Ist die Zahl 6 oder kleiner?&quot;;
                    cin &gt;&gt; qst;
                    if (qst == 'j')     // ist 2,4,6
                    {
                        cout &lt;&lt; &quot;6. Ist die Zahl 4 oder kleiner?&quot;;
                        cin &gt;&gt; qst;
                        if (qst == 'j')     // ist 4,2
                        {
                            cout &lt;&lt; &quot;7. Ist die Zahl 4 ?&quot;;
                            cin &gt;&gt; qst;
                            if (qst == 'j')     // ist 2
                            {
                                cout &lt;&lt; &quot;Die Zahl ist 2!\n&quot;;
                            }
                            else if (qst == 'n')    // ist 4
                            {
                                cout &lt;&lt; &quot;Die Zahl ist 4!\n&quot;;
                            }
                            else
                                cout &lt;&lt; &quot;Falsche Eingabe!&quot;;
                        }
                        else if (qst == 'n')
                        {
                            cout &lt;&lt; &quot;Die Zahl ist 6!&quot;;
                        }
                        else
                            cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;
                    }
                    else if (qst == 'n')    // ist 8,10,12
                    {
                        cout &lt;&lt; &quot;6. Ist die Zahl 10 oder kleiner?&quot;;
                        cin &gt;&gt; qst;
                        if (qst == 'j')     // ist 8,10
                        {
                            cout &lt;&lt; &quot;7. Ist die Zahl 10?\n&quot;;
                            cin &gt;&gt; qst;
                            if (qst == 'j')     // ist 10
                            {
                                cout &lt;&lt; &quot;Die Zahl ist 10!&quot;;
                            }
                            else if (qst == 'n')    // ist 8
                            {
                                cout &lt;&lt; &quot;Die Zahl ist 8!\n&quot;;
                            }
                            else
                                cout &lt;&lt; &quot;Falsche Eingabe!&quot;;
                        }
                        else if (qst == 'n')    // ist 12
                        {
                            cout &lt;&lt; &quot;Die Zahl ist 12!\n&quot;;
                        }
                        else
                            cout &lt;&lt; &quot;falsche Eingabe!&quot;;
                    }
                    else
                        cout &lt;&lt; &quot;falsche Eingabe!&quot;;

                }
                else if (qst == 'n')    // ist 14,16,18,20,22,24
                {
                    cout &lt;&lt; &quot;5. Ist die Zahl 18 oder kleiner?&quot;;
                    cin &gt;&gt; qst;
                    if (qst == 'j')     // ist 14,16,18
                    {
                        cout &lt;&lt; &quot;6. Ist die Zahl 16 oder kleiner?\n&quot;;
                        cin &gt;&gt; qst;
                        if (qst == 'j')     // ist 14,16
                        {
                            cout &lt;&lt; &quot;7. Ist die Zahl 16?\n&quot;;
                            cin &gt;&gt; qst;
                        }
                        else if (qst == 'n')
                        {
                            cout &lt;&lt; &quot;Die Zahl ist 18!\n&quot;;
                        }
                        else
                            cout &lt;&lt; &quot;Falsche Eingabe!&quot;;
                    }
                    else if (qst == 'n')    // ist 20,22,24
                    {
                        cout &lt;&lt; &quot;6. Ist die Zahl 22 oder kleiner?\n&quot;;
                        cin &gt;&gt; qst;
                        if (qst == 'j')     // ist 20,22
                        {
                            cout &lt;&lt; &quot;Ist die Zahl 22?\n&quot;;
                            cin &gt;&gt; qst;
                            if (qst == 'j')
                            {
                                cout &lt;&lt; &quot;Die Zahl ist 22 !\n&quot;;
                            }
                            else if (qst == 'n')
                            {
                                cout &lt;&lt; &quot;Die Zahl ist 20!\n&quot;;
                            }
                            else
                                cout &lt;&lt; &quot;Falsche Eingabe!&quot;;
                        }
                        else if (qst == 'n') // ist 24
                        {
                            cout &lt;&lt; &quot;Die Zahl ist 24!\n&quot;;
                        }
                        else
                            cout &lt;&lt; &quot;Falsche Eingabe!&quot;;
                    }
                    else
                        cout &lt;&lt; &quot;Falsche Eingabe!&quot;;
                }
                else
                    cout &lt;&lt; &quot;Falsche Eingabe!&quot;;
            }
// ------------------------------------------------------------------------------------------
            else if (qst == 'n')    // ist 1,3,5,7,9,11,13,15,17,19,21,23,25
            {
                cout &lt;&lt; &quot;4. Ist die Zahl 13 oder kleiner?\n&quot;;
                cin &gt;&gt; qst;
                if (qst == 'j')     // ist 1,3,5,7,9,11,13
                {
                    cout &lt;&lt; &quot;5. Ist die Zahl 5 oder kleiner?\n&quot;;
                    cin &gt;&gt; qst;
                    if (qst == 'j')     // ist 1,3,5,
                    {
                        cout &lt;&lt; &quot;6. Ist die Zahl 3 oder kleiner?\n&quot;;
                        cin &gt;&gt; qst;
                        if (qst == 'j')     // ist 1,3
                        {
                            cout &lt;&lt; &quot;7. Ist die Zahl 3 ?\n&quot;;
                            cin &gt;&gt; qst;
                            if (qst == 'j')     // ist 3
                            {
                                cout &lt;&lt; &quot;Die Zahl ist 3! \n&quot;;
                            }
                            else if (qst == 'n')
                            {
                                cout &lt;&lt; &quot;Die Zahl ist 1 !\n&quot;;
                            }
                            else
                                cout &lt;&lt; &quot;Falsche Eingabe!&quot;;

                        }
                        else if (qst == 'n')    // ist 5
                        {
                            cout &lt;&lt; &quot;Die Zahl ist 5\n&quot;;
                        }
                        else
                            cout &lt;&lt; &quot;Falsche Eingabe!&quot;;
                    }
                    else if (qst == 'n')    // ist 7,9,11,13
                    {
                        cout &lt;&lt; &quot;6. Ist die Zahl 7 oder 9?\n&quot;;
                        cin &gt;&gt; qst;
                        if (qst == 'j')     // ist 7,9
                        {
                            cout &lt;&lt; &quot;7. Ist die Zahl 7 ?\n&quot;;
                            cin &gt;&gt; qst;
                            if (qst == 'j')     // ist 7
                            {
                                cout &lt;&lt; &quot;Die Zahl ist 7 ! \n&quot;;
                            }
                            else if (qst == 'n')    // ist 9
                            {
                                cout &lt;&lt; &quot;Die Zahl ist 9&quot;;
                            }
                            else
                                cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;
                        }
                        else if (qst == 'n')    // ist 11,13
                        {
                            cout &lt;&lt; &quot;Ist die Zahl 11 ?\n&quot;;
                            cin &gt;&gt; qst;
                            if (qst == 'j')     // ist 11
                            {
                                cout &lt;&lt; &quot;Die Zahl ist 11 ! \n&quot;;
                            }
                            else if (qst == 'j')    // ist 13
                            {
                                cout &lt;&lt; &quot;Die Zahl ist 13 !\n&quot;;
                            }
                            else
                                cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;
                        }
                        else
                            cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;
                    }
                    else
                        cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;

                }
                else if (qst == 'n')    // ist 15,17,19,21,23,25
                {
                    cout &lt;&lt; &quot;5. Ist die Zahl 19 oder kleiner?\n&quot;;
                    cin &gt;&gt; qst;
                    if (qst == 'j')     // ist 15,17,19
                    {
                        cout &lt;&lt; &quot;6. Ist die Zahl 17 oder kleiner?\n&quot;;
                        cin &gt;&gt; qst;
                        if (qst == 'j')     // ist 15,17
                        {
                            cout  &lt;&lt; &quot;6. Ist die Zahl 17?\n&quot;;
                            cin &gt;&gt; qst;
                            if (qst == 'j')     // ist 17
                            {
                                cout &lt;&lt; &quot;Die Zahl ist 17! \n&quot;;
                            }
                            else if (qst == 'n')    // ist 15
                            {
                                cout &lt;&lt; &quot;Die Zahl ist 15! \n&quot;;
                            }
                            else
                                cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;

                        }
                        else if (qst == 'n')    // ist 19
                        {
                            cout &lt;&lt; &quot;Die Zahl ist 19!&quot;;
                        }
                        else
                            cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;
                    }
                    else if (qst == 'n')        // ist 21,23,25
                    {
                        cout &lt;&lt; &quot;6. Ist die Zahl 23 oder kleiner?&quot;;
                        cin &gt;&gt; qst;
                        if (qst == 'j')         // ist 21,23
                        {
                            cout &lt;&lt; &quot;7. Ist die Zahl 23? \n&quot;;
                            cin &gt;&gt; qst;
                            if (qst == 'j')     // ist 23
                            {
                                cout &lt;&lt; &quot;Die Zahl ist 23! \n&quot;;
                            }
                            else if (qst == 'n')    // ist 21
                            {
                                cout &lt;&lt; &quot;Die Zahl ist 21! \n&quot;;
                            }
                            else
                                cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;
                        }
                        else if (qst == 'n')    // ist 25
                        {
                            cout &lt;&lt; &quot;Die Zahl ist 25!\n&quot;;
                        }
                        else
                            cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;
                    }
                    else
                        cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;
                }
                else
                    cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;
            }

        }
// ---------------------------------------------------------------------------------------------
        else if (qst == 'n')        // ist 26-50
        {
            cout &lt;&lt; &quot;3. Ist die Zahl gerade?:\n&quot;;
            cin &gt;&gt; qst;
            if (qst == 'j')         // ist 26,28,30,32,34,36,38,40,42,44,46,48,50
            {
                cout &lt;&lt; &quot;4. Ist die Zahl 36 oder kleiner\n&quot;;
                cin &gt;&gt; qst;
                if (qst == 'j')     // ist 26,28,30,32,34,36
                {
                    cout &lt;&lt; &quot;5. Ist die Zahl 30 oder kleiner?\n&quot;;
                    cin &gt;&gt; qst;
                    if (qst == 'j') // ist 26,28,30
                    {
                        cout &lt;&lt; &quot;6. Ist die Zahl 28 oder kleiner?\n&quot;;
                        cin &gt;&gt; qst;
                        if (qst == 'j')     // ist 26,28
                        {
                            cout &lt;&lt; &quot;7. Ist die Zahl 28?\n&quot;;
                            cin &gt;&gt; qst;
                            if (qst == 'j')     // ist 28
                            {
                                cout &lt;&lt; &quot;Die Zahl ist 28! \n&quot;;
                            }
                            else if (qst == 'n')    // ist 26
                            {
                                cout &lt;&lt; &quot;Die Zahl ist 26!\n&quot;;
                            }
                            else
                                cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;
                        }
                        else if (qst == 'n')    // ist 30
                        {
                            cout &lt;&lt; &quot;Die Zahl ist 30\n&quot;;
                        }
                        else
                            cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;
                    }
                }
                else if (qst == 'n') // ist 38,40,42,44,46,48,50
                {
                    cout &lt;&lt; &quot;5. Ist die Zahl 42 oder kleiner?\n&quot;;
                    cin &gt;&gt; qst;
                    if (qst == 'j')     // ist 28,40,42
                    {
                        cout &lt;&lt; &quot;6. Ist die Zahl 40 oder kleiner?\n&quot;;
                        cin &gt;&gt; qst;
                        if (qst == 'j')     // ist 28,40
                        {
                            cout &lt;&lt; &quot;7. Ist die Zahl 40?\n&quot;;
                            cin &gt;&gt; qst;
                            if (qst == 'j')     // ist 40
                            {
                                cout &lt;&lt; &quot;Die Zahl ist 40!\n&quot;;
                            }
                            else if (qst == 'n')    // ist 28
                            {
                                cout &lt;&lt; &quot;Die Zahl ist 28!\n&quot;;
                            }
                            else
                                cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;
                        }
                        else if (qst == 'n')    // ist 42
                        {
                            cout &lt;&lt; &quot;Die Zahl ist 42!\n&quot;;
                        }
                        else
                            cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;
                    }
                }
                else if (qst == 'n')    // ist 44,46,48,50
                {
                    cout &lt;&lt; &quot;6. &lt;=46 ?&quot;;
                    cin &gt;&gt; qst;
                    if (qst == 'j')     // 44,46
                    {
                        cout &lt;&lt; &quot;7. 46?\n&quot;;
                        cin &gt;&gt; qst;
                        if (qst == 'j')     // ist 46
                        {
                            cout &lt;&lt; &quot;Ist 46!\n&quot;;
                        }
                        else if (qst == 'n')    // ist 44
                        {
                            cout &lt;&lt; &quot;Ist 44!\n&quot;;
                        }
                        else
                            cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;
                    }
                    else if (qst == 'n')   // ist 48,50
                    {
                        cout &lt;&lt; &quot;6. 48?\n&quot;;
                        cin &gt;&gt; qst;
                        if (qst == 'j')     // ist 48
                        {
                            cout &lt;&lt; &quot;Ist 48!\n&quot;;
                        }
                        else if (qst == 'n')    // ist 50
                        {
                            cout &lt;&lt; &quot;Ist 50!\n&quot;;
                        }
                        else
                            cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;
                    }

                }

                else
                    cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;
            }
// -----------------------------------------------------------------------------------
            else if (qst == 'n')    // ist 27,29,31,33,35,37,39,41,43,45,47,49
            {
                cout &lt;&lt; &quot;4. &lt;=37 ?\n&quot;;
                cin &gt;&gt; qst;
                if (qst == 'j')     // ist 27,29,31,33,35,37
                {
                    cout &lt;&lt; &quot;5. &lt;=31 ?\n&quot;;
                    cin &gt;&gt; qst;
                    if (qst == 'j')     // ist 27,29,31
                    {
                        cout &lt;&lt; &quot;6. &lt;=29 ?\n&quot;;
                        cin &gt;&gt; qst;
                        if (qst == 'j')     // ist 27,29
                        {
                            cout &lt;&lt; &quot;7. =29 ?&quot;;
                            cin &gt;&gt; qst;
                            if (qst == 'j')     // 29
                            {
                                cout &lt;&lt; &quot;Ist 29!\n&quot;;
                            }
                            else if (qst == 'n')    // 27
                            {
                                cout &lt;&lt; &quot;Ist 27!\n&quot;;
                            }
                        }
                        else if (qst == 'n')    // 31
                        {

                        }
                        else
                            cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;
                    }
                    else if (qst == 'n')    // ist 33,35,37
                    {
                        cout &lt;&lt; &quot; &lt;=35 ?\n&quot;;
                        cin &gt;&gt; qst;
                        if (qst == 'j')     // 33,35
                        {
                            cout &lt;&lt; &quot; =35?\n&quot;;
                            cin &gt;&gt; qst;
                            if (qst == 'j')     // 35
                            {
                                cout &lt;&lt; &quot; =35!\n&quot;;
                            }
                            else if (qst == 'n')
                                cout &lt;&lt; &quot; =33\n&quot;;
                            else
                                cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;
                        }
                        else if (qst == 'n')    // 37
                            cout &lt;&lt; &quot; =37!\n&quot;;
                        else
                            cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;
                    }
                    else
                        cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;
                }
                else if (qst == 'n')    // ist 39,41,43,45,47,49
                {
                    cout &lt;&lt; &quot; &lt;=43 ?\n&quot;;
                    cin &gt;&gt; qst;
                    if (qst == 'j')     // 39,41,43
                    {
                        cout &lt;&lt; &quot; &lt;=41 ?\n&quot;;
                        cin &gt;&gt; qst;
                        if (qst == 'j')     // 39,41
                        {
                            cout &lt;&lt; &quot; =41?\n&quot;;
                            cin &gt;&gt; qst;
                            if (qst == 'j')     // 41
                                cout &lt;&lt; &quot; =41!\n&quot;;
                            else if (qst == 'n')    // 39
                                cout &lt;&lt; &quot; =39!\n&quot;;
                            else
                                cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;
                        }
                        else if (qst == 'n')    // 43
                            cout &lt;&lt; &quot; =43!\n&quot;;
                        else
                            cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;
                    }
                    else if (qst == 'n')    // 45,47,49
                    {
                        cout &lt;&lt; &quot; &lt;=47 ?\n&quot;;
                        cin &gt;&gt; qst;
                        if (qst == 'j')     // 45,47
                        {
                            cout &lt;&lt; &quot; =47 ?\n&quot;;
                            cin &gt;&gt; qst;
                            if (qst == 'j')     // 47
                                cout &lt;&lt; &quot; =47!\n&quot;;
                            else if (qst == 'n')    // 45
                                cout &lt;&lt; &quot; =45!\n&quot;;
                            else
                                cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;
                        }
                        else if (qst == 'n')    // 49
                            cout &lt;&lt; &quot; =49!\n&quot;;
                        else
                            cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;
                    }
                }
                else
                    cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;

            }
            else
                cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;
        }
        else
            cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;

    }

//--------------------------------------------------------------------------------

    else if (qst == 'n')            // ist zwischen 50-100
    {
        cout &lt;&lt; &quot;Ist die Zahl zwischen 50 und 75?\n&quot;;
        cin &gt;&gt; qst;

        if (qst == 'j')             // ist zwischen 50-75
        {
            cout &lt;&lt; &quot;Ist die Zahl gerade?:\n&quot;;
            cin &gt;&gt; qst;
        }
        else if (qst == 'n')        // ist zwischen 75-100
        {
            cout &lt;&lt; &quot;Ist die Zahl gerade?:\n&quot;;
            cin &gt;&gt; qst;
        }
        else
            cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;
    }

    else
// TODO (astro#1#): test
        cout &lt;&lt; &quot;falsche Eingabe!&quot;;

    return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2442690</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2442690</guid><dc:creator><![CDATA[pauledd]]></dc:creator><pubDate>Sat, 14 Feb 2015 11:18:22 GMT</pubDate></item><item><title><![CDATA[Reply to Zahlenratespiel von Stroustrup on Sat, 14 Feb 2015 12:10:50 GMT]]></title><description><![CDATA[<p>Ich kenne das Buch nicht.</p>
<p>Waren noch keine Schleifen an der Reihe?</p>
<p>1. Du hast zwei (obere und untere) Grenzen (am Anfang 1 und 100).<br />
2. Du berechnest die Mitte daraus.<br />
3. Dann fragst du den Nutzer ob seine Zahl größer oder kleiner-gleich der Mitte ist.<br />
4. Je nach Antwort, setzt du die obere oder untere Grenze auf die bisherige Mitte.<br />
5. ist obere und untere Grenze gleich? Nein, dann weiter bei 2.<br />
6. Fertig.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2442694</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2442694</guid><dc:creator><![CDATA[DirkB]]></dc:creator><pubDate>Sat, 14 Feb 2015 12:10:50 GMT</pubDate></item><item><title><![CDATA[Reply to Zahlenratespiel von Stroustrup on Sat, 14 Feb 2015 12:31:39 GMT]]></title><description><![CDATA[<p>DirkB schrieb:</p>
<blockquote>
<p>Waren noch keine Schleifen an der Reihe?</p>
</blockquote>
<p>Doch, es wurde &quot;while&quot;, &quot;for&quot; und &quot;vector&quot; schon angeschnitten.<br />
Ich dachte halt nur if-else weil nichts anderes in der Aufgabe steht.<br />
Ich kanns mir aber auch nicht Vorstellen das ich 1000 Zeilen schreiben soll. <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="😃"
    /><br />
Ich werde dein Vorschlag mal durchprobieren, danke erstmal.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2442696</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2442696</guid><dc:creator><![CDATA[pauledd]]></dc:creator><pubDate>Sat, 14 Feb 2015 12:31:39 GMT</pubDate></item><item><title><![CDATA[Reply to Zahlenratespiel von Stroustrup on Sat, 14 Feb 2015 12:52:43 GMT]]></title><description><![CDATA[<p>Du kannst schon alles verwenden, das bereits behandelt wurde, es sei denn es wird explizit ausgeschlossen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2442697</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2442697</guid><dc:creator><![CDATA[Marthog]]></dc:creator><pubDate>Sat, 14 Feb 2015 12:52:43 GMT</pubDate></item><item><title><![CDATA[Reply to Zahlenratespiel von Stroustrup on Sat, 14 Feb 2015 13:48:50 GMT]]></title><description><![CDATA[<p>Marthog schrieb:</p>
<blockquote>
<p>Du kannst schon alles verwenden, das bereits behandelt wurde, es sei denn es wird explizit ausgeschlossen.</p>
</blockquote>
<p>Ok, danke.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2442698</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2442698</guid><dc:creator><![CDATA[pauledd]]></dc:creator><pubDate>Sat, 14 Feb 2015 13:48:50 GMT</pubDate></item><item><title><![CDATA[Reply to Zahlenratespiel von Stroustrup on Sat, 14 Feb 2015 16:21:05 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">#include &lt;iostream&gt;

using namespace std;
int main()
{
	cout&lt;&lt;&quot;[1,100]?&quot;;
	char c;int i,l=1,u=100;cin&gt;&gt;i;
	while(i=l+(u-l)/2,u-l&gt;1)cout&lt;&lt;l&lt;&lt;&quot;&lt;=x&lt;=&quot;&lt;&lt;i&lt;&lt;&quot;?&quot;,cin&gt;&gt;c,c=='y'?u=i:l=i+1;
	cout&lt;&lt;&quot;x&lt;&quot;&lt;&lt;u&lt;&lt;'?';cout&lt;&lt;&quot;x=&quot;&lt;&lt;(cin&gt;&gt;c,c=='y'?l:u)&lt;&lt;'\n';
}
</code></pre>
<p>Jemand Lust auf Golf? <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f921.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--clown_face"
      title=":clown:"
      alt="🤡"
    /></p>
<p>// edit: -6<br />
// edit2: -20<br />
// edit3: länge über alles ohne whitespace: 238</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2442702</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2442702</guid><dc:creator><![CDATA[Swordfish]]></dc:creator><pubDate>Sat, 14 Feb 2015 16:21:05 GMT</pubDate></item><item><title><![CDATA[Reply to Zahlenratespiel von Stroustrup on Sat, 14 Feb 2015 15:46:58 GMT]]></title><description><![CDATA[<p>Bitte ma noch keine Komplettlösung posten bis ich eine habe, auch wenn ich den Ehrgeiz einiger Leute geweckt haben sollte! Ich guck da jetzt mal extra weg! Ich will erstmal für mich eine Lösung finden. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2442707</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2442707</guid><dc:creator><![CDATA[pauledd]]></dc:creator><pubDate>Sat, 14 Feb 2015 15:46:58 GMT</pubDate></item><item><title><![CDATA[Reply to Zahlenratespiel von Stroustrup on Sat, 14 Feb 2015 15:51:21 GMT]]></title><description><![CDATA[<p>Die Lösung ist sowieso extrem unleserlich und kryptisch geschrieben.<br />
Swordfish hat zum Code-Golf aufgerufen, das heißt, dass für fortgeschrittene triviale Programm so kurz und/oder kryptisch wie möglich zu schreiben, indem man sämtliche Tricks der Sprache ausnutzt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2442708</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2442708</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Sat, 14 Feb 2015 15:51:21 GMT</pubDate></item><item><title><![CDATA[Reply to Zahlenratespiel von Stroustrup on Sat, 14 Feb 2015 17:04:28 GMT]]></title><description><![CDATA[<p>Nicht auf Kürze, dafür aber auf Würze optimiert:</p>
<pre><code>#include &lt;stdio.h&gt;
char i;template&lt;int I,int O&gt;struct o{void L(){const int _=(I+O)&gt;&gt;1;printf(&quot;&gt;%d?&quot;
,_);scanf(&quot; %c&quot;,&amp;i);(i-0x6E)?o&lt;_+1,O&gt;().L():o&lt;I,_&gt;().L();}};template&lt;&gt;template&lt;
int _&gt;struct o&lt;_, _&gt;{void L(){printf(&quot;=%d!\n&quot;,_);}};int main(){o&lt;0,0x64&gt;().L();}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2442717</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2442717</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Sat, 14 Feb 2015 17:04:28 GMT</pubDate></item><item><title><![CDATA[Reply to Zahlenratespiel von Stroustrup on Sat, 14 Feb 2015 17:33:48 GMT]]></title><description><![CDATA[<p>omfg. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44d.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_up"
      title=":+1:"
      alt="👍"
    /> hib mir ne Stunde dann steig ich vielleicht durch <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f62e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_open_mouth"
      title=":open_mouth:"
      alt="😮"
    /></p>
<p>// wenn nicht <code>'n'</code> ? ne, sorry, das ist ja nur noch gemein <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f60b.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_savoring_food"
      title=":yum:"
      alt="😋"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2442720</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2442720</guid><dc:creator><![CDATA[Swordfish]]></dc:creator><pubDate>Sat, 14 Feb 2015 17:33:48 GMT</pubDate></item><item><title><![CDATA[Reply to Zahlenratespiel von Stroustrup on Sat, 14 Feb 2015 17:30:48 GMT]]></title><description><![CDATA[<p><img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f62e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_open_mouth"
      title=":open_mouth:"
      alt="😮"
    /> Ich dachte zuerst wir sind im Taschenrechner-Thread und SeppJ hätte was erstaunliches erbracht.</p>
<p>Einfachere Version, Fehlerfreiheit nicht garantiert:</p>
<pre><code>#include &lt;cstdio&gt;

template&lt;int I,int O&gt;
void L() {
	const int _=(I+O) &gt;&gt; 1;

	if (I == O)
		return (void)printf(&quot;=%d!\n&quot;, _);

	printf(&quot;&gt;%d?&quot;, _);
	char i;
	scanf(&quot; %c&quot;, &amp;i);

	if (i != 'n')
		return L&lt;_+1,O&gt;();

	L&lt;I,_&gt;();
}

int main() {
	L&lt;0,100&gt;();
}
</code></pre>
<p>... und schon merkt man wie kaputt der Code tatsächlich ist.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2442722</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2442722</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Sat, 14 Feb 2015 17:30:48 GMT</pubDate></item><item><title><![CDATA[Reply to Zahlenratespiel von Stroustrup on Sat, 14 Feb 2015 17:35:54 GMT]]></title><description><![CDATA[<p>Arcoth schrieb:</p>
<blockquote>
<p>Einfachere Version, [...]</p>
</blockquote>
<p>Tja, wenns darum ginge ...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2442723</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2442723</guid><dc:creator><![CDATA[Swordfish]]></dc:creator><pubDate>Sat, 14 Feb 2015 17:35:54 GMT</pubDate></item><item><title><![CDATA[Reply to Zahlenratespiel von Stroustrup on Sat, 14 Feb 2015 17:37:21 GMT]]></title><description><![CDATA[<p>Swordfish schrieb:</p>
<blockquote>
<p>Arcoth schrieb:</p>
<blockquote>
<p>Einfachere Version, [...]</p>
</blockquote>
<p>Tja, wenns darum ginge ...</p>
</blockquote>
<p><img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /> Dir ist schon klar dass das einfach SeppJ's code ist, aber auseinandergenommen und mit Leerraum versehen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2442724</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2442724</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Sat, 14 Feb 2015 17:37:21 GMT</pubDate></item><item><title><![CDATA[Reply to Zahlenratespiel von Stroustrup on Sat, 14 Feb 2015 17:48:28 GMT]]></title><description><![CDATA[<p>No-na?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2442728</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2442728</guid><dc:creator><![CDATA[Swordfish]]></dc:creator><pubDate>Sat, 14 Feb 2015 17:48:28 GMT</pubDate></item><item><title><![CDATA[Reply to Zahlenratespiel von Stroustrup on Sat, 14 Feb 2015 21:02:58 GMT]]></title><description><![CDATA[<p>Hier ist eine verständliche Version:<br />
<a href="https://www.c-plusplus.net/forum/p2189560#2189560">https://www.c-plusplus.net/forum/p2189560#2189560</a></p>
<p>PS: Im Gegensatz zu diesen neumodischen Varianten mit den Schleifen (wer braucht die schon, wenn doch Templates Turing-vollständig sind :p ), macht meine Version genau das gleiche, was der Threadersteller im Originalbeitrag gezeigt hat (also eine ewig lange if-Kaskade) und sollte somit auch ungefähr den gleichen Maschinencode produzieren. Es ist bloß ein bisschen kompakter aufgeschrieben.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2442738</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2442738</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Sat, 14 Feb 2015 21:02:58 GMT</pubDate></item><item><title><![CDATA[Reply to Zahlenratespiel von Stroustrup on Sun, 15 Feb 2015 15:40:04 GMT]]></title><description><![CDATA[<p>Oh man, ich bekomme es nicht gebacken. Was ich bis jetzt versucht habe:</p>
<pre><code>#include &quot;std_lib_facilities.h&quot;     // vorgefertigte Datei für das Buch

int main() 				            // C++-Programme beginnen mit der Ausführung von main
{

    char qst = 'n';
    int high = 100;
    int low = 1;
    int middle = 50;

    cout &lt;&lt; &quot;Ist die Zahl kleiner oder gleich &quot; &lt;&lt; middle &lt;&lt; &quot; ? (j,n)\n&quot;;
    while (cin &gt;&gt; qst)
    {

        if (qst == 'j')
        {
            high = middle;
            middle = middle/2;
            cout &lt;&lt; &quot;high= &quot; &lt;&lt; high &lt;&lt; endl;
            cout &lt;&lt; &quot;middle= &quot; &lt;&lt; middle &lt;&lt; endl;
            cout &lt;&lt; &quot;Ist die Zahl kleiner oder gleich &quot; &lt;&lt; middle &lt;&lt; &quot; ? (j,n)&quot; &lt;&lt; endl;
        }
        else if (qst == 'n')
        {
            low = middle;
            middle = ((high-low)/2)+low;
            cout &lt;&lt; &quot;high= &quot; &lt;&lt; high &lt;&lt; endl;
            cout &lt;&lt; &quot;middle= &quot; &lt;&lt; middle &lt;&lt; endl;
            cout &lt;&lt; &quot;low= &quot; &lt;&lt; low &lt;&lt; endl;
            cout &lt;&lt; &quot;Ist die Zahl kleiner oder gleich &quot; &lt;&lt; middle &lt;&lt; &quot; ? (j,n)&quot; &lt;&lt; endl;
        }
        else
            cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;
    }
    return 0;
}
</code></pre>
<p>Ich komme nicht dahinter wie die Schleife merken soll wann die richtige Zahl<br />
erraten wurde. Die Ausgabe von high/middle/low hab ich nur gemacht mit ich ne Ahnung davon bekomme was in der Schleife gerade passiert.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2442810</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2442810</guid><dc:creator><![CDATA[pauledd]]></dc:creator><pubDate>Sun, 15 Feb 2015 15:40:04 GMT</pubDate></item><item><title><![CDATA[Reply to Zahlenratespiel von Stroustrup on Sun, 15 Feb 2015 17:44:50 GMT]]></title><description><![CDATA[<p>Nach diesem Verfahren suchst du die Zahl auf einem bestimmten Intervall.<br />
Wann ist die Suche beendet? Wenn das Intervall nur noch eine Zahl enthaelt - naemlich die gesuchte.<br />
Danach solltest du deine Schleife ausrichten.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2442849</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2442849</guid><dc:creator><![CDATA[Hyde++ 0]]></dc:creator><pubDate>Sun, 15 Feb 2015 17:44:50 GMT</pubDate></item><item><title><![CDATA[Reply to Zahlenratespiel von Stroustrup on Mon, 16 Feb 2015 08:47:00 GMT]]></title><description><![CDATA[<p>So, jetzt funktioniert zumindest schon mal alles bis das Ergebnis erreicht wurde.<br />
Was ist nun aber wenn die Zahl 50 oder 25 oder 37 ist. Dann wäre die Sache ja nach 1,2 und drei Fragen zu ende. Ist die Aufgabe so gemeint das beim erscheinen der richtigen Zahl die Sache erledigt ist oder soll das Programm explizit herausfinden das die 37 nun das Ergebnis ist und keine Weiteren Fragen mehr stellen <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /></p>
<pre><code>#include &quot;std_lib_facilities.h&quot;     // vorgefertigte Datei für das Buch

int main()                          // C++-Programme beginnen mit der Ausführung von main
{

    char qst = 'n';
    int high = 100;
    int low = 1;
    int middle = 50;

    cout &lt;&lt; &quot;kleiner oder gleich &quot; &lt;&lt; middle &lt;&lt; &quot; ? (j,n)\n&quot;;
    while (cin &gt;&gt; qst)
    {

        if (qst == 'j')
        {
            high = (high+low)/2;
            middle =((high-low)/2)+low;
        }
        else if (qst == 'n')
        {
            low = (low+high)/2;
            middle = ((high-low)/2)+low;
        }
        else
            cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;
        cout &lt;&lt; &quot;kleiner oder gleich &quot; &lt;&lt; middle &lt;&lt; &quot; ? (j,n)&quot; &lt;&lt; endl;
    }
    return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2442920</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2442920</guid><dc:creator><![CDATA[pauledd]]></dc:creator><pubDate>Mon, 16 Feb 2015 08:47:00 GMT</pubDate></item><item><title><![CDATA[Reply to Zahlenratespiel von Stroustrup on Mon, 16 Feb 2015 08:56:10 GMT]]></title><description><![CDATA[<p>Hyde++ schrieb:</p>
<blockquote>
<p>...Wenn das Intervall nur noch eine Zahl enthaelt - naemlich die gesuchte.<br />
Danach solltest du deine Schleife ausrichten.</p>
</blockquote>
<p>Versteh ich nicht, die Zahl wird solange aus high und low &quot;gemittelt&quot; bis es die Zahl ist. Bei 50 also schon nach der ersten Frage. Wenn ich aber nur ja, nein Fragen darf wie soll ich dann der Schleife mitteilen nicht mehr zu fragen, falls das überhaupt die Aufgabenstellung ist.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2442921</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2442921</guid><dc:creator><![CDATA[pauledd]]></dc:creator><pubDate>Mon, 16 Feb 2015 08:56:10 GMT</pubDate></item><item><title><![CDATA[Reply to Zahlenratespiel von Stroustrup on Mon, 16 Feb 2015 09:55:32 GMT]]></title><description><![CDATA[<p>pauledd schrieb:</p>
<blockquote>
<p>Versteh ich nicht, die Zahl wird solange aus high und low &quot;gemittelt&quot; bis es die Zahl ist. Bei 50 also schon nach der ersten Frage.</p>
</blockquote>
<p>Ja und? Die Frage ist doch &quot;kleiner oder gleich&quot;. Und nicht &quot;kleiner (k), gleich (e) oder größer (g)&quot;. Das Programm kann da noch nicht wissen, dass die Zahl erraten ist.</p>
<p>pauledd schrieb:</p>
<blockquote>
<p>Wenn ich aber nur ja, nein Fragen darf wie soll ich dann der Schleife mitteilen nicht mehr zu fragen, falls das überhaupt die Aufgabenstellung ist.</p>
</blockquote>
<p>Dann ist deine Abbruchbedingung nicht richtig.<br />
Lies dir die Antworten mal richtig durch:</p>
<p>Hyde++ schrieb:</p>
<blockquote>
<p>...Wenn das Intervall nur noch eine Zahl enthaelt - naemlich die gesuchte.<br />
Danach solltest du deine Schleife ausrichten.</p>
</blockquote>
<p>DirkB schrieb:</p>
<blockquote>
<p>5. ist obere und untere Grenze gleich? Nein, dann weiter bei 2.</p>
</blockquote>
<p>Da steht nichts von: <em>Solange du eine gültige Eingabe bekommst, .....</em></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2442925</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2442925</guid><dc:creator><![CDATA[DirkB]]></dc:creator><pubDate>Mon, 16 Feb 2015 09:55:32 GMT</pubDate></item><item><title><![CDATA[Reply to Zahlenratespiel von Stroustrup on Mon, 16 Feb 2015 10:35:00 GMT]]></title><description><![CDATA[<p>DirkB schrieb:</p>
<blockquote>
<p>Dann ist deine Abbruchbedingung nicht richtig.<br />
Lies dir die Antworten mal richtig durch:</p>
<p>Hyde++ schrieb:</p>
<blockquote>
<p>...Wenn das Intervall nur noch eine Zahl enthaelt - naemlich die gesuchte.<br />
Danach solltest du deine Schleife ausrichten.</p>
</blockquote>
<p>DirkB schrieb:</p>
<blockquote>
<p>5. ist obere und untere Grenze gleich? Nein, dann weiter bei 2.</p>
</blockquote>
<p>Da steht nichts von: <em>Solange du eine gültige Eingabe bekommst, .....</em></p>
</blockquote>
<p>Dann ist bei mir leider wieder alles falsch. Die obere/untere Grenze wird bei mir nie gleich.<br />
Ich fang nochmal von vorne an aber ich zweifel langsam was mein Abstraktionsvermögen angeht da die Aufgabe nun eigentlich Kindergarten ist und ich nach 3 Tagen immer noch nicht dahinter gestiegen bin.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2442933</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2442933</guid><dc:creator><![CDATA[pauledd]]></dc:creator><pubDate>Mon, 16 Feb 2015 10:35:00 GMT</pubDate></item><item><title><![CDATA[Reply to Zahlenratespiel von Stroustrup on Mon, 16 Feb 2015 17:03:00 GMT]]></title><description><![CDATA[<blockquote>
<p>Dann ist bei mir leider wieder alles falsch. Die obere/untere Grenze wird bei mir nie gleich.</p>
</blockquote>
<p>Korrekt. Dazu nochmal genau ueberlegen, welche Ja/Nein-Fragen du stellst:<br />
&quot;Ist die Zahl kleiner oder gleich x?&quot; Wenn du hier mit Nein anwortest, setzt du das untere Limit auf x.<br />
Das bedeutet, die kleinste Zahl die deine zahl annehmen koennte, waere x.<br />
Aber die Frage war kleiner ODER GLEICH. Wenn du hier mit nein antwortest, sagst du also, dass deine Zahl groesser als x ist. Du musst hier also low = middle + 1 setzen.</p>
<p>Edit:<br />
Lass dich nicht entmutigen! Um Programmieren zu lernen braucht man nun mal Zeit. Wenn C++ deine erste Sprache ist, wird es umso schwieriger. Ich habe auch mit c++ angefangen und habe Monate gebraucht, um ueberhaupt ein einfaches Programm zu schreiben, dass ohne Fehler kompiliert.<br />
An der Aufgabe, die Binaerdarstellung einer Zahl zu erzeugen, bin ich voellig gescheitert.</p>
<p>Wenn dich das interessiert, bleib dran. Am besten ist es, wenn du irgendwann mal ein echtes Projekt hast, dass du umsetzen kannst. Ich bin Elektrotechniker, hatte nur absolut rudimentaere Programmierkurse in Java und schreibe momentan meine Bachelorarbeit in c++. Das ist zwar kein Grossprojekt, aber es ist trotzdem ein schoenes Gefuehl, mal die Sachen, die man ueber die Jahre so liesst, wirklich anzuwenden. Hier im Forum mitlesen hilft auch schon einiges, da man manche Sachen, die man besser vermeiden sollte, die man besser nutzen sollte usw. unbewusst aufnimmt.</p>
<p>Zum abstrakten Denken: ueben, ueben, ueben. Das kommt leider nicht von irgendwo her, ich fand es als Programmierneuling eine sehr eigenartige Denkweise.<br />
Heute helfe ich ab und zu meiner Schwester in Programmierhausaufgaben der Uni (meistens Python) und bin erstaunt, welche (vermeintlich einfachste) Konzepte ihr nicht klar sind. Das ist natuerlich nicht ihr Fehler, man neigt nur dazu, viel zu lernen und zu verinnerlichen, dass man vergisst, welche Probleme Schwierigkeiten bereiten.<br />
Also: Kopf hoch!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2442943</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2442943</guid><dc:creator><![CDATA[Hyde++ 0]]></dc:creator><pubDate>Mon, 16 Feb 2015 17:03:00 GMT</pubDate></item><item><title><![CDATA[Reply to Zahlenratespiel von Stroustrup on Mon, 16 Feb 2015 16:58:24 GMT]]></title><description><![CDATA[<p>Hyde++ schrieb:</p>
<blockquote>
<p>Lass dich nicht entmutigen!</p>
</blockquote>
<p>Nein, und danke <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>Hyde++ schrieb:</p>
<blockquote>
<p>Zum abstrakten Denken: ueben, ueben, ueben.</p>
</blockquote>
<p>Mach ich, zum Glück kann ich mir Zeit lassen und kann das einfach mal beiseite legen und dann nochmal neu rann. Aber bis jetzt hat das noch nicht &quot;klick&quot; gemacht.</p>
<p>Hyde++ schrieb:</p>
<blockquote>
<p>Du musst hier also low = middle + 1 setzen.</p>
</blockquote>
<p>Ok, das verstehe ich glaube ich. Weil ja im &quot;ja&quot; Fall 1-50 schon abgedekt<br />
ist muss es dann bei &quot;nein&quot; 51-100 sein.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2442996</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2442996</guid><dc:creator><![CDATA[pauledd]]></dc:creator><pubDate>Mon, 16 Feb 2015 16:58:24 GMT</pubDate></item><item><title><![CDATA[Reply to Zahlenratespiel von Stroustrup on Mon, 16 Feb 2015 17:25:30 GMT]]></title><description><![CDATA[<p>Ok ich glaube jetzt klappts. Das &quot;+1&quot; hat geholfen. Jetzt werden Unter/Obergrenze auch mal gleich.</p>
<pre><code>#include &quot;std_lib_facilities.h&quot;     // vorgefertigte Datei für das Buch

int main()                          // C++-Programme beginnen mit der Ausführung von main
{

    char qst = 'n';
    int high = 100;
    int low = 1;
    int middle = 50;
    int runde =0;
    cout &lt;&lt; &quot;kleiner oder gleich &quot; &lt;&lt; middle &lt;&lt; &quot; ? (j,n)\n&quot;;
    while (cin &gt;&gt; qst)
    {
        runde = runde+1;
        cout &lt;&lt; &quot;Frage= &quot; &lt;&lt; runde &lt;&lt; endl;
        if (qst == 'j')
        {
            high = (high+low)/2;
            middle =((high-low)/2)+low;
        }
        else if (qst == 'n')
        {
            low = ((low+high)/2)+1;
            middle = ((high-low)/2)+low;
        }
        else
            cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;

        cout &lt;&lt; &quot;high= &quot; &lt;&lt; high &lt;&lt; endl;
        cout &lt;&lt; &quot;middle= &quot; &lt;&lt; middle &lt;&lt; endl;
        cout &lt;&lt; &quot;low= &quot; &lt;&lt; low &lt;&lt; endl;
        cout &lt;&lt; &quot;Runde= &quot; &lt;&lt; runde &lt;&lt; endl;
        if (low != high)
            cout &lt;&lt; &quot;kleiner oder gleich &quot; &lt;&lt; middle &lt;&lt; &quot; ? (j,n)&quot; &lt;&lt; endl;
        else
            cout &lt;&lt; &quot;Die Zahl ist &quot; &lt;&lt; middle &lt;&lt; endl;
    }

    return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2443001</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2443001</guid><dc:creator><![CDATA[pauledd]]></dc:creator><pubDate>Mon, 16 Feb 2015 17:25:30 GMT</pubDate></item><item><title><![CDATA[Reply to Zahlenratespiel von Stroustrup on Mon, 16 Feb 2015 17:37:56 GMT]]></title><description><![CDATA[<p>Ich weiß nicht *kopfkratz* ist <code>cin &gt;&gt; qst</code> wirklich eine gute Abbruchbedingung?<br />
Warum überhaupt eine kopfgesteuerte <code>while(...){}</code> -Schleife wenn die Werte für eine sinnvolle Abbruchbedingung erst <em>nach</em> Ausführung des Schleifenkörpers feststehen? <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/27a1.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--right_arrow"
      title=":arrow_right:"
      alt="➡"
    /> <code>do{}while(...)</code></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2443002</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2443002</guid><dc:creator><![CDATA[Swordfish]]></dc:creator><pubDate>Mon, 16 Feb 2015 17:37:56 GMT</pubDate></item><item><title><![CDATA[Reply to Zahlenratespiel von Stroustrup on Mon, 16 Feb 2015 17:37:05 GMT]]></title><description><![CDATA[<p>Swordfish schrieb:</p>
<blockquote>
<p>Ich weiß nicht *kopfkratz* ist <code>cin &gt;&gt; qst</code> wirklich eine gute Schleifenbedingung?</p>
</blockquote>
<p>Weiß ich leider auch nicht aber die Schleife wurde in dem Buch bis zu der Übung schon öfters so zur Demonstration verwendet, oder soll das ein &quot;Wink&quot; in Richtung Optimierung sein? <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>
]]></description><link>https://www.c-plusplus.net/forum/post/2443004</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2443004</guid><dc:creator><![CDATA[pauledd]]></dc:creator><pubDate>Mon, 16 Feb 2015 17:37:05 GMT</pubDate></item></channel></rss>