<?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[Mit cin beliebig viele Zahlen, eines bestimmten Zahlentyps, eingeben]]></title><description><![CDATA[<p>Hi,</p>
<p>- Ich will, dass der Nutzer beliebig viele Zahlen eingibt. Die Zahlen bekommt der Nutzer dann in einem std::vector zurückgeliefert.<br />
- Den Datentyp für die Zahlen soll der Nutzer angeben. Ist es kein geeigneter Typ für eine Zahl, soll es ein Compilerfehler geben.<br />
- Ich will keinen string-&gt;stringstream Umweg gehen.</p>
<p>Das habe ich aktuell:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;algorithm&gt;
#include &lt;iterator&gt;
#include &lt;type_traits&gt;
using namespace std;

template&lt; char C &gt;
std::istream&amp; skip_chars( std::istream&amp; in )
{
	const char line_feed = '\n';
	const char carriage_return = '\r';

	for(char c;;)
	{
		if( in &gt;&gt; c &amp;&amp; (c==line_feed || c==carriage_return) )
		{
			in.setstate( std::ios_base::eofbit );
			return in;
		}
		else if( c != C )
			return in.unget();
	}
}

template&lt; typename U, typename ... Ts &gt; struct any_of_type_helper
{
	static constexpr bool value = ( std::is_same&lt; U, Ts &gt;::value || ... );
	using type = typename std::enable_if&lt; value, U &gt;::type;
};
template&lt; typename T &gt;
using any_of_type = typename any_of_type_helper&lt; T, int, float, double &gt;::type;

template&lt; typename T &gt;
vector&lt; any_of_type&lt;T&gt; &gt; enter_some_numbers()
{
	vector&lt;T&gt; numbers;
	for( T num; cin &gt;&gt; std::noskipws &gt;&gt; skip_chars&lt;' '&gt; &gt;&gt; num; numbers.emplace_back(num) );
	return numbers;
}

int main()
{
	cout &lt;&lt; &quot;enter some numbers: &quot;;
	auto numbers = enter_some_numbers&lt;int&gt;();
	cout &lt;&lt; &quot;your input was: &quot;;
	for( auto num: numbers )
		cout &lt;&lt; num &lt;&lt; ' ';
}
</code></pre>
<p>Hat jemand Verbesserungsvorschläge oder einen ganz anderen Lösungsansatz?</p>
<p>Danke im Voraus</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/347229/mit-cin-beliebig-viele-zahlen-eines-bestimmten-zahlentyps-eingeben</link><generator>RSS for Node</generator><lastBuildDate>Tue, 15 Sep 2026 17:36:03 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/347229.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 21 Apr 2018 12:40:14 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Mit cin beliebig viele Zahlen, eines bestimmten Zahlentyps, eingeben on Sat, 21 Apr 2018 12:46:53 GMT]]></title><description><![CDATA[<p>Hi,</p>
<p>- Ich will, dass der Nutzer beliebig viele Zahlen eingibt. Die Zahlen bekommt der Nutzer dann in einem std::vector zurückgeliefert.<br />
- Den Datentyp für die Zahlen soll der Nutzer angeben. Ist es kein geeigneter Typ für eine Zahl, soll es ein Compilerfehler geben.<br />
- Ich will keinen string-&gt;stringstream Umweg gehen.</p>
<p>Das habe ich aktuell:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;algorithm&gt;
#include &lt;iterator&gt;
#include &lt;type_traits&gt;
using namespace std;

template&lt; char C &gt;
std::istream&amp; skip_chars( std::istream&amp; in )
{
	const char line_feed = '\n';
	const char carriage_return = '\r';

	for(char c;;)
	{
		if( in &gt;&gt; c &amp;&amp; (c==line_feed || c==carriage_return) )
		{
			in.setstate( std::ios_base::eofbit );
			return in;
		}
		else if( c != C )
			return in.unget();
	}
}

template&lt; typename U, typename ... Ts &gt; struct any_of_type_helper
{
	static constexpr bool value = ( std::is_same&lt; U, Ts &gt;::value || ... );
	using type = typename std::enable_if&lt; value, U &gt;::type;
};
template&lt; typename T &gt;
using any_of_type = typename any_of_type_helper&lt; T, int, float, double &gt;::type;

template&lt; typename T &gt;
vector&lt; any_of_type&lt;T&gt; &gt; enter_some_numbers()
{
	vector&lt;T&gt; numbers;
	for( T num; cin &gt;&gt; std::noskipws &gt;&gt; skip_chars&lt;' '&gt; &gt;&gt; num; numbers.emplace_back(num) );
	return numbers;
}

int main()
{
	cout &lt;&lt; &quot;enter some numbers: &quot;;
	auto numbers = enter_some_numbers&lt;int&gt;();
	cout &lt;&lt; &quot;your input was: &quot;;
	for( auto num: numbers )
		cout &lt;&lt; num &lt;&lt; ' ';
}
</code></pre>
<p>Hat jemand Verbesserungsvorschläge oder einen ganz anderen Lösungsansatz?</p>
<p>Danke im Voraus</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2555754</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2555754</guid><dc:creator><![CDATA[[[global:former_user]]]]></dc:creator><pubDate>Sat, 21 Apr 2018 12:46:53 GMT</pubDate></item><item><title><![CDATA[Reply to Mit cin beliebig viele Zahlen, eines bestimmten Zahlentyps, eingeben on Sat, 21 Apr 2018 13:05:44 GMT]]></title><description><![CDATA[<p>Was kann das jetzt besser als</p>
<pre><code>vector&lt;int&gt; data(istream_iterator&lt;int&gt;(cin), istream_iterator&lt;int&gt;());
</code></pre>
<p>?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2555755</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2555755</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Sat, 21 Apr 2018 13:05:44 GMT</pubDate></item><item><title><![CDATA[Reply to Mit cin beliebig viele Zahlen, eines bestimmten Zahlentyps, eingeben on Sat, 21 Apr 2018 13:09:55 GMT]]></title><description><![CDATA[<p>SeppJ schrieb:</p>
<blockquote>
<p>Was kann das jetzt besser als</p>
<pre><code>vector&lt;int&gt; data(istream_iterator&lt;int&gt;(cin), istream_iterator&lt;int&gt;());
</code></pre>
<p>?</p>
</blockquote>
<p>Da hänge ich in einer Schleife fest, die ich dann abbrechen müsste.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2555756</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2555756</guid><dc:creator><![CDATA[[[global:former_user]]]]></dc:creator><pubDate>Sat, 21 Apr 2018 13:09:55 GMT</pubDate></item><item><title><![CDATA[Reply to Mit cin beliebig viele Zahlen, eines bestimmten Zahlentyps, eingeben on Sat, 21 Apr 2018 13:29:14 GMT]]></title><description><![CDATA[<p>Ahh, jetzt weiß ich was du meinst, SeppJ. Ich kann ja sozusagen mit Strg+C EOF auslösen.<br />
Da hab ich wieder viel zu kompliziert gedacht. Danke dir <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>
]]></description><link>https://www.c-plusplus.net/forum/post/2555758</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2555758</guid><dc:creator><![CDATA[[[global:former_user]]]]></dc:creator><pubDate>Sat, 21 Apr 2018 13:29:14 GMT</pubDate></item></channel></rss>