<?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[Neuling Frage]]></title><description><![CDATA[<p>Hallöchen</p>
<p>Da ich vor 3 - 4 Tagen angefangen C++ zu lernen muss ich euch mal bischen Löschern XD.</p>
<p>Also ich würde gerne Wissen ob es möglich ist zu Prüfen, ob in einer Variable die vom User eingegeben wurde, nur zahlen sind ??</p>
<p>Da mein erstes Programm nen Kleiner Rechner ist um mit den Strukturen und Befehlen in C++ klar zu kommen XD</p>
<p>Hab im Quellcode auch 2 Komentare dazu gemacht wo ich es brauchen könnte.</p>
<pre><code class="language-cpp">// Kleiner Rechner 
// c++ includes
#include &lt;iostream&gt;

using namespace std;

// funktionen vorladen
double fnRechnen(double dZahl1, double dZahl2, int nRechenart);

int main(void)
{
	// variablen deklarieren
	// M_ = Menü  / p_ = Prüfen

	int nM_eing1, nM_eing2, nP_schl = 1, nM_ende = 1;
	double dZ1,dZ2;

	// Programm beginn

	while(nM_ende)
	{
		//Menü
		system(&quot;cls&quot;);
		nP_schl = 1;
		cout &lt;&lt; &quot; Menue&quot; &lt;&lt; endl &lt;&lt; endl;
		cout &lt;&lt; &quot;&lt;1&gt;	Addition&quot; &lt;&lt; endl;
		cout &lt;&lt; &quot;&lt;2&gt;	Subtraktion&quot; &lt;&lt; endl;
		cout &lt;&lt; &quot;&lt;3&gt;	Multiplikation&quot; &lt;&lt; endl;
		cout &lt;&lt; &quot;&lt;4&gt;	Division&quot; &lt;&lt; endl;
		cout &lt;&lt; &quot;&lt;5&gt;	Exit&quot; &lt;&lt; endl &lt;&lt; endl;

		// Benutzereingabe Menü
		cin &gt;&gt; nM_eing1;

		//Prüfen der eingabe ob 1-5 ist 
		if(nM_eing1 == 1 || nM_eing1 == 2 || nM_eing1 == 3 || nM_eing1 == 4 || nM_eing1 == 5  )
		{

			// Rechenarten wählen
			switch(nM_eing1)
			{

			case 1:   // Addition 

				while(nP_schl)
				{
					cout &lt;&lt; &quot;------ Addition ------&quot; &lt;&lt; endl &lt;&lt; endl;
					cout &lt;&lt; &quot;Bitte Zahl1 eingeben: &quot; &lt;&lt; endl;
					cin	&gt;&gt; dZ1;  // hier muss ich noch herrausfinden wie ich prüfe das nur zahlen eingegeben sind &lt;------------------
					cout &lt;&lt; endl &lt;&lt; &quot;Bitte Zahl2 eingeben: &quot; &lt;&lt; endl;
					cin	&gt;&gt; dZ2;	// hier muss ich noch herrausfinden wie ich prüfe das nur zahlen eingegeben sind &lt;------------------
					cout &lt;&lt; endl &lt;&lt; dZ1 &lt;&lt; &quot; + &quot; &lt;&lt; dZ2 &lt;&lt; &quot; = &quot; &lt;&lt; fnRechnen(dZ1, dZ2, nM_eing1) &lt;&lt; endl &lt;&lt; endl;

					// Prüfen ob nochmal Addition rechen
					while(nP_schl)
					{

						cout &lt;&lt; &quot;Wollen sie noch eine Additionsaufgabe Rechnen &quot; &lt;&lt; endl &lt;&lt; endl;
						cout &lt;&lt; &quot;&lt;1&gt; = Ja&quot; &lt;&lt; endl;
						cout &lt;&lt; &quot;&lt;2&gt; = Nein&quot; &lt;&lt; endl &lt;&lt; endl;

						cin &gt;&gt; nM_eing2;

						system(&quot;cls&quot;);

						// eingabe prüfen
						if(nM_eing2 == 1 || nM_eing2 == 2 )
						{

							if(nM_eing2 == 1)
							{
								break;

							}else
							{

								nP_schl = 0;

							}

						}else{

							continue;
						}

					}

				}

				break;

				// Addition Ende

			case 2:

				while(nP_schl)
				{
					cout &lt;&lt; &quot;------ Subtraktion ------&quot; &lt;&lt; endl &lt;&lt; endl;
					cout &lt;&lt; &quot;Bitte Zahl1 eingeben: &quot; &lt;&lt; endl;
					cin	&gt;&gt; dZ1;  // hier muss ich noch herrausfinden wie ich prüfe das nur zahlen eingegeben sind &lt;------------------
					cout &lt;&lt; endl &lt;&lt; &quot;Bitte Zahl2 eingeben: &quot; &lt;&lt; endl;
					cin	&gt;&gt; dZ2;	// hier muss ich noch herrausfinden wie ich prüfe das nur zahlen eingegeben sind &lt;------------------
					cout &lt;&lt; endl &lt;&lt; dZ1 &lt;&lt; &quot; - &quot; &lt;&lt; dZ2 &lt;&lt; &quot; = &quot; &lt;&lt; fnRechnen(dZ1, dZ2, nM_eing1) &lt;&lt; endl &lt;&lt; endl;

					// Prüfen ob nochmal Subtraktion rechen
					while(nP_schl)
					{

						cout &lt;&lt; &quot;Wollen sie noch eine Subtraktionsaufgabe Rechnen &quot; &lt;&lt; endl &lt;&lt; endl;
						cout &lt;&lt; &quot;&lt;1&gt; = Ja&quot; &lt;&lt; endl;
						cout &lt;&lt; &quot;&lt;2&gt; = Nein&quot; &lt;&lt; endl &lt;&lt; endl;

						cin &gt;&gt; nM_eing2;

						system(&quot;cls&quot;);

						// eingabe prüfen
						if(nM_eing2 == 1 || nM_eing2 == 2 )
						{

							if(nM_eing2 == 1)
							{
								break;

							}else
							{

								nP_schl = 0;

							}

						}else{

							continue;
						}

					}

				}

				break;

				// subtraktion Ende

			case 3:

				while(nP_schl)
				{
					cout &lt;&lt; &quot;------ Multiplikation ------&quot; &lt;&lt; endl &lt;&lt; endl;
					cout &lt;&lt; &quot;Bitte Zahl1 eingeben: &quot; &lt;&lt; endl;
					cin	&gt;&gt; dZ1;  // hier muss ich noch herrausfinden wie ich prüfe das nur zahlen eingegeben sind &lt;------------------
					cout &lt;&lt; endl &lt;&lt; &quot;Bitte Zahl2 eingeben: &quot; &lt;&lt; endl;
					cin	&gt;&gt; dZ2;	// hier muss ich noch herrausfinden wie ich prüfe das nur zahlen eingegeben sind &lt;------------------
					cout &lt;&lt; endl &lt;&lt; dZ1 &lt;&lt; &quot; * &quot; &lt;&lt; dZ2 &lt;&lt; &quot; = &quot; &lt;&lt; fnRechnen(dZ1, dZ2, nM_eing1) &lt;&lt; endl &lt;&lt; endl;

					// Prüfen ob nochmal Multiplikation rechen
					while(nP_schl)
					{

						cout &lt;&lt; &quot;Wollen sie noch eine Multiplikationsaufgabe Rechnen &quot; &lt;&lt; endl &lt;&lt; endl;
						cout &lt;&lt; &quot;&lt;1&gt; = Ja&quot; &lt;&lt; endl;
						cout &lt;&lt; &quot;&lt;2&gt; = Nein&quot; &lt;&lt; endl &lt;&lt; endl;

						cin &gt;&gt; nM_eing2;

						system(&quot;cls&quot;);

						// eingabe prüfen
						if(nM_eing2 == 1 || nM_eing2 == 2 )
						{

							if(nM_eing2 == 1)
							{
								break;

							}else
							{

								nP_schl = 0;

							}

						}else{

							continue;
						}

					}

				}

				break;
				// Multiplikation Ende

			case 4:

								while(nP_schl)
				{
					cout &lt;&lt; &quot;------ Division ------&quot; &lt;&lt; endl &lt;&lt; endl;
					cout &lt;&lt; &quot;Bitte Zahl1 eingeben: &quot; &lt;&lt; endl;
					cin	&gt;&gt; dZ1;  // hier muss ich noch herrausfinden wie ich prüfe das nur zahlen eingegeben sind &lt;------------------
					cout &lt;&lt; endl &lt;&lt; &quot;Bitte Zahl2 eingeben: &quot; &lt;&lt; endl;
					cin	&gt;&gt; dZ2;	// hier muss ich noch herrausfinden wie ich prüfe das nur zahlen eingegeben sind &lt;------------------
					cout &lt;&lt; endl &lt;&lt; dZ1 &lt;&lt; &quot; / &quot; &lt;&lt; dZ2 &lt;&lt; &quot; = &quot; &lt;&lt; fnRechnen(dZ1, dZ2, nM_eing1) &lt;&lt; endl &lt;&lt; endl;

					// Prüfen ob nochmal Division rechen
					while(nP_schl)
					{

						cout &lt;&lt; &quot;Wollen sie noch eine Divisionsaufgabe Rechnen &quot; &lt;&lt; endl &lt;&lt; endl;
						cout &lt;&lt; &quot;&lt;1&gt; = Ja&quot; &lt;&lt; endl;
						cout &lt;&lt; &quot;&lt;2&gt; = Nein&quot; &lt;&lt; endl &lt;&lt; endl;

						cin &gt;&gt; nM_eing2;

						system(&quot;cls&quot;);

						// eingabe prüfen
						if(nM_eing2 == 1 || nM_eing2 == 2 )
						{

							if(nM_eing2 == 1)
							{
								break;

							}else
							{

								nP_schl = 0;

							}

						}else{

							continue;
						}

					}

				}

				break;

				// Division Ende
			case 5:
				nM_ende = 0;
				break;

			}

		}else{

		cout &lt;&lt; &quot;Falsche eingabe bitte neu eingeben &quot; &lt;&lt; endl;
		system(&quot;pause&quot;);
		system(&quot;cls&quot;);
		}

	}

	system(&quot;cls&quot;);
	cout &lt;&lt; &quot; Vielen Dank das sie Kleiner Rechner benutz haben &lt;ENTER&gt;&quot; &lt;&lt; endl &lt;&lt; endl;

	system(&quot;pause&quot;);
	return 0;
}

// funktionen 

double fnRechnen(double dZahl1, double dZahl2, int nRechenart)
{
	double dErg;

	switch(nRechenart)
	{
	case 1:
		dErg = dZahl1 + dZahl2;
		break;

	case 2:
		dErg = dZahl1 - dZahl2;
		break;

	case 3:
		dErg = dZahl1 * dZahl2;
		break;

	case 4:
		dErg = dZahl1 / dZahl2;
		break;

	}

	return dErg;
}
</code></pre>
<p>Wie gesagt ist mein erstes soweit fertiges Programm !!!!</p>
<p>Bitte reist mir nicht den Kopf runter es geht bestimmt vieles besser zu Programmieren.</p>
<p>Danke schon mal für eure Mühe.... <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/topic/273294/neuling-frage</link><generator>RSS for Node</generator><lastBuildDate>Fri, 28 Aug 2026 10:05:44 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/273294.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 03 Sep 2010 21:04:27 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Neuling Frage on Fri, 03 Sep 2010 21:04:27 GMT]]></title><description><![CDATA[<p>Hallöchen</p>
<p>Da ich vor 3 - 4 Tagen angefangen C++ zu lernen muss ich euch mal bischen Löschern XD.</p>
<p>Also ich würde gerne Wissen ob es möglich ist zu Prüfen, ob in einer Variable die vom User eingegeben wurde, nur zahlen sind ??</p>
<p>Da mein erstes Programm nen Kleiner Rechner ist um mit den Strukturen und Befehlen in C++ klar zu kommen XD</p>
<p>Hab im Quellcode auch 2 Komentare dazu gemacht wo ich es brauchen könnte.</p>
<pre><code class="language-cpp">// Kleiner Rechner 
// c++ includes
#include &lt;iostream&gt;

using namespace std;

// funktionen vorladen
double fnRechnen(double dZahl1, double dZahl2, int nRechenart);

int main(void)
{
	// variablen deklarieren
	// M_ = Menü  / p_ = Prüfen

	int nM_eing1, nM_eing2, nP_schl = 1, nM_ende = 1;
	double dZ1,dZ2;

	// Programm beginn

	while(nM_ende)
	{
		//Menü
		system(&quot;cls&quot;);
		nP_schl = 1;
		cout &lt;&lt; &quot; Menue&quot; &lt;&lt; endl &lt;&lt; endl;
		cout &lt;&lt; &quot;&lt;1&gt;	Addition&quot; &lt;&lt; endl;
		cout &lt;&lt; &quot;&lt;2&gt;	Subtraktion&quot; &lt;&lt; endl;
		cout &lt;&lt; &quot;&lt;3&gt;	Multiplikation&quot; &lt;&lt; endl;
		cout &lt;&lt; &quot;&lt;4&gt;	Division&quot; &lt;&lt; endl;
		cout &lt;&lt; &quot;&lt;5&gt;	Exit&quot; &lt;&lt; endl &lt;&lt; endl;

		// Benutzereingabe Menü
		cin &gt;&gt; nM_eing1;

		//Prüfen der eingabe ob 1-5 ist 
		if(nM_eing1 == 1 || nM_eing1 == 2 || nM_eing1 == 3 || nM_eing1 == 4 || nM_eing1 == 5  )
		{

			// Rechenarten wählen
			switch(nM_eing1)
			{

			case 1:   // Addition 

				while(nP_schl)
				{
					cout &lt;&lt; &quot;------ Addition ------&quot; &lt;&lt; endl &lt;&lt; endl;
					cout &lt;&lt; &quot;Bitte Zahl1 eingeben: &quot; &lt;&lt; endl;
					cin	&gt;&gt; dZ1;  // hier muss ich noch herrausfinden wie ich prüfe das nur zahlen eingegeben sind &lt;------------------
					cout &lt;&lt; endl &lt;&lt; &quot;Bitte Zahl2 eingeben: &quot; &lt;&lt; endl;
					cin	&gt;&gt; dZ2;	// hier muss ich noch herrausfinden wie ich prüfe das nur zahlen eingegeben sind &lt;------------------
					cout &lt;&lt; endl &lt;&lt; dZ1 &lt;&lt; &quot; + &quot; &lt;&lt; dZ2 &lt;&lt; &quot; = &quot; &lt;&lt; fnRechnen(dZ1, dZ2, nM_eing1) &lt;&lt; endl &lt;&lt; endl;

					// Prüfen ob nochmal Addition rechen
					while(nP_schl)
					{

						cout &lt;&lt; &quot;Wollen sie noch eine Additionsaufgabe Rechnen &quot; &lt;&lt; endl &lt;&lt; endl;
						cout &lt;&lt; &quot;&lt;1&gt; = Ja&quot; &lt;&lt; endl;
						cout &lt;&lt; &quot;&lt;2&gt; = Nein&quot; &lt;&lt; endl &lt;&lt; endl;

						cin &gt;&gt; nM_eing2;

						system(&quot;cls&quot;);

						// eingabe prüfen
						if(nM_eing2 == 1 || nM_eing2 == 2 )
						{

							if(nM_eing2 == 1)
							{
								break;

							}else
							{

								nP_schl = 0;

							}

						}else{

							continue;
						}

					}

				}

				break;

				// Addition Ende

			case 2:

				while(nP_schl)
				{
					cout &lt;&lt; &quot;------ Subtraktion ------&quot; &lt;&lt; endl &lt;&lt; endl;
					cout &lt;&lt; &quot;Bitte Zahl1 eingeben: &quot; &lt;&lt; endl;
					cin	&gt;&gt; dZ1;  // hier muss ich noch herrausfinden wie ich prüfe das nur zahlen eingegeben sind &lt;------------------
					cout &lt;&lt; endl &lt;&lt; &quot;Bitte Zahl2 eingeben: &quot; &lt;&lt; endl;
					cin	&gt;&gt; dZ2;	// hier muss ich noch herrausfinden wie ich prüfe das nur zahlen eingegeben sind &lt;------------------
					cout &lt;&lt; endl &lt;&lt; dZ1 &lt;&lt; &quot; - &quot; &lt;&lt; dZ2 &lt;&lt; &quot; = &quot; &lt;&lt; fnRechnen(dZ1, dZ2, nM_eing1) &lt;&lt; endl &lt;&lt; endl;

					// Prüfen ob nochmal Subtraktion rechen
					while(nP_schl)
					{

						cout &lt;&lt; &quot;Wollen sie noch eine Subtraktionsaufgabe Rechnen &quot; &lt;&lt; endl &lt;&lt; endl;
						cout &lt;&lt; &quot;&lt;1&gt; = Ja&quot; &lt;&lt; endl;
						cout &lt;&lt; &quot;&lt;2&gt; = Nein&quot; &lt;&lt; endl &lt;&lt; endl;

						cin &gt;&gt; nM_eing2;

						system(&quot;cls&quot;);

						// eingabe prüfen
						if(nM_eing2 == 1 || nM_eing2 == 2 )
						{

							if(nM_eing2 == 1)
							{
								break;

							}else
							{

								nP_schl = 0;

							}

						}else{

							continue;
						}

					}

				}

				break;

				// subtraktion Ende

			case 3:

				while(nP_schl)
				{
					cout &lt;&lt; &quot;------ Multiplikation ------&quot; &lt;&lt; endl &lt;&lt; endl;
					cout &lt;&lt; &quot;Bitte Zahl1 eingeben: &quot; &lt;&lt; endl;
					cin	&gt;&gt; dZ1;  // hier muss ich noch herrausfinden wie ich prüfe das nur zahlen eingegeben sind &lt;------------------
					cout &lt;&lt; endl &lt;&lt; &quot;Bitte Zahl2 eingeben: &quot; &lt;&lt; endl;
					cin	&gt;&gt; dZ2;	// hier muss ich noch herrausfinden wie ich prüfe das nur zahlen eingegeben sind &lt;------------------
					cout &lt;&lt; endl &lt;&lt; dZ1 &lt;&lt; &quot; * &quot; &lt;&lt; dZ2 &lt;&lt; &quot; = &quot; &lt;&lt; fnRechnen(dZ1, dZ2, nM_eing1) &lt;&lt; endl &lt;&lt; endl;

					// Prüfen ob nochmal Multiplikation rechen
					while(nP_schl)
					{

						cout &lt;&lt; &quot;Wollen sie noch eine Multiplikationsaufgabe Rechnen &quot; &lt;&lt; endl &lt;&lt; endl;
						cout &lt;&lt; &quot;&lt;1&gt; = Ja&quot; &lt;&lt; endl;
						cout &lt;&lt; &quot;&lt;2&gt; = Nein&quot; &lt;&lt; endl &lt;&lt; endl;

						cin &gt;&gt; nM_eing2;

						system(&quot;cls&quot;);

						// eingabe prüfen
						if(nM_eing2 == 1 || nM_eing2 == 2 )
						{

							if(nM_eing2 == 1)
							{
								break;

							}else
							{

								nP_schl = 0;

							}

						}else{

							continue;
						}

					}

				}

				break;
				// Multiplikation Ende

			case 4:

								while(nP_schl)
				{
					cout &lt;&lt; &quot;------ Division ------&quot; &lt;&lt; endl &lt;&lt; endl;
					cout &lt;&lt; &quot;Bitte Zahl1 eingeben: &quot; &lt;&lt; endl;
					cin	&gt;&gt; dZ1;  // hier muss ich noch herrausfinden wie ich prüfe das nur zahlen eingegeben sind &lt;------------------
					cout &lt;&lt; endl &lt;&lt; &quot;Bitte Zahl2 eingeben: &quot; &lt;&lt; endl;
					cin	&gt;&gt; dZ2;	// hier muss ich noch herrausfinden wie ich prüfe das nur zahlen eingegeben sind &lt;------------------
					cout &lt;&lt; endl &lt;&lt; dZ1 &lt;&lt; &quot; / &quot; &lt;&lt; dZ2 &lt;&lt; &quot; = &quot; &lt;&lt; fnRechnen(dZ1, dZ2, nM_eing1) &lt;&lt; endl &lt;&lt; endl;

					// Prüfen ob nochmal Division rechen
					while(nP_schl)
					{

						cout &lt;&lt; &quot;Wollen sie noch eine Divisionsaufgabe Rechnen &quot; &lt;&lt; endl &lt;&lt; endl;
						cout &lt;&lt; &quot;&lt;1&gt; = Ja&quot; &lt;&lt; endl;
						cout &lt;&lt; &quot;&lt;2&gt; = Nein&quot; &lt;&lt; endl &lt;&lt; endl;

						cin &gt;&gt; nM_eing2;

						system(&quot;cls&quot;);

						// eingabe prüfen
						if(nM_eing2 == 1 || nM_eing2 == 2 )
						{

							if(nM_eing2 == 1)
							{
								break;

							}else
							{

								nP_schl = 0;

							}

						}else{

							continue;
						}

					}

				}

				break;

				// Division Ende
			case 5:
				nM_ende = 0;
				break;

			}

		}else{

		cout &lt;&lt; &quot;Falsche eingabe bitte neu eingeben &quot; &lt;&lt; endl;
		system(&quot;pause&quot;);
		system(&quot;cls&quot;);
		}

	}

	system(&quot;cls&quot;);
	cout &lt;&lt; &quot; Vielen Dank das sie Kleiner Rechner benutz haben &lt;ENTER&gt;&quot; &lt;&lt; endl &lt;&lt; endl;

	system(&quot;pause&quot;);
	return 0;
}

// funktionen 

double fnRechnen(double dZahl1, double dZahl2, int nRechenart)
{
	double dErg;

	switch(nRechenart)
	{
	case 1:
		dErg = dZahl1 + dZahl2;
		break;

	case 2:
		dErg = dZahl1 - dZahl2;
		break;

	case 3:
		dErg = dZahl1 * dZahl2;
		break;

	case 4:
		dErg = dZahl1 / dZahl2;
		break;

	}

	return dErg;
}
</code></pre>
<p>Wie gesagt ist mein erstes soweit fertiges Programm !!!!</p>
<p>Bitte reist mir nicht den Kopf runter es geht bestimmt vieles besser zu Programmieren.</p>
<p>Danke schon mal für eure Mühe.... <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/1947994</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1947994</guid><dc:creator><![CDATA[Nattgard]]></dc:creator><pubDate>Fri, 03 Sep 2010 21:04:27 GMT</pubDate></item><item><title><![CDATA[Reply to Neuling Frage on Fri, 03 Sep 2010 21:19:36 GMT]]></title><description><![CDATA[<p>Ich benutze für so was folgende Funktionsvorlage:</p>
<pre><code class="language-cpp">#ifndef INCLUDED_READ_TYPE_HH
#define INCLUDED_READ_TYPE_HH

#include &lt;iostream&gt;
#include &lt;sstream&gt;
#include &lt;string&gt;

template&lt;typename result_t&gt;
result_t read_type_ref(result_t &amp;dest,
		       std::string const &amp;prompt,
		       std::istream &amp;in  = std::cin,
		       std::ostream &amp;out = std::cout) {
  std::istringstream parser;
  std::string line;

  do {
    parser.clear();

    out &lt;&lt; prompt &lt;&lt; std::flush;

    std::getline(in, line);

    parser.str(line);
    parser &gt;&gt; dest;
  } while(!parser);

  return dest;
}

template&lt;typename result_t&gt;
result_t read_type(std::string const &amp;prompt,
		   std::istream &amp;in  = std::cin,
		   std::ostream &amp;out = std::cout) {
  result_t result;
  return read_type_ref(result, prompt, in, out);
}

#endif
</code></pre>
<p>Zu benutzen wie folgt:</p>
<pre><code class="language-cpp">double d = read_type&lt;double&gt;(&quot;Zahl eingeben: &quot;);
</code></pre>
<p>In der obrigen Implementation fragt das so lange nach, bis eine gültige Eingabe eingegeben wurde.</p>
<p>Die Idee ist im Grunde ziemlich simpel:</p>
<pre><code class="language-cpp">std::string line;
double number;

std::getline(std::cin, line);    // Zeile einlesen

std::istringstream parser(line); // In ein lokales Stream-Objekt stopfen, das mit Strings arbeitet
if(parser &gt;&gt; number) {           // Versuchen, einzulesen.
  // Erfolg
} else {
  // Fehlschlag
}
</code></pre>
<p>std::istringstream verhält sich genau wie std::cin, liest aber nicht von der Konsole, sondern aus einem String (in diesem Fall line). Alle Streams sind außerdem in einen zu bool kompatiblen Typ umwandelbar, der true ist, wenn im Stream alles in Ordnung ist - nach einem fehlgeschlagenen Einleseversuch ist das nicht der Fall.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1947996</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1947996</guid><dc:creator><![CDATA[seldon]]></dc:creator><pubDate>Fri, 03 Sep 2010 21:19:36 GMT</pubDate></item><item><title><![CDATA[Reply to Neuling Frage on Fri, 03 Sep 2010 21:33:53 GMT]]></title><description><![CDATA[<p>nun gut ich verstehe gerade erstmal nur Bahnhof.</p>
<p>der C++ code ist noch um einiges zu hoch für mich denoch vielen vielen dank.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1948000</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1948000</guid><dc:creator><![CDATA[Nattgard]]></dc:creator><pubDate>Fri, 03 Sep 2010 21:33:53 GMT</pubDate></item><item><title><![CDATA[Reply to Neuling Frage on Fri, 03 Sep 2010 21:51:14 GMT]]></title><description><![CDATA[<p>Naja, du kannst auch schlicht</p>
<pre><code class="language-cpp">cin &gt;&gt; dZ1;

if(cin) {
  // Erfolg
} else {
  // Fehlschlag
}
</code></pre>
<p>schreiben, aber dann ist das, was nicht eingelesen werden konnte, halt immer noch in cin drin.</p>
<p>Dann geht auch</p>
<pre><code class="language-cpp">cin &gt;&gt; dZ1;

if(cin) {
  // Erfolg
} else {
  std::cin.clear(); // Fehlerzustand aufheben
  std::cin.ignore(std::numeric_limits&lt;std::streamsize&gt;::max(), '\n'); // Bis zum Zeilenende ignorieren
}
</code></pre>
<p>...aber ich finde das eigentlich nicht bedeutend einfacher als die stringstream-Variante. (Am Rande: std::numeric_limits&lt;std::streamsize::max() ist der größte Wert, den std::streamsize annehmen kann.) Die zeilenbasierte Einlesevariante hat auch den Vorteil, dass keine Zeilenreste im Eingabestrom bleiben - Anfänger sind häufig verwirrt, wenn nach</p>
<pre><code class="language-cpp">std::cin &gt;&gt; x;
std::getline(std::cin, s);
</code></pre>
<p>s meistens leer ist (das Zeilenende ist nach cin &gt;&gt; x immer noch in cin drin).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1948001</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1948001</guid><dc:creator><![CDATA[seldon]]></dc:creator><pubDate>Fri, 03 Sep 2010 21:51:14 GMT</pubDate></item><item><title><![CDATA[Reply to Neuling Frage on Fri, 03 Sep 2010 23:08:59 GMT]]></title><description><![CDATA[<p>Xd</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1948010</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1948010</guid><dc:creator><![CDATA[Nattgard]]></dc:creator><pubDate>Fri, 03 Sep 2010 23:08:59 GMT</pubDate></item></channel></rss>