<?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[Exception 1. Versuche]]></title><description><![CDATA[<p>Hallo zusammen,</p>
<p>ich möchte aus einem String ein Integer Wert machen wenn jedoch ein Buchstabe eingegeben wird soll es mit einer Exception abgefangen werden. Hier mal mein Code.</p>
<pre><code>string name,alter,vorname;

       cout &lt;&lt; &quot;Bitte geben Sie ihren Namen ein!&quot;;
       cin &gt;&gt; name;
       cout &lt;&lt; &quot;Bitte geben Sie ihren Vornamen ein!&quot;;
       cin &gt;&gt; vorname;
       cout &lt;&lt; &quot;Bitte geben Sie ihr Alter an!&quot;;
       cin &gt;&gt; alter;
       cout &lt;&lt; &quot;Ihre Eingabe lautet: &quot; &lt;&lt; name &lt;&lt; &quot;, &quot; &lt;&lt; vorname &lt;&lt; &quot;, &quot; &lt;&lt; alter &lt;&lt; &quot;!&quot; &lt;&lt; endl;

       try {
           int alt,zeit;
           alt = atoi (alter.c_str());
           zeit = 100 - alt;
           cout &lt;&lt; &quot;Sie brauchen noch &quot; &lt;&lt; zeit &lt;&lt; &quot; Jahre um 100 Jahre alt zu werden!&quot; &lt;&lt; endl;
       } catch (...) {
           cout &lt;&lt; &quot;Na hat da wer geflunkert?&quot; &lt;&lt; endl;
       }
</code></pre>
<p>Ist nicht kommentiert sollte an sich ja eigentlich jeder verstehen!<br />
<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>Gruß<br />
Ursa</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/117958/exception-1-versuche</link><generator>RSS for Node</generator><lastBuildDate>Fri, 21 Aug 2026 06:22:55 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/117958.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 12 Aug 2005 19:57:33 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Exception 1. Versuche on Fri, 12 Aug 2005 19:57:33 GMT]]></title><description><![CDATA[<p>Hallo zusammen,</p>
<p>ich möchte aus einem String ein Integer Wert machen wenn jedoch ein Buchstabe eingegeben wird soll es mit einer Exception abgefangen werden. Hier mal mein Code.</p>
<pre><code>string name,alter,vorname;

       cout &lt;&lt; &quot;Bitte geben Sie ihren Namen ein!&quot;;
       cin &gt;&gt; name;
       cout &lt;&lt; &quot;Bitte geben Sie ihren Vornamen ein!&quot;;
       cin &gt;&gt; vorname;
       cout &lt;&lt; &quot;Bitte geben Sie ihr Alter an!&quot;;
       cin &gt;&gt; alter;
       cout &lt;&lt; &quot;Ihre Eingabe lautet: &quot; &lt;&lt; name &lt;&lt; &quot;, &quot; &lt;&lt; vorname &lt;&lt; &quot;, &quot; &lt;&lt; alter &lt;&lt; &quot;!&quot; &lt;&lt; endl;

       try {
           int alt,zeit;
           alt = atoi (alter.c_str());
           zeit = 100 - alt;
           cout &lt;&lt; &quot;Sie brauchen noch &quot; &lt;&lt; zeit &lt;&lt; &quot; Jahre um 100 Jahre alt zu werden!&quot; &lt;&lt; endl;
       } catch (...) {
           cout &lt;&lt; &quot;Na hat da wer geflunkert?&quot; &lt;&lt; endl;
       }
</code></pre>
<p>Ist nicht kommentiert sollte an sich ja eigentlich jeder verstehen!<br />
<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>Gruß<br />
Ursa</p>
]]></description><link>https://www.c-plusplus.net/forum/post/851235</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/851235</guid><dc:creator><![CDATA[UrsaMajor]]></dc:creator><pubDate>Fri, 12 Aug 2005 19:57:33 GMT</pubDate></item><item><title><![CDATA[Reply to Exception 1. Versuche on Fri, 12 Aug 2005 19:59:20 GMT]]></title><description><![CDATA[<p>Hi,</p>
<p>ja hmm ok.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/851237</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/851237</guid><dc:creator><![CDATA[prolog]]></dc:creator><pubDate>Fri, 12 Aug 2005 19:59:20 GMT</pubDate></item><item><title><![CDATA[Reply to Exception 1. Versuche on Fri, 12 Aug 2005 20:10:33 GMT]]></title><description><![CDATA[<p>afaik wirft atoi keine Exception wenn es fehl schlägt?!</p>
<p>Bzw. wo liegt eigentlich das Problem? Deinem Posting fehlt ein Fragezeichen.</p>
<p>MfG SideWinder</p>
]]></description><link>https://www.c-plusplus.net/forum/post/851242</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/851242</guid><dc:creator><![CDATA[SideWinder]]></dc:creator><pubDate>Fri, 12 Aug 2005 20:10:33 GMT</pubDate></item><item><title><![CDATA[Reply to Exception 1. Versuche on Fri, 12 Aug 2005 20:11:31 GMT]]></title><description><![CDATA[<p>Ich seh nirgends eine throw-Anweisung und auch sonst nichts, was eine Exception werfen könnte. Lies dir die Dokumentation von atoi nochmal durch.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/851243</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/851243</guid><dc:creator><![CDATA[Bashar]]></dc:creator><pubDate>Fri, 12 Aug 2005 20:11:31 GMT</pubDate></item><item><title><![CDATA[Reply to Exception 1. Versuche on Fri, 12 Aug 2005 20:20:21 GMT]]></title><description><![CDATA[<p>ja genau danach suche ich ja. wenn jemand einen buchstabem beim alter eingibt dann soll eine exception starten. aber ich weiß net so genau wie! im borland war das hier prinzipiell schon alles, da gibs sowas wie throw nicht so richtig!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/851248</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/851248</guid><dc:creator><![CDATA[UrsaMajor]]></dc:creator><pubDate>Fri, 12 Aug 2005 20:20:21 GMT</pubDate></item><item><title><![CDATA[Reply to Exception 1. Versuche on Fri, 12 Aug 2005 20:34:29 GMT]]></title><description><![CDATA[<p>atoi ist C und C kennt (meines Wissens, korrigiert mich) keine Exceptions. Das kannst du mit Stringstreams lösen, das wirft dann eine Exception.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/851259</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/851259</guid><dc:creator><![CDATA[Artchi]]></dc:creator><pubDate>Fri, 12 Aug 2005 20:34:29 GMT</pubDate></item><item><title><![CDATA[Reply to Exception 1. Versuche on Fri, 12 Aug 2005 20:45:00 GMT]]></title><description><![CDATA[<p>thx, mehr wollte ich doch gar net wissen. vielen dank.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/851267</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/851267</guid><dc:creator><![CDATA[UrsaMajor]]></dc:creator><pubDate>Fri, 12 Aug 2005 20:45:00 GMT</pubDate></item><item><title><![CDATA[Reply to Exception 1. Versuche on Fri, 12 Aug 2005 20:45:39 GMT]]></title><description><![CDATA[<p>Artchi schrieb:</p>
<blockquote>
<p>Das kannst du mit Stringstreams lösen, ...</p>
</blockquote>
<p>Ja.</p>
<p>Artchi schrieb:</p>
<blockquote>
<p>... das wirft dann eine Exception.</p>
</blockquote>
<p>Nein.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/851268</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/851268</guid><dc:creator><![CDATA[finix]]></dc:creator><pubDate>Fri, 12 Aug 2005 20:45:39 GMT</pubDate></item><item><title><![CDATA[Reply to Exception 1. Versuche on Fri, 12 Aug 2005 20:48:54 GMT]]></title><description><![CDATA[<p>evtl. solltest du dir auch mal überlegen, was das Wort &quot;exception&quot; bedeutet:<br />
Ausnahme.<br />
Sowas sollte also nur in den Fällen benutzt werden, die wirklich Ausnahmen darstellen und im Normalfall nicht vorkommen (wundersames Verschwinden wichtiger Dateien etc.)<br />
Die Fehleingabe eines Users wie in diesem Fall würde ich eher nicht dazu zählen...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/851269</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/851269</guid><dc:creator><![CDATA[pumuckl]]></dc:creator><pubDate>Fri, 12 Aug 2005 20:48:54 GMT</pubDate></item><item><title><![CDATA[Reply to Exception 1. Versuche on Fri, 12 Aug 2005 21:08:29 GMT]]></title><description><![CDATA[<p>Ja, und ein Alpha in einem Numericblock ist eine Ausnahme. Es ist schon korrekt was der Topicersteller will.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/851280</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/851280</guid><dc:creator><![CDATA[Artchi]]></dc:creator><pubDate>Fri, 12 Aug 2005 21:08:29 GMT</pubDate></item><item><title><![CDATA[Reply to Exception 1. Versuche on Fri, 12 Aug 2005 21:22:13 GMT]]></title><description><![CDATA[<p>Stringstreams werfen keine Exceptions, das heist keine mit der Ausnahme, dass man sie explicit mit ios::exceptions einschaltet. Die Stringstreams sind nur bedingt für Convertirungen geeignet. Das Abfragen des Failbits nach der Lese Operation ist ja leider nicht alles was gemacht werden muss, man muss ja auch testen ob alle Zeichen im Stream gelesen wurden. Alles in allem eine mögliche Lösung wenn auch nicht die Beste.</p>
<p>Das was du suchst ist boost::lexical_cast. Leider nicht Standard <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /> kann allerdings für so gut wie jeden Compiler von <a href="http://boost.org" rel="nofollow">boost.org</a> runtergeladen werden.</p>
<pre><code class="language-cpp">cout &lt;&lt; &quot;Bitte geben Sie ihren Namen ein!&quot;;
cin &gt;&gt; name;
cout &lt;&lt; &quot;Bitte geben Sie ihren Vornamen ein!&quot;;
cin &gt;&gt; vorname;
cout &lt;&lt; &quot;Bitte geben Sie ihr Alter an!&quot;;
cin &gt;&gt; alter;
cout &lt;&lt; &quot;Ihre Eingabe lautet: &quot; &lt;&lt; name &lt;&lt; &quot;, &quot; &lt;&lt; vorname &lt;&lt; &quot;, &quot; &lt;&lt; alter &lt;&lt; &quot;!&quot; &lt;&lt; endl;

try {
  int alt,zeit;
  alt = lexical_cast&lt;int&gt;(alter);
  zeit = 100 - alt;
  cout &lt;&lt; &quot;Sie brauchen noch &quot; &lt;&lt; zeit &lt;&lt; &quot; Jahre um 100 Jahre alt zu werden!&quot; &lt;&lt; endl;
} catch (bad_lexical_cast&amp;) {
  cout &lt;&lt; &quot;Na hat da wer geflunkert?&quot; &lt;&lt; endl;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/851291</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/851291</guid><dc:creator><![CDATA[Ben04]]></dc:creator><pubDate>Fri, 12 Aug 2005 21:22:13 GMT</pubDate></item><item><title><![CDATA[Reply to Exception 1. Versuche on Fri, 12 Aug 2005 21:28:58 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">#include &lt;strstream&gt;
#include &lt;iostream&gt;

using namespace std;

stringstream ios;
ios.exceptions(stringstream::failbit); // exceptions einschalten, historisch bedingt off.
ios &lt;&lt; &quot;AA&quot;;
int f;

try {
    ios &gt;&gt; f;   // wirft std::ios_base::failure!!!!!!!!!!!!
}
catch(const std::ios_base::failure &amp;ex) {
    std::cerr &lt;&lt; ex.what() &lt;&lt; std::endl;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/851294</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/851294</guid><dc:creator><![CDATA[Artchi]]></dc:creator><pubDate>Fri, 12 Aug 2005 21:28:58 GMT</pubDate></item><item><title><![CDATA[Reply to Exception 1. Versuche on Fri, 12 Aug 2005 21:29:01 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">stringstream ios;
ios.exceptions(stringstream::failbit);
ios &lt;&lt; &quot;156AA&quot;;
int f;
try {
  ios &gt;&gt; f;   // wirft kein std::ios_base::failure
}
catch(const std::ios_base::failure &amp;ex) {
  std::cerr &lt;&lt; ex.what() &lt;&lt; std::endl;
}
</code></pre>
<p>Je nach Ansichtssache ist eine Convertirung von &quot;156AA&quot; zu 156 falsch.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/851299</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/851299</guid><dc:creator><![CDATA[Ben04]]></dc:creator><pubDate>Fri, 12 Aug 2005 21:29:01 GMT</pubDate></item><item><title><![CDATA[Reply to Exception 1. Versuche on Sat, 13 Aug 2005 20:03:05 GMT]]></title><description><![CDATA[<p>Um zu prüfen ob eine Eingabe eine Zahl oder ein Buchstabe ist, reicht doch die Verwendung von</p>
<pre><code>isalpha
</code></pre>
<p><a href="http://cppreference.com/stdstring/isalpha.html" rel="nofollow">http://cppreference.com/stdstring/isalpha.html</a></p>
<p>mit einer negativabfrage. Oder irre ich mich??</p>
<p>rya.<br />
Scorcher24</p>
]]></description><link>https://www.c-plusplus.net/forum/post/851720</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/851720</guid><dc:creator><![CDATA[[[global:former_user]]]]></dc:creator><pubDate>Sat, 13 Aug 2005 20:03:05 GMT</pubDate></item><item><title><![CDATA[Reply to Exception 1. Versuche on Sat, 13 Aug 2005 20:08:02 GMT]]></title><description><![CDATA[<p>@Scorcher24: Das läuft für einzelne Zeichen, hat aber nichts damit zu tun, ob eine Zahl ein float/double/sonstwas darstellt.</p>
<p>MfG SideWinder</p>
]]></description><link>https://www.c-plusplus.net/forum/post/851722</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/851722</guid><dc:creator><![CDATA[SideWinder]]></dc:creator><pubDate>Sat, 13 Aug 2005 20:08:02 GMT</pubDate></item><item><title><![CDATA[Reply to Exception 1. Versuche on Sat, 13 Aug 2005 21:33:34 GMT]]></title><description><![CDATA[<p>Scorcher24 schrieb:</p>
<blockquote>
<p>Um zu prüfen ob eine Eingabe eine Zahl oder ein Buchstabe ist, reicht doch die Verwendung von</p>
<pre><code>isalpha
</code></pre>
<p><a href="http://cppreference.com/stdstring/isalpha.html" rel="nofollow">http://cppreference.com/stdstring/isalpha.html</a></p>
<p>mit einer negativabfrage. Oder irre ich mich??</p>
</blockquote>
<p>Siehe Sides &amp; Bens Einwürfe...<br />
Versuch's so:</p>
<pre><code class="language-cpp">myStream &gt;&gt; myInt;

// if (myStream &amp;&amp; myStream.eof()) { // oder:
if (!myStream.fail() &amp;&amp; myStream.eof()) {
  // alles ok
} else {
  // Stream war nicht OK,
  // Stream war leer,
  // Konvertierung schlug fehl,
  // String war nicht komplett als int interpretierbar,
  // ...
  // throw up!?
}

// oder ganz einfach
#include &lt;boost/lexical_cast.hpp&gt;
// ...
try {
  myInt = lexical_cast&lt;int&gt;(myString);
} catch (bad_lexical_cast&amp; exc) {
  // ...
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/851751</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/851751</guid><dc:creator><![CDATA[finix]]></dc:creator><pubDate>Sat, 13 Aug 2005 21:33:34 GMT</pubDate></item></channel></rss>