<?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[If Anweisung prüfen ob Variable eine Zahl ist]]></title><description><![CDATA[<p>Hi,<br />
kann ich mit einer If Anweisung prüfen, ob eine Variable ( wenn es geht vom Typ &quot;double&quot; ) eine Zahl ist und kein Wort oder so?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/201206/if-anweisung-prüfen-ob-variable-eine-zahl-ist</link><generator>RSS for Node</generator><lastBuildDate>Mon, 05 Oct 2026 23:48:51 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/201206.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 26 Dec 2007 12:31:36 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to If Anweisung prüfen ob Variable eine Zahl ist on Wed, 26 Dec 2007 12:31:36 GMT]]></title><description><![CDATA[<p>Hi,<br />
kann ich mit einer If Anweisung prüfen, ob eine Variable ( wenn es geht vom Typ &quot;double&quot; ) eine Zahl ist und kein Wort oder so?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1426266</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1426266</guid><dc:creator><![CDATA[intelfan]]></dc:creator><pubDate>Wed, 26 Dec 2007 12:31:36 GMT</pubDate></item><item><title><![CDATA[Reply to If Anweisung prüfen ob Variable eine Zahl ist on Wed, 26 Dec 2007 12:51:36 GMT]]></title><description><![CDATA[<p>die frage verstehe ich nicht. wenn du eine variable von typ double hast, ist es immer eine zahl.</p>
<p>erkläre doch bitte das, was du machen willst, ein bisschen genauer.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1426284</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1426284</guid><dc:creator><![CDATA[ghorst]]></dc:creator><pubDate>Wed, 26 Dec 2007 12:51:36 GMT</pubDate></item><item><title><![CDATA[Reply to If Anweisung prüfen ob Variable eine Zahl ist on Wed, 26 Dec 2007 12:53:05 GMT]]></title><description><![CDATA[<p>Bei der Deklaration gibst du schon an, ob es ein Wort oder eine Zahl ist:</p>
<pre><code class="language-cpp">double number; // ist immer eine Zahl (oder NaN oder INF, aber das lassen wir mal)
std::string word; // ist immer ein Wort
</code></pre>
<p>Ich schätze, du willst wissen, ob ein Wort als eine Zahl interpretiert werden kann. Dazu könntest du stringstreams benutzen.</p>
<p>Gruß<br />
Don06</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1426287</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1426287</guid><dc:creator><![CDATA[Don06]]></dc:creator><pubDate>Wed, 26 Dec 2007 12:53:05 GMT</pubDate></item><item><title><![CDATA[Reply to If Anweisung prüfen ob Variable eine Zahl ist on Wed, 26 Dec 2007 13:51:06 GMT]]></title><description><![CDATA[<p>wenn ich dein frage richtig verstanden habe und du überprüfen willst, ob in einem string eine zahl steckt, dann kann man das z.B so machen:</p>
<pre><code class="language-cpp">int zahl_rausbekommen(char *string){
    int i = 0;
    int zahl = 0;

    cin.getline(string, 100);

    for(i=0;string[i]&lt;'0' || string[i] &gt; '9';i++){}//läuft so lange bis es auf eine ziffer trifft wenn er auf eine trifft hast du sie direkt zugriff auf sie weil i dann an der stelle steht

    zahl = string[i] - '0'; //und so kannst du die zahl direkt in einen integer umwandeln, weil das ascii zeichen '0' den wert abzieht damit du auf den richtigen int-wert kommst
    return zahl;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1426304</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1426304</guid><dc:creator><![CDATA[Ryu]]></dc:creator><pubDate>Wed, 26 Dec 2007 13:51:06 GMT</pubDate></item><item><title><![CDATA[Reply to If Anweisung prüfen ob Variable eine Zahl ist on Wed, 26 Dec 2007 14:00:32 GMT]]></title><description><![CDATA[<p>Ups sorry, wegen oben, das cin.getline gehört natürlich nicht rein, wenn man den string chon als parameter bekommt.</p>
<p>wenn du das als if abfrage formulierst kannst du einfach die funktion in die if klammern schreiben, du musst nur dafür sorgen, dass zahl nur zugewiesen wird, wenn eine zahl im string war, also wenn</p>
<pre><code class="language-cpp">i &lt; (strlen(string)-1)
</code></pre>
<p>, ansonsten gib zahl einfach so zurück, denn eine 0 wird im if als negativ interpretiert</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1426306</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1426306</guid><dc:creator><![CDATA[Ryu]]></dc:creator><pubDate>Wed, 26 Dec 2007 14:00:32 GMT</pubDate></item><item><title><![CDATA[Reply to If Anweisung prüfen ob Variable eine Zahl ist on Wed, 26 Dec 2007 14:13:46 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">int zahl_rausbekommen(char *string){
    int i = 0;
    int zahl = 0;

    cin.getline(string, 100);

    for(i=0;string[i]&lt;'0' || string[i] &gt; '9';i++){}//läuft so lange bis es auf eine ziffer trifft wenn er auf eine trifft hast du sie direkt zugriff auf sie weil i dann an der stelle steht

    zahl = string[i] - '0'; //und so kannst du die zahl direkt in einen integer umwandeln, weil das ascii zeichen '0' den wert abzieht damit du auf den richtigen int-wert kommst
    return zahl;
}
</code></pre>
<p>also das ist so nen murks <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61b.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_tongue"
      title=":P"
      alt="😛"
    /> Gib mal nen String rein, der keine Zahl enthält!</p>
<pre><code class="language-cpp">void get_number(std::string const &amp; string, int&amp; dest)
{
    const std::string::size_type pos(string.find_first_of(&quot;0123456789&quot;));
    if (pos == std::string::npos) throw std::invalid_argument(&quot;no number found&quot;);
    std::istringstream ss(string.substr(pos));
    if (!(ss &gt;&gt; dest)) throw std::runtime_error(&quot;could not convert to integer&quot;);
}
</code></pre>
<p>oder so sollte gehen ...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1426310</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1426310</guid><dc:creator><![CDATA[*D*Evil]]></dc:creator><pubDate>Wed, 26 Dec 2007 14:13:46 GMT</pubDate></item><item><title><![CDATA[Reply to If Anweisung prüfen ob Variable eine Zahl ist on Wed, 26 Dec 2007 14:35:26 GMT]]></title><description><![CDATA[<p>Ja, das ist mir dann ja auch aufgefallen, aber weil editieren nicht möglich ist, habe ich das im 2ten post dann ja gesagt</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1426317</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1426317</guid><dc:creator><![CDATA[Ryu]]></dc:creator><pubDate>Wed, 26 Dec 2007 14:35:26 GMT</pubDate></item><item><title><![CDATA[Reply to If Anweisung prüfen ob Variable eine Zahl ist on Wed, 26 Dec 2007 14:36:41 GMT]]></title><description><![CDATA[<p>Also ich will das mit ner Eingabe machen. Problem: Ich will machen, dass wenn man bei folgendem Code einen Buchstaben eingibt, eine Fehlermeldung kommt und nicht gleich alles unendlich mal ausgeführt wird:</p>
<p>// Quadratzahlen berechnen<br />
#include &lt;iostream&gt;<br />
using namespace std;</p>
<p>int main()<br />
{<br />
double eingabe; // Schleifenvariable</p>
<p>eingabe = 1;</p>
<p>while (1)<br />
{<br />
cout &lt;&lt; &quot;Programm zur Berechnung von Quadratzahlen&quot; &lt;&lt; endl;<br />
cout &lt;&lt; &quot;Geben Sie eine Zahl ein ( 0 zum Beenden ): &quot;;</p>
<p>cin &gt;&gt; eingabe;</p>
<p>if (eingabe == 0)<br />
break;</p>
<p>cout &lt;&lt; &quot;Das Quadrat von &quot; &lt;&lt; eingabe<br />
&lt;&lt; &quot; ist &quot; &lt;&lt; eingabe * eingabe &lt;&lt; endl;<br />
cout &lt;&lt; endl;</p>
<p>}</p>
<p>return 0;<br />
}</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1426318</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1426318</guid><dc:creator><![CDATA[intelfan]]></dc:creator><pubDate>Wed, 26 Dec 2007 14:36:41 GMT</pubDate></item><item><title><![CDATA[Reply to If Anweisung prüfen ob Variable eine Zahl ist on Wed, 26 Dec 2007 14:41:53 GMT]]></title><description><![CDATA[<p>wenns geht dann auch gleich noch die benötigte Headerdatei angeben, falls benötigt</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1426320</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1426320</guid><dc:creator><![CDATA[intelfan]]></dc:creator><pubDate>Wed, 26 Dec 2007 14:41:53 GMT</pubDate></item><item><title><![CDATA[Reply to If Anweisung prüfen ob Variable eine Zahl ist on Wed, 26 Dec 2007 15:52:10 GMT]]></title><description><![CDATA[<p>hmm einfach forensuche bemühen ... ^^ ist mitlerweile 0815 Thema der Woche ^^</p>
<p>Gucken ob eingabe erfolgreich war (!std::cin), d.h. etwas eingegeben wurde, dass man in einen double konvertieren kann. Wenn es nicht geklappt hat, das ausgeben und Stream zurücksetzen(std::cin.clear(), std::cin.ignore(std::numeric_limits<a href="std::streamsize" rel="nofollow">std::streamsize</a>::max(), '\n')). Jetzt kannst du nochmal versuchen eine Zahl einzulesen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1426343</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1426343</guid><dc:creator><![CDATA[*D*Evil]]></dc:creator><pubDate>Wed, 26 Dec 2007 15:52:10 GMT</pubDate></item><item><title><![CDATA[Reply to If Anweisung prüfen ob Variable eine Zahl ist on Wed, 26 Dec 2007 20:07:17 GMT]]></title><description><![CDATA[<p>Lies einen String ein, konvertiere den String selbst nach double, und überprüfe ob die Konvertierung hingehaut hat.<br />
Eine Möglichkeit:</p>
<pre><code class="language-cpp">std::string str;
std::cin &gt;&gt; str;
if (!std::cin)
    input_error();
try
{
    double d = boost::lexical_cast&lt;double&gt;(str);
}
catch (std::exception const&amp;)
{
    war_keine_zahl();
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1426454</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1426454</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Wed, 26 Dec 2007 20:07:17 GMT</pubDate></item><item><title><![CDATA[Reply to If Anweisung prüfen ob Variable eine Zahl ist on Wed, 26 Dec 2007 21:02:40 GMT]]></title><description><![CDATA[<p>Naja warum so aufwendig? Das ist ja Performance Verlust pur. lexical_cast nutzt ja auch nur die Streamoperatoren und da kannst du besser direkt gucken ob das Einlesen geklappt hat(also std::cin überprüfen!) ...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1426489</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1426489</guid><dc:creator><![CDATA[*D*Evil]]></dc:creator><pubDate>Wed, 26 Dec 2007 21:02:40 GMT</pubDate></item><item><title><![CDATA[Reply to If Anweisung prüfen ob Variable eine Zahl ist on Wed, 26 Dec 2007 22:38:39 GMT]]></title><description><![CDATA[<p>Und warum nicht gleich atoi verwenden, oder sscanf?<br />
Blubb.</p>
<p>Ja, es gibt mehrere Möglichkeiten.</p>
<p>Erstmal nen String lesen und dann umwandlungen selbst machen ist oft die beste, da man so schön prüfen kann wo der Fehler liegt, also ob z.B. der Input Stream leer war, oder aber etwas eingegeben wurde was nicht als Zahl interpretiert werden konnte etc.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1426544</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1426544</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Wed, 26 Dec 2007 22:38:39 GMT</pubDate></item><item><title><![CDATA[Reply to If Anweisung prüfen ob Variable eine Zahl ist on Wed, 26 Dec 2007 22:48:27 GMT]]></title><description><![CDATA[<p>dann aber mit getline</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1426555</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1426555</guid><dc:creator><![CDATA[..........]]></dc:creator><pubDate>Wed, 26 Dec 2007 22:48:27 GMT</pubDate></item><item><title><![CDATA[Reply to If Anweisung prüfen ob Variable eine Zahl ist on Thu, 27 Dec 2007 17:38:27 GMT]]></title><description><![CDATA[<p>.......... schrieb:</p>
<blockquote>
<p>dann aber mit getline</p>
</blockquote>
<p>Da hast du wohl Recht <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/1426930</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1426930</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Thu, 27 Dec 2007 17:38:27 GMT</pubDate></item></channel></rss>