<?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[Datum erhöhen, die fünfte Version]]></title><description><![CDATA[<p>Jetzt ist das Problem mit add_day. Wieso erhalte ich ungültige Werte in add_day obwohl dort eine Prüfung eingebaut wurde?</p>
<pre><code class="language-cpp">class Date{
public:
	enum Month{ 
        jan=1,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec 
    };

	int day() const { return d; };
	Month month() const { return m; };
	int year() const { return y; };

	void add_day(int n);            // increase Day with n value 
	void add_month(int n);          // increase Month with n value 
	void add_year(int n);           // increase Year with n value 
	Date(int y, Month m, int d); 
private:
	int y; // Year
	Month m;
	int d; // Day
};

Date::Date(int y, Month m, int d):y(y),m(m),d(d) // checking for valid date and                                                                   
{                                                // does the initialization 
	if(y&lt;1200 || y&gt;2200 || d&lt;1 || d&gt;31)
	{
		this-&gt;y = 0;
		this-&gt;d = 0;
	}
}

void Date::add_day(int n)
{
	if(d + n &gt;= 32) { d = 0;}
	else {d = d + n;}
}

void f()
{
	const Date today(1978,Date::jun,25);
	cout &lt;&lt; today.day() &lt;&lt; endl;

	Date tomorrow = today;
	tomorrow.add_day(1);
	cout &lt;&lt; tomorrow.day() &lt;&lt; endl;

	Date non(2240,Date::sep,28);
	cout &lt;&lt; non.year() &lt;&lt; endl;
	non.add_day(12);
	cout &lt;&lt; non.day() &lt;&lt; endl;
}

int main()
try{
    f();

    keep_window_open();
    return 0;
}
catch (runtime_error e) {     
    cout &lt;&lt; e.what() &lt;&lt; '\n'; 
    keep_window_open(&quot;~&quot;);     
} 
catch (...) {     
    cout &lt;&lt; &quot;exiting\n&quot;; 
    keep_window_open(&quot;~&quot;);     
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/280964/datum-erhöhen-die-fünfte-version</link><generator>RSS for Node</generator><lastBuildDate>Sun, 23 Aug 2026 22:37:38 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/280964.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 24 Jan 2011 18:18:51 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Datum erhöhen, die fünfte Version on Mon, 24 Jan 2011 18:18:51 GMT]]></title><description><![CDATA[<p>Jetzt ist das Problem mit add_day. Wieso erhalte ich ungültige Werte in add_day obwohl dort eine Prüfung eingebaut wurde?</p>
<pre><code class="language-cpp">class Date{
public:
	enum Month{ 
        jan=1,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec 
    };

	int day() const { return d; };
	Month month() const { return m; };
	int year() const { return y; };

	void add_day(int n);            // increase Day with n value 
	void add_month(int n);          // increase Month with n value 
	void add_year(int n);           // increase Year with n value 
	Date(int y, Month m, int d); 
private:
	int y; // Year
	Month m;
	int d; // Day
};

Date::Date(int y, Month m, int d):y(y),m(m),d(d) // checking for valid date and                                                                   
{                                                // does the initialization 
	if(y&lt;1200 || y&gt;2200 || d&lt;1 || d&gt;31)
	{
		this-&gt;y = 0;
		this-&gt;d = 0;
	}
}

void Date::add_day(int n)
{
	if(d + n &gt;= 32) { d = 0;}
	else {d = d + n;}
}

void f()
{
	const Date today(1978,Date::jun,25);
	cout &lt;&lt; today.day() &lt;&lt; endl;

	Date tomorrow = today;
	tomorrow.add_day(1);
	cout &lt;&lt; tomorrow.day() &lt;&lt; endl;

	Date non(2240,Date::sep,28);
	cout &lt;&lt; non.year() &lt;&lt; endl;
	non.add_day(12);
	cout &lt;&lt; non.day() &lt;&lt; endl;
}

int main()
try{
    f();

    keep_window_open();
    return 0;
}
catch (runtime_error e) {     
    cout &lt;&lt; e.what() &lt;&lt; '\n'; 
    keep_window_open(&quot;~&quot;);     
} 
catch (...) {     
    cout &lt;&lt; &quot;exiting\n&quot;; 
    keep_window_open(&quot;~&quot;);     
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2011384</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2011384</guid><dc:creator><![CDATA[winux]]></dc:creator><pubDate>Mon, 24 Jan 2011 18:18:51 GMT</pubDate></item><item><title><![CDATA[Reply to Datum erhöhen, die fünfte Version on Mon, 24 Jan 2011 18:26:53 GMT]]></title><description><![CDATA[<pre><code>if(d + n &gt;= 32) { d = 0;}
</code></pre>
<p>Wenn du mir den Monat mit 32 Tagen zeigst, zeige ich dir den Fehler :p</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2011392</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2011392</guid><dc:creator><![CDATA[_kermit]]></dc:creator><pubDate>Mon, 24 Jan 2011 18:26:53 GMT</pubDate></item><item><title><![CDATA[Reply to Datum erhöhen, die fünfte Version on Mon, 24 Jan 2011 18:30:15 GMT]]></title><description><![CDATA[<p>Ach nee wieder ein schreib Fehler, auf 31 korrigiert, doch wo ist der Fehler.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2011399</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2011399</guid><dc:creator><![CDATA[winux]]></dc:creator><pubDate>Mon, 24 Jan 2011 18:30:15 GMT</pubDate></item><item><title><![CDATA[Reply to Datum erhöhen, die fünfte Version on Mon, 24 Jan 2011 18:32:21 GMT]]></title><description><![CDATA[<p>nutz den debugger</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2011400</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2011400</guid><dc:creator><![CDATA[BigNeal]]></dc:creator><pubDate>Mon, 24 Jan 2011 18:32:21 GMT</pubDate></item><item><title><![CDATA[Reply to Datum erhöhen, die fünfte Version on Mon, 24 Jan 2011 18:38:32 GMT]]></title><description><![CDATA[<p>Ne der 32 ist kein Fehler. Damit meine ich wenn der Wert von d 32 ist oder grösser dann sollte d den Wert 0 erhalten.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2011406</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2011406</guid><dc:creator><![CDATA[winux]]></dc:creator><pubDate>Mon, 24 Jan 2011 18:38:32 GMT</pubDate></item><item><title><![CDATA[Reply to Datum erhöhen, die fünfte Version on Mon, 24 Jan 2011 18:43:25 GMT]]></title><description><![CDATA[<p>Fehler gefunden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2011410</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2011410</guid><dc:creator><![CDATA[winux]]></dc:creator><pubDate>Mon, 24 Jan 2011 18:43:25 GMT</pubDate></item><item><title><![CDATA[Reply to Datum erhöhen, die fünfte Version on Mon, 24 Jan 2011 19:13:09 GMT]]></title><description><![CDATA[<p>Ich würde assert verwenden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2011436</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2011436</guid><dc:creator><![CDATA[314159265358979]]></dc:creator><pubDate>Mon, 24 Jan 2011 19:13:09 GMT</pubDate></item><item><title><![CDATA[Reply to Datum erhöhen, die fünfte Version on Mon, 24 Jan 2011 19:14:16 GMT]]></title><description><![CDATA[<p>314159265358979 schrieb:</p>
<blockquote>
<p>Ich würde assert verwenden.</p>
</blockquote>
<p><img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44d.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_up"
      title=":+1:"
      alt="👍"
    /></p>
<p>Obwohl die &quot;Fehlerbehandlung&quot; hier natürlich auch in's Gesamtkonzept der Anwendung passen muss/sollte. So generell.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2011437</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2011437</guid><dc:creator><![CDATA[sdfsdf]]></dc:creator><pubDate>Mon, 24 Jan 2011 19:14:16 GMT</pubDate></item><item><title><![CDATA[Reply to Datum erhöhen, die fünfte Version on Tue, 25 Jan 2011 14:07:57 GMT]]></title><description><![CDATA[<p>Um die Gedanken meiner Vorposter etwas auszuführen:</p>
<pre><code class="language-cpp">Date::Date(int y, Month m, int d):y(y),m(m),d(d) // checking for valid date and                                                                   
{                                                // does the initialization 
    if(y&lt;1200 || y&gt;2200 || d&lt;1 || d&gt;31) 
    { 
        this-&gt;y = 0; 
        this-&gt;d = 0; 
    } 
}
</code></pre>
<p>Diese Art von &quot;Fehlerbehandlung&quot; ist das Allerschlimmste, was man machen kann. Wenn ich ein Datum erstelle und nichts passiert, erwarte ich, dass das Objekt das von mir angegebene Datum enthält. Dass aber einfach so irgendwelche willkürlichen anderen Daten zugewiesen werden – dann noch ungültige (welcher Monat hat einen nullten Tag?) und inkonsistente (Jahr und Tag wird verändert, aber Monat bleibt?) – ist sehr schlecht und intransparent für den Benutzer. Es kann hingegen angebracht sein, wenn ein sinnvoller Default-Wert als Fallback existiert, was hier nicht gegeben ist.</p>
<p>Nimm wie schon angetönt einen sinnvollen Fehlerbehandlungsmechanismus. Das können Exceptions sein, wobei dann Leute auf die Idee kommen, damit Logikfehler abzufangen. Ich würde auch <code>assert</code> nehmen, ein falsches Datum hat sofort bemerkt zu werden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2011861</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2011861</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Tue, 25 Jan 2011 14:07:57 GMT</pubDate></item></channel></rss>