<?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[problem mit test von variable auf inhalt]]></title><description><![CDATA[<p>hallo,<br />
ich habe hier ein kleines programmgeschrieben, das den größten gemeinsamen teiler 2er zahlen findet.<br />
dazu habe ich etwas eingebaut was testen (soll) ob die eingaben wirklich zahlen sind... geht aber nicht <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=":/"
      alt="😕"
    /><br />
das programm springt von dem if in zahl_pruefen immer zu else! warum? bzw. springt auch, wenn eine zahl eingegeben wurde in else?! wie mach ich das das das programm aus else wieder an den anfang von main geht?<br />
hier der gesamte code</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;ctype.h&gt;

using namespace std;

int main();

int zahl_pruefen( int testzahl ){

    if (isdigit ( testzahl ) ) {
        cout &lt;&lt; &quot;Eingabe ist eine Zahl.\n&quot;;
    }
    else {
        cout&lt;&lt; &quot;Bitte eine Zahl eingeben!\n&quot;;

    }
}
//#####################################------&gt; _MAIN_ &lt;------#########################
main()
{
    cout &lt;&lt; &quot;---------------------------------- \nkomischer algorytmus \n----------------------------------\n&quot;;
    int a;
    int b;
    int c;
    cout &lt;&lt; &quot;bitte a angeben: \n&quot;;
        cin &gt;&gt; a;
zahl_pruefen (a);
    cout &lt;&lt; &quot;bitte b angeben: \n&quot;;
        cin &gt;&gt; b;
zahl_pruefen (b);
    int x = a;
    int y = b;
    // begin des algorytmus
    while( a != b ){

        if ( a &lt; b ){
            c = a;
            a = b;
            b = c;
        }
        if ( a &gt; b ){
            c = a - b;
            a = b;
            b = c;
        }
    }

    cout &lt;&lt; &quot;Der ggt der Zahlen &quot; &lt;&lt; x &lt;&lt; &quot; und &quot; &lt;&lt; y &lt;&lt; &quot; ist &quot; &lt;&lt; a &lt;&lt; &quot;! \n----------------------------------\n&quot;;

    return 0;
}
</code></pre>
<p>viele grüße und danke für eure antworten!</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/275228/problem-mit-test-von-variable-auf-inhalt</link><generator>RSS for Node</generator><lastBuildDate>Thu, 27 Aug 2026 02:53:41 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/275228.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 11 Oct 2010 17:24:05 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to problem mit test von variable auf inhalt on Mon, 11 Oct 2010 17:25:53 GMT]]></title><description><![CDATA[<p>hallo,<br />
ich habe hier ein kleines programmgeschrieben, das den größten gemeinsamen teiler 2er zahlen findet.<br />
dazu habe ich etwas eingebaut was testen (soll) ob die eingaben wirklich zahlen sind... geht aber nicht <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=":/"
      alt="😕"
    /><br />
das programm springt von dem if in zahl_pruefen immer zu else! warum? bzw. springt auch, wenn eine zahl eingegeben wurde in else?! wie mach ich das das das programm aus else wieder an den anfang von main geht?<br />
hier der gesamte code</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;ctype.h&gt;

using namespace std;

int main();

int zahl_pruefen( int testzahl ){

    if (isdigit ( testzahl ) ) {
        cout &lt;&lt; &quot;Eingabe ist eine Zahl.\n&quot;;
    }
    else {
        cout&lt;&lt; &quot;Bitte eine Zahl eingeben!\n&quot;;

    }
}
//#####################################------&gt; _MAIN_ &lt;------#########################
main()
{
    cout &lt;&lt; &quot;---------------------------------- \nkomischer algorytmus \n----------------------------------\n&quot;;
    int a;
    int b;
    int c;
    cout &lt;&lt; &quot;bitte a angeben: \n&quot;;
        cin &gt;&gt; a;
zahl_pruefen (a);
    cout &lt;&lt; &quot;bitte b angeben: \n&quot;;
        cin &gt;&gt; b;
zahl_pruefen (b);
    int x = a;
    int y = b;
    // begin des algorytmus
    while( a != b ){

        if ( a &lt; b ){
            c = a;
            a = b;
            b = c;
        }
        if ( a &gt; b ){
            c = a - b;
            a = b;
            b = c;
        }
    }

    cout &lt;&lt; &quot;Der ggt der Zahlen &quot; &lt;&lt; x &lt;&lt; &quot; und &quot; &lt;&lt; y &lt;&lt; &quot; ist &quot; &lt;&lt; a &lt;&lt; &quot;! \n----------------------------------\n&quot;;

    return 0;
}
</code></pre>
<p>viele grüße und danke für eure antworten!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1963998</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1963998</guid><dc:creator><![CDATA[c++ freak 0]]></dc:creator><pubDate>Mon, 11 Oct 2010 17:25:53 GMT</pubDate></item><item><title><![CDATA[Reply to problem mit test von variable auf inhalt on Mon, 11 Oct 2010 17:39:34 GMT]]></title><description><![CDATA[<p>das programm ist total falsch</p>
<p>von den syntax fehlern mal abgesehen geh ich jetzt nur mal auf den ablauf ein:</p>
<p>du gibst eine zahl ein, speicherst sie in einem int und übergibst diesen an eine methode. dort prüfst du diese zahl ob sie eine zahl ist, aber isdigit erwartet einen int oder char, bin mir nicht ganz sicher, ist aber egal weil die ineinander konvertierbar sind, jedenfalls prüft diese methode nur EINE zahl bzw eineen BUCHSTABEN, daher wird gar nicht deine eingebene zahl geprüft. da dabei eh vllt osgar merhere buffer overflows passieren, quetscht die isdigit funktion deine eingegebene zahl in einen char, die wahrscheinlichkeit eine der 10 zahlen zu treffen ist recht klein, daher ist das ergebnis bei dir dass der sofort in die else abzweigung springt nachvollziehbar</p>
<p>dazu vergisst du auch einen rückgab wert</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1964005</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1964005</guid><dc:creator><![CDATA[Skym0sh0]]></dc:creator><pubDate>Mon, 11 Oct 2010 17:39:34 GMT</pubDate></item><item><title><![CDATA[Reply to problem mit test von variable auf inhalt on Mon, 11 Oct 2010 17:46:01 GMT]]></title><description><![CDATA[<p>Und die Zeile 6 ist auch Fragwürdig und demzufolge auch die Zeile 20.<br />
<strong>&quot;default-int&quot; wird von C++ nicht unterstützt.</strong></p>
<p>Welcher Compiler lässt sowas zu?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1964009</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1964009</guid><dc:creator><![CDATA[Doug_HH]]></dc:creator><pubDate>Mon, 11 Oct 2010 17:46:01 GMT</pubDate></item><item><title><![CDATA[Reply to problem mit test von variable auf inhalt on Mon, 11 Oct 2010 17:52:19 GMT]]></title><description><![CDATA[<p>cin &gt;&gt; b</p>
<p>liest eine Zahl ein. Dabei hat die Wandlung von Zeichen die die Zahl repräsentiert zu einem int mit dem entsprechenden Wert schon stattgefunden. Da muss man nicht noch isdigit benutzen. Du verwendest isdigit hier falsch. isdigit macht etwas anderes.</p>
<p>Du solltest trotzdem überprüfen, ob das mit dem Einlesen richtig geklappt hat. Dazu hat so ein Stream einen Zustand, den man abfragen kann.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1964018</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1964018</guid><dc:creator><![CDATA[lalule]]></dc:creator><pubDate>Mon, 11 Oct 2010 17:52:19 GMT</pubDate></item><item><title><![CDATA[Reply to problem mit test von variable auf inhalt on Mon, 11 Oct 2010 18:38:31 GMT]]></title><description><![CDATA[<p>hm danke erstmal für eure hilfe.<br />
wie teste ich denn ob die eingabe eine zahl ist am besten?<br />
habe die anderen fehler behoben denke ich oder was macht mein programm so falsch?</p>
<pre><code class="language-cpp">int zahl_pruefen( int testzahl ){

    if (isdigit ( testzahl ) ) {
        cout &lt;&lt; &quot;Eingabe ist eine Zahl.\n&quot;;
        return 0;
    }
    else {
        cout&lt;&lt; &quot;Bitte eine Zahl eingeben!\n&quot;;
        return 0;

    }
}
</code></pre>
<p>zu der frage mit dem compiler:<br />
man gibt g++ algorytmus.cpp in das terminal ein. denke der compiler heißt g++?(ich nutze ubuntu)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1964044</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1964044</guid><dc:creator><![CDATA[c++ freak 0]]></dc:creator><pubDate>Mon, 11 Oct 2010 18:38:31 GMT</pubDate></item><item><title><![CDATA[Reply to problem mit test von variable auf inhalt on Mon, 11 Oct 2010 19:56:51 GMT]]></title><description><![CDATA[<p>hiho, so z.B.:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
using namespace std;

int main()
{
    int x = 0;

    cin &gt;&gt; x;

    if(cin.fail() == true)
    {
        cout &lt;&lt; &quot;fail, no int entered&quot;;
    }

    /*
        Weitermachen kannst du danach z.B. so.
        Es kann allerdings sein, dass sync() bei dir nicht funktioniert,
        dann gibt es andere Möglichkeiten um weiter zu machen.

    */
    cin.clear(); // Fehlerflags zurücksetzen.
    cin.sync(); // Zeichen im Puffer verwerfen.
    cin &gt;&gt; x; // nächste Eingabe geht wieder.

    return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1964073</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1964073</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Mon, 11 Oct 2010 19:56:51 GMT</pubDate></item><item><title><![CDATA[Reply to problem mit test von variable auf inhalt on Mon, 11 Oct 2010 20:14:03 GMT]]></title><description><![CDATA[<p>ich bin ein stückchen weiter gekommen <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="🙂"
    /><br />
mein jetziger code ist:</p>
<pre><code class="language-cpp">void zahl_pruefen( int testzahl ){
    if (isalpha ( testzahl ) == 0 ) {
        cout &lt;&lt; &quot;--\n&quot;;
    }
    if (isalpha(testzahl) != 0 ) {
        cout &lt;&lt; &quot;Bitte eine Zahl eingeben!!!\n&quot;;
    }

    }
}
</code></pre>
<p>jetzt wird wenigstens bei einer zahl erkannt dass es sich um eine zahl handelt und das programm lässt else aus und geht normal weiter. wenn ich aber einen buchstaben eingebe dann kommt nach &quot;bitte b angeben:&quot; folgender fehler: Segmentation fault und das programm bricht ab<br />
EDIT: @Dweb das probier ich mal danke!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1964079</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1964079</guid><dc:creator><![CDATA[c++ freak 0]]></dc:creator><pubDate>Mon, 11 Oct 2010 20:14:03 GMT</pubDate></item><item><title><![CDATA[Reply to problem mit test von variable auf inhalt on Mon, 11 Oct 2010 21:30:44 GMT]]></title><description><![CDATA[<p>Du hast da glaub noch was falsch verstanden.</p>
<p>Wenn du bei</p>
<pre><code class="language-cpp">int a;
</code></pre>
<p>einen Buchstaben eingibst, dann schlägt das fehl, und das cin-Objekt ist in einem Fehlerzustand (cin.fail() ist true), und die Variable x wird nicht verändert und behält ihren ursprünglichen Wert.</p>
<p>Dementsprechend trifft (fast) immer das</p>
<pre><code class="language-cpp">if(isalpha ( testzahl ) == 0)
</code></pre>
<p>zu, da du die Variable x (wie ich oben beschrieben habe) ja gar nicht verändert hast,<br />
und die Variable x somit noch den zufälligen Wert hat, der zufällig an ihrer Speicherstelle steht.</p>
<p>PS:<br />
- Es ist eher unwahrscheinlich, dass an dieser Speicherstelle zufällig ein ASCII-Wert steht der zu einem Buchstaben gehört,<br />
deswegen hatte ich oben &quot;(fast) immer&quot; geschrieben.</p>
<p>- In meinem obigen Post siehst du eine Möglichkeit, wie man den Fehlerzustand des cin-Objekts beheben kann.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1964099</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1964099</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Mon, 11 Oct 2010 21:30:44 GMT</pubDate></item><item><title><![CDATA[Reply to problem mit test von variable auf inhalt on Tue, 12 Oct 2010 06:37:48 GMT]]></title><description><![CDATA[<p>danke dweb! das scheint ja zu funktioniern!<br />
nur habe ich jetzt das nächste problem <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=":/"
      alt="😕"
    /> habe eine schleife eingebaut die jetzt leider unendlich läuft...<br />
hier die funktion zahl_pruefen:</p>
<pre><code class="language-cpp">bool zahl_pruefen( int testzahl ){
    if (cin.fail() == true){
    cout &lt;&lt; &quot;Bitte eine Zahl eingeben!\n&quot;;
    cin.clear();
    cin.sync();
    return false;
    }
    else {return true;}
}
</code></pre>
<p>und der abfrage teil aus main():</p>
<pre><code class="language-cpp">bool pruefen = false;
//abfrage von den beiden zu prüfenden zahlen
while (pruefen == false){
    cout &lt;&lt; &quot;bitte a angeben: \n&quot;;
        cin &gt;&gt; a;
pruefen = zahl_pruefen (a);
}
</code></pre>
<p>der wartet jetzt wenn ich einmal eine falsche antwort eingegeben habe nichtmehr auf eine eingabe bei cin &gt;&gt; a;! (geht auch nicht wenn ich das ganze in main schreibe) da hab ich jetzt überhaupt keine ahnung woran das liegt! normal wartet das programm doch immer auf eine einfabe?!<br />
PS: ich habe meinen letzten beitrag geschrieben als du deinen geschrieben hast <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="😉"
    /> daher ist deiner noch nicht berücksichtigt gewesen</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1964149</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1964149</guid><dc:creator><![CDATA[c++ freak 0]]></dc:creator><pubDate>Tue, 12 Oct 2010 06:37:48 GMT</pubDate></item><item><title><![CDATA[Reply to problem mit test von variable auf inhalt on Tue, 12 Oct 2010 07:19:20 GMT]]></title><description><![CDATA[<p>Ich sehe da keinen Fehler, das funktioniert bei tadellos so wie du es wolltest,<br />
egal ob ich<br />
- aslfkääa´ß+.0943 // Fehler<br />
- abc // Fehler<br />
- x // Fehler<br />
- 123 // ok<br />
eingebe. <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>
<p>Wenn der bei dir nicht mehr auf eine Eingabe wartet, wenn du 1x etwas falsches eingibst, dann funktioniert bei deinem System wohl das cin.sync() nicht.<br />
Wenn dies der Fall ist, dann gibt es andere Möglichkeiten:</p>
<p><a href="http://c-plusplus.net/forum/viewtopic-var-t-is-111042.html" rel="nofollow">http://c-plusplus.net/forum/viewtopic-var-t-is-111042.html</a></p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
using namespace std;

bool zahl_pruefen( int testzahl )
{
    if (cin.fail() == true)
    {
        cout &lt;&lt; &quot;Bitte eine Zahl eingeben!\n&quot;;
        cin.clear();
        cin.sync();
        return false;
    }

    return true;
}

int main()
{
    int a;
    bool pruefen = false;

    while(pruefen == false)
    {
        cout &lt;&lt; &quot;bitte a angeben: \n&quot;;
        cin &gt;&gt; a;
        pruefen = zahl_pruefen (a);
    }

    return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1964161</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1964161</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Tue, 12 Oct 2010 07:19:20 GMT</pubDate></item><item><title><![CDATA[Reply to problem mit test von variable auf inhalt on Tue, 12 Oct 2010 16:16:42 GMT]]></title><description><![CDATA[<p>ha super! jetzt gehts einwandfrei!<br />
hier der ganze code falls ihn nochmal jemand sehen will</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;ctype.h&gt;
#include &lt;cstdlib&gt;
#include &lt;stdio.h&gt; 

using namespace std;

void wait ()
{
    // Setzte Puffergröße auf 0 (=&gt; leere Puffer)
    setvbuf(stdin, NULL, _IONBF, 0);
    // Setze Puffergröße wieder auf die ursprüngliche Größe zurück
    setvbuf(stdin, NULL, _IOFBF, BUFSIZ);
    // Füge alle eingelesenen Zeichen in den Puffer bis ein Enter gedrückt wird
    // getchar() liefert dann das erste Zeichen aus dem Puffer zurück, welches wir aber ignorieren (interessiert uns ja nicht)
    getchar();
} 

bool zahl_pruefen( int testzahl ){
    if (cin.fail() == true){
    cout &lt;&lt; &quot;----------------------------------\nBitte eine Zahl eingeben!\n----------------------------------\n-------------NOCHMAL!-------------\n&quot;;
    cin.clear();
    return false;
    }
    else {return true;}
}
//#####################################------&gt; _MAIN_ &lt;------#########################
main()
{
    cout &lt;&lt; &quot;---------------------------------- \nkomischer algorytmus \n----------------------------------\n&quot;;
    int a;
    int b;
    int c;
    bool pruefen = false;
    int i = 0;

    cout &lt;&lt; &quot;[ENTER] zum Starten!\n&quot;;
//abfrage von den beiden zu prüfenden zahlen

while (pruefen == false ){

        wait();

    cout &lt;&lt; &quot;Bitte a angeben: \n&quot;;

        cin &gt;&gt; a;
    pruefen = zahl_pruefen (a);
    i++;
if (pruefen == false){
    cout &lt;&lt; &quot;Das ist die &quot; &lt;&lt; i &lt;&lt; &quot;. falsche Eingabe!\n&quot;;
}    
}
    cout &lt;&lt; &quot;a ist &quot; &lt;&lt; a &lt;&lt; &quot; - [ENTER] zum Fortfahren!\n&quot;;
    i = 0; //i zurücksetzten
    pruefen = false;//pruefen wieder auf false setzten damit die zweite schleife überhaupt in gang kommt

while (pruefen == false){
    wait();

        cout &lt;&lt; &quot;Bitte b angeben: \n&quot;;

        cin &gt;&gt; b;
    pruefen = zahl_pruefen (b);
    i++;
if (pruefen == false){
    cout &lt;&lt; &quot;Das ist die &quot; &lt;&lt; i &lt;&lt; &quot;. falsche Eingabe!\n&quot;;
}
}

    int x = a;//zahlen für spätere ausgabe speichern
    int y = b;

    // begin des algorytmus
    while( a != b ){

        if ( a &lt; b ){
            c = a;//a wird dubliziert
            a = b;//a wird b
            b = c;//b wird a bzw. c
        }
        if ( a &gt; b ){
            c = a - b;
            a = b;
            b = c;
        }
    }
    //a ist das Gesuchte!
    cout &lt;&lt; &quot;Der ggt der Zahlen &quot; &lt;&lt; x &lt;&lt; &quot; und &quot; &lt;&lt; y &lt;&lt; &quot; ist &quot; &lt;&lt; a &lt;&lt; &quot;! \n----------------------------------\n&quot;;

    return 0;
}
</code></pre>
<p>danke an alle die gehalfen haben!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1964549</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1964549</guid><dc:creator><![CDATA[c++ freak 0]]></dc:creator><pubDate>Tue, 12 Oct 2010 16:16:42 GMT</pubDate></item><item><title><![CDATA[Reply to problem mit test von variable auf inhalt on Tue, 12 Oct 2010 16:32:34 GMT]]></title><description><![CDATA[<p>jetzt fehlen nurnoch ne ordentliche Einrückung und vor allem standardkoformes C++.<br />
ctype.h und stdio.h sind alte C-Header, die kein gültiges C++ sind (d.h. DEIN Compiler akzeptiert die vermutlich, aber andere Compiler müssen die nicht kennen)<br />
Die entsprechenden C++-Header sind &lt;cctype&gt; und &lt;cstdio&gt;</p>
<p>Die Vergleiche mit true und false sind auch sinnlos - den mit true kannst du gleich weglassen, den mit false durch ein logisches NOT ersetzen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1964557</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1964557</guid><dc:creator><![CDATA[pumuckl]]></dc:creator><pubDate>Tue, 12 Oct 2010 16:32:34 GMT</pubDate></item></channel></rss>