<?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[String überprüfen | Abfrage überspringt bei falscher Eingabe]]></title><description><![CDATA[<p>Ich hab zwei Probleme, und zwar wie überprüfe ich ob der Spieler &quot;ja&quot; oder &quot;nein&quot; in die Konsole eingeben hat um im dem Fall das Programm wiederholen oder es wird abgebrochen?</p>
<p>2. Wenn die Abfrage kommt, dass der Nutzer eine Zahl eingeben solle und stattdessen ein Sonderzeichen oder irgendwas anderes als eine Zahl ist dann überspringt er alle Schleifen und geht bis zur letzten und beendet das Programm? Wie kann ich bei jeder Abfrage sicher gehen, dass er nur die eine Auswahl hat - die Zahleneingabe? [Foto-Link am Ende des Beitrags]</p>
<pre><code>#import &lt;iostream&gt;
#import &lt;windows.h&gt;

int Zahl1, Zahl2, Ergebnis, Exit;
char Operation;

int main()
{
	system(&quot;cls&quot;);
	std::cout &lt;&lt; &quot;Syntax: &lt;Zahl1&gt; + | - | * | / | % &lt;Zahl2&gt; = Ergebnis&quot; &lt;&lt; std::endl &lt;&lt; std::endl;
	Sleep(2*1000);

	do
	{
		std::cout &lt;&lt; &quot;Geben Sie die erste Zahl ein: &quot;;
		std::cin &gt;&gt; Zahl1;
		std::cout &lt;&lt; std::endl;
		std::cout &lt;&lt; &quot;Geben Sie die zweite Zahl ein: &quot;;
		std::cin &gt;&gt; Zahl2;
		std::cout &lt;&lt; std::endl;

		std::cout &lt;&lt; &quot;Geben Sie als nächstes die Rechenoperation ein mit der Sie rechnen wollen: &quot;;
		std::cin &gt;&gt; Operation;
		std::cout &lt;&lt; std::endl;	

		switch(Operation)
		{
			case '+':
				Ergebnis = Zahl1 + Zahl2;
				std::cout &lt;&lt; &quot;Das Ergebnis ist: &quot; &lt;&lt; Ergebnis &lt;&lt; std::endl;
				std::cout &lt;&lt; &quot;Beenden(0), Wiederholen(1): &quot;;
				std::cin &gt;&gt; Exit;
				std::cout &lt;&lt; std::endl;
				break;
			case '-':
				Ergebnis = Zahl1 - Zahl2;
				std::cout &lt;&lt; &quot;Das Ergebnis ist: &quot; &lt;&lt; Ergebnis &lt;&lt; std::endl;
				std::cout &lt;&lt; &quot;Beenden(0), Wiederholen(1): &quot;;
				std::cin &gt;&gt; Exit;
				std::cout &lt;&lt; std::endl;
				break;
			case '*':
				Ergebnis = Zahl1 * Zahl2;
				std::cout &lt;&lt; &quot;Das Ergebnis ist: &quot; &lt;&lt; Ergebnis &lt;&lt; std::endl;
				std::cout &lt;&lt; &quot;Beenden(0), Wiederholen(1): &quot;;
				std::cin &gt;&gt; Exit;
				std::cout &lt;&lt; std::endl;
				break;
			case '/':
				if(Zahl1 || Zahl2 == 0)
				{
					std::cout &lt;&lt; (&quot;ERROR! Programm beendet vorzeitig um sich nicht zu schaden!&quot;) &lt;&lt; std::endl &lt;&lt; std::endl;
					Sleep(1000);
					return 0;
				}
				Ergebnis = Zahl1 / Zahl2;
				std::cout &lt;&lt; &quot;Das Ergebnis ist: &quot; &lt;&lt; Ergebnis &lt;&lt; std::endl;
				std::cout &lt;&lt; &quot;Beenden(0), Wiederholen(1): &quot;;
				std::cin &gt;&gt; Exit;
				std::cout &lt;&lt; std::endl;
				break;
			case '%':
				Ergebnis = Zahl1 % Zahl2;
				std::cout &lt;&lt; &quot;Der Rest ist: &quot; &lt;&lt; Ergebnis &lt;&lt; std::endl;
				std::cout &lt;&lt; &quot;Beenden(0), Wiederholen(1): &quot;;
				std::cin &gt;&gt; Exit;
				std::cout &lt;&lt; std::endl;
				break;
			default:
				std::cout &lt;&lt; &quot;Falsche eingabe: &quot; &lt;&lt; Operation &lt;&lt; std::endl;
				std::cout &lt;&lt; &quot;Beenden(0), Wiederholen(1): &quot;;
				std::cin &gt;&gt; Exit;
				std::cout &lt;&lt; std::endl;
				break;		

		}

	} while(Exit == 1);

	if(Exit &gt; 1 || Exit &lt; 1)
	{
		std::cout &lt;&lt; &quot;Beendet...&quot; &lt;&lt; std::endl &lt;&lt; std::endl;
	}

	system(&quot;PAUSE&quot;);
	return 0;
}
</code></pre>
<p>Foto:<br />
<a href="http://imgur.com/sbC2r7f" rel="nofollow">http://imgur.com/sbC2r7f</a> (ich wusste nicht wie man Fotos einfügt)</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/331940/string-überprüfen-abfrage-überspringt-bei-falscher-eingabe</link><generator>RSS for Node</generator><lastBuildDate>Wed, 29 Apr 2026 13:02:57 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/331940.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 30 Mar 2015 10:52:35 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to String überprüfen | Abfrage überspringt bei falscher Eingabe on Mon, 30 Mar 2015 10:53:37 GMT]]></title><description><![CDATA[<p>Ich hab zwei Probleme, und zwar wie überprüfe ich ob der Spieler &quot;ja&quot; oder &quot;nein&quot; in die Konsole eingeben hat um im dem Fall das Programm wiederholen oder es wird abgebrochen?</p>
<p>2. Wenn die Abfrage kommt, dass der Nutzer eine Zahl eingeben solle und stattdessen ein Sonderzeichen oder irgendwas anderes als eine Zahl ist dann überspringt er alle Schleifen und geht bis zur letzten und beendet das Programm? Wie kann ich bei jeder Abfrage sicher gehen, dass er nur die eine Auswahl hat - die Zahleneingabe? [Foto-Link am Ende des Beitrags]</p>
<pre><code>#import &lt;iostream&gt;
#import &lt;windows.h&gt;

int Zahl1, Zahl2, Ergebnis, Exit;
char Operation;

int main()
{
	system(&quot;cls&quot;);
	std::cout &lt;&lt; &quot;Syntax: &lt;Zahl1&gt; + | - | * | / | % &lt;Zahl2&gt; = Ergebnis&quot; &lt;&lt; std::endl &lt;&lt; std::endl;
	Sleep(2*1000);

	do
	{
		std::cout &lt;&lt; &quot;Geben Sie die erste Zahl ein: &quot;;
		std::cin &gt;&gt; Zahl1;
		std::cout &lt;&lt; std::endl;
		std::cout &lt;&lt; &quot;Geben Sie die zweite Zahl ein: &quot;;
		std::cin &gt;&gt; Zahl2;
		std::cout &lt;&lt; std::endl;

		std::cout &lt;&lt; &quot;Geben Sie als nächstes die Rechenoperation ein mit der Sie rechnen wollen: &quot;;
		std::cin &gt;&gt; Operation;
		std::cout &lt;&lt; std::endl;	

		switch(Operation)
		{
			case '+':
				Ergebnis = Zahl1 + Zahl2;
				std::cout &lt;&lt; &quot;Das Ergebnis ist: &quot; &lt;&lt; Ergebnis &lt;&lt; std::endl;
				std::cout &lt;&lt; &quot;Beenden(0), Wiederholen(1): &quot;;
				std::cin &gt;&gt; Exit;
				std::cout &lt;&lt; std::endl;
				break;
			case '-':
				Ergebnis = Zahl1 - Zahl2;
				std::cout &lt;&lt; &quot;Das Ergebnis ist: &quot; &lt;&lt; Ergebnis &lt;&lt; std::endl;
				std::cout &lt;&lt; &quot;Beenden(0), Wiederholen(1): &quot;;
				std::cin &gt;&gt; Exit;
				std::cout &lt;&lt; std::endl;
				break;
			case '*':
				Ergebnis = Zahl1 * Zahl2;
				std::cout &lt;&lt; &quot;Das Ergebnis ist: &quot; &lt;&lt; Ergebnis &lt;&lt; std::endl;
				std::cout &lt;&lt; &quot;Beenden(0), Wiederholen(1): &quot;;
				std::cin &gt;&gt; Exit;
				std::cout &lt;&lt; std::endl;
				break;
			case '/':
				if(Zahl1 || Zahl2 == 0)
				{
					std::cout &lt;&lt; (&quot;ERROR! Programm beendet vorzeitig um sich nicht zu schaden!&quot;) &lt;&lt; std::endl &lt;&lt; std::endl;
					Sleep(1000);
					return 0;
				}
				Ergebnis = Zahl1 / Zahl2;
				std::cout &lt;&lt; &quot;Das Ergebnis ist: &quot; &lt;&lt; Ergebnis &lt;&lt; std::endl;
				std::cout &lt;&lt; &quot;Beenden(0), Wiederholen(1): &quot;;
				std::cin &gt;&gt; Exit;
				std::cout &lt;&lt; std::endl;
				break;
			case '%':
				Ergebnis = Zahl1 % Zahl2;
				std::cout &lt;&lt; &quot;Der Rest ist: &quot; &lt;&lt; Ergebnis &lt;&lt; std::endl;
				std::cout &lt;&lt; &quot;Beenden(0), Wiederholen(1): &quot;;
				std::cin &gt;&gt; Exit;
				std::cout &lt;&lt; std::endl;
				break;
			default:
				std::cout &lt;&lt; &quot;Falsche eingabe: &quot; &lt;&lt; Operation &lt;&lt; std::endl;
				std::cout &lt;&lt; &quot;Beenden(0), Wiederholen(1): &quot;;
				std::cin &gt;&gt; Exit;
				std::cout &lt;&lt; std::endl;
				break;		

		}

	} while(Exit == 1);

	if(Exit &gt; 1 || Exit &lt; 1)
	{
		std::cout &lt;&lt; &quot;Beendet...&quot; &lt;&lt; std::endl &lt;&lt; std::endl;
	}

	system(&quot;PAUSE&quot;);
	return 0;
}
</code></pre>
<p>Foto:<br />
<a href="http://imgur.com/sbC2r7f" rel="nofollow">http://imgur.com/sbC2r7f</a> (ich wusste nicht wie man Fotos einfügt)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2448521</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2448521</guid><dc:creator><![CDATA[crazyyzarc]]></dc:creator><pubDate>Mon, 30 Mar 2015 10:53:37 GMT</pubDate></item><item><title><![CDATA[Reply to String überprüfen | Abfrage überspringt bei falscher Eingabe on Mon, 30 Mar 2015 11:07:56 GMT]]></title><description><![CDATA[<p>Wenn etwas anderes eingegeben wird als die Nummer, geht cin in einen Fehlerzustand, aus dem er nicht mehr rauskommt, außer mit clear().</p>
<p>Den Fehlerzustand kann man mit good() oder bad() überprüfen, dann mit clear() zurücksetzt und am besten mit ignore() all fehlehaften Zeichen entfernen.</p>
<p>Siehe hier:<br />
<a href="http://www.cplusplus.com/reference/istream/istream/" rel="nofollow">http://www.cplusplus.com/reference/istream/istream/</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2448522</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2448522</guid><dc:creator><![CDATA[coder777]]></dc:creator><pubDate>Mon, 30 Mar 2015 11:07:56 GMT</pubDate></item><item><title><![CDATA[Reply to String überprüfen | Abfrage überspringt bei falscher Eingabe on Mon, 30 Mar 2015 12:04:30 GMT]]></title><description><![CDATA[<p>baue Dir eine kleine Funktion, die die Überprüfung durchführt. Das funktioniert auch für beliebige Typen 'T'. Dann vereinfacht sich auch Dein Programm wesentlich:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;string&gt;   // std::char_traits&lt;&gt;
#include &lt;cassert&gt;
#include &lt;limits&gt;
#include &lt;stdexcept&gt;

template&lt; typename T &gt;
T eingabe( std::istream&amp; in, std::ostream&amp; out, const char* text )
{
    T x;
    while( out &lt;&lt; text, (in &gt;&gt; x).fail() )
    {
        out &lt;&lt; &quot;Eingabe war fehlerhaft&quot; &lt;&lt; std::endl;
        in.clear(); // Fehlerflag zurück setzen
        if( !in.ignore( std::numeric_limits&lt; std::streamsize &gt;::max(), '\n' ) )
            throw std::runtime_error( &quot;Eingabe Stream ist kaputt&quot; );
    }
    return x;
}

struct Operation
{
    char get() const { return operator_; }

    friend std::istream&amp; operator&gt;&gt;( std::istream&amp; in, Operation&amp; op )
    {
        char c;
        if( in &gt;&gt; c &amp;&amp; std::char_traits&lt; char &gt;::find( &quot;+-*/%&quot;, 5, c ) == nullptr )
            in.setstate( std::ios_base::failbit );
        else
            op.operator_ = c;
        return in;
    }
private:
    char operator_;
};

int main()
{
    std::cout &lt;&lt; &quot;Syntax: &lt;Zahl1&gt; + | - | * | / | % &lt;Zahl2&gt; = Ergebnis&quot; &lt;&lt; std::endl &lt;&lt; std::endl;

    int Exit = 1;   // ## lokale Variablen verwenden
    int Ergebnis;
    do
    {
        auto Zahl1 = eingabe&lt; int &gt;( std::cin, std::cout, &quot;Geben Sie die erste Zahl ein: &quot; );
        auto Zahl2 = eingabe&lt; int &gt;( std::cin, std::cout, &quot;Geben Sie die zweite Zahl ein: &quot; );
        switch( eingabe&lt; Operation &gt;( std::cin, std::cout, 
            &quot;Geben Sie als nächstes die Rechenoperation ein mit der Sie rechnen wollen: &quot; ).get() )
        {
            case '+':
                Ergebnis = Zahl1 + Zahl2;
                std::cout &lt;&lt; &quot;Das Ergebnis ist: &quot; &lt;&lt; Ergebnis &lt;&lt; std::endl;
                break;
            case '-':
                Ergebnis = Zahl1 - Zahl2;
                std::cout &lt;&lt; &quot;Das Ergebnis ist: &quot; &lt;&lt; Ergebnis &lt;&lt; std::endl;
                break;
            case '*':
                Ergebnis = Zahl1 * Zahl2;
                std::cout &lt;&lt; &quot;Das Ergebnis ist: &quot; &lt;&lt; Ergebnis &lt;&lt; std::endl;
                break;
            case '/':
                if(/*Zahl1 ||*/ Zahl2 == 0) // ## Abfrage von Zahl1 überflüssig
                {
                    std::cout &lt;&lt; (&quot;ERROR! Programm beendet vorzeitig um sich nicht zu schaden!&quot;) &lt;&lt; std::endl &lt;&lt; std::endl;
                    // Sleep(1000);
                    return 0;
                }
                Ergebnis = Zahl1 / Zahl2;
                std::cout &lt;&lt; &quot;Das Ergebnis ist: &quot; &lt;&lt; Ergebnis &lt;&lt; std::endl;
                break;
            case '%':
                Ergebnis = Zahl1 % Zahl2;
                std::cout &lt;&lt; &quot;Der Rest ist: &quot; &lt;&lt; Ergebnis &lt;&lt; std::endl;
                break;

            default:
                assert( (&quot;sollte gar nicht moeglich sein!&quot;, 0) );
                break;            
        }

        Exit = eingabe&lt; int &gt;( std::cin, std::cout, &quot;Beenden(0), Wiederholen(1): &quot; );
    } while(Exit == 1);

    std::cout &lt;&lt; &quot;Beendet...&quot; &lt;&lt; std::endl &lt;&lt; std::endl;
    return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2448525</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2448525</guid><dc:creator><![CDATA[Werner Salomon]]></dc:creator><pubDate>Mon, 30 Mar 2015 12:04:30 GMT</pubDate></item><item><title><![CDATA[Reply to String überprüfen | Abfrage überspringt bei falscher Eingabe on Tue, 31 Mar 2015 19:24:50 GMT]]></title><description><![CDATA[<p>Werner Salomon schrieb:</p>
<blockquote>
<p>baue Dir eine kleine Funktion, die die Überprüfung durchführt. Das funktioniert auch für beliebige Typen 'T'. Dann vereinfacht sich auch Dein Programm wesentlich:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;string&gt;   // std::char_traits&lt;&gt;
#include &lt;cassert&gt;
#include &lt;limits&gt;
#include &lt;stdexcept&gt;

template&lt; typename T &gt;
T eingabe( std::istream&amp; in, std::ostream&amp; out, const char* text )
{
    T x;
    while( out &lt;&lt; text, (in &gt;&gt; x).fail() )
    {
        out &lt;&lt; &quot;Eingabe war fehlerhaft&quot; &lt;&lt; std::endl;
        in.clear(); // Fehlerflag zurück setzen
        if( !in.ignore( std::numeric_limits&lt; std::streamsize &gt;::max(), '\n' ) )
            throw std::runtime_error( &quot;Eingabe Stream ist kaputt&quot; );
    }
    return x;
}

struct Operation
{
    char get() const { return operator_; }

    friend std::istream&amp; operator&gt;&gt;( std::istream&amp; in, Operation&amp; op )
    {
        char c;
        if( in &gt;&gt; c &amp;&amp; std::char_traits&lt; char &gt;::find( &quot;+-*/%&quot;, 5, c ) == nullptr )
            in.setstate( std::ios_base::failbit );
        else
            op.operator_ = c;
        return in;
    }
private:
    char operator_;
};

int main()
{
    std::cout &lt;&lt; &quot;Syntax: &lt;Zahl1&gt; + | - | * | / | % &lt;Zahl2&gt; = Ergebnis&quot; &lt;&lt; std::endl &lt;&lt; std::endl;
 
    int Exit = 1;   // ## lokale Variablen verwenden
    int Ergebnis;
    do
    {
        auto Zahl1 = eingabe&lt; int &gt;( std::cin, std::cout, &quot;Geben Sie die erste Zahl ein: &quot; );
        auto Zahl2 = eingabe&lt; int &gt;( std::cin, std::cout, &quot;Geben Sie die zweite Zahl ein: &quot; );
        switch( eingabe&lt; Operation &gt;( std::cin, std::cout, 
            &quot;Geben Sie als nächstes die Rechenoperation ein mit der Sie rechnen wollen: &quot; ).get() )
        {
            case '+':
                Ergebnis = Zahl1 + Zahl2;
                std::cout &lt;&lt; &quot;Das Ergebnis ist: &quot; &lt;&lt; Ergebnis &lt;&lt; std::endl;
                break;
            case '-':
                Ergebnis = Zahl1 - Zahl2;
                std::cout &lt;&lt; &quot;Das Ergebnis ist: &quot; &lt;&lt; Ergebnis &lt;&lt; std::endl;
                break;
            case '*':
                Ergebnis = Zahl1 * Zahl2;
                std::cout &lt;&lt; &quot;Das Ergebnis ist: &quot; &lt;&lt; Ergebnis &lt;&lt; std::endl;
                break;
            case '/':
                if(/*Zahl1 ||*/ Zahl2 == 0) // ## Abfrage von Zahl1 überflüssig
                {
                    std::cout &lt;&lt; (&quot;ERROR! Programm beendet vorzeitig um sich nicht zu schaden!&quot;) &lt;&lt; std::endl &lt;&lt; std::endl;
                    // Sleep(1000);
                    return 0;
                }
                Ergebnis = Zahl1 / Zahl2;
                std::cout &lt;&lt; &quot;Das Ergebnis ist: &quot; &lt;&lt; Ergebnis &lt;&lt; std::endl;
                break;
            case '%':
                Ergebnis = Zahl1 % Zahl2;
                std::cout &lt;&lt; &quot;Der Rest ist: &quot; &lt;&lt; Ergebnis &lt;&lt; std::endl;
                break;

            default:
                assert( (&quot;sollte gar nicht moeglich sein!&quot;, 0) );
                break;            
        }

        Exit = eingabe&lt; int &gt;( std::cin, std::cout, &quot;Beenden(0), Wiederholen(1): &quot; );
    } while(Exit == 1);
   
    std::cout &lt;&lt; &quot;Beendet...&quot; &lt;&lt; std::endl &lt;&lt; std::endl;
    return 0;
}
</code></pre>
</blockquote>
<p>er spuckt aus, dass nullptr nicht deklariert ist... gibt es keine einfachere Methode die auch für mich verstehbar sein kann? Trotzdem 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>
]]></description><link>https://www.c-plusplus.net/forum/post/2448664</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2448664</guid><dc:creator><![CDATA[crazyyzarc]]></dc:creator><pubDate>Tue, 31 Mar 2015 19:24:50 GMT</pubDate></item><item><title><![CDATA[Reply to String überprüfen | Abfrage überspringt bei falscher Eingabe on Wed, 01 Apr 2015 07:29:49 GMT]]></title><description><![CDATA[<p>crazyyzarc schrieb:</p>
<blockquote>
<p>er spuckt aus, dass nullptr nicht deklariert ist...</p>
</blockquote>
<p>Das liegt an Deinem veralteten Compiler. Dann ersetzte doch 'nullptr' durch eine 0</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2448687</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2448687</guid><dc:creator><![CDATA[Kenner von C++]]></dc:creator><pubDate>Wed, 01 Apr 2015 07:29:49 GMT</pubDate></item><item><title><![CDATA[Reply to String überprüfen | Abfrage überspringt bei falscher Eingabe on Thu, 02 Apr 2015 20:00:28 GMT]]></title><description><![CDATA[<p>ich benutze kein Visual Studio C++ aber Dev-C++ <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /></p>
<p>Er spuckt nun:<br />
Error: Zahl1, Zahl2 was not declared in this scope</p>
<p>hab schon versucht irgendwie es mal darüber zu deklarieren aber ohne Erfolg</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2448943</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2448943</guid><dc:creator><![CDATA[crazyyzarc]]></dc:creator><pubDate>Thu, 02 Apr 2015 20:00:28 GMT</pubDate></item></channel></rss>