W
ThomasLanger schrieb:
Auch wenn ihr diese Frage nicht mehr lesen könnt, stelle ich sie in der Hoffnung eine Hilfe zu bekommen:
cout << "Bitte Von-Bis eingeben! " << endl;
cout << "Von: "; cin >> x;
y=x;
cout << "Bis: "; cin >> y;
wenn bei bis die Enter Taste gedrückt wird, soll gelten dass y=x ist.
Bisher alles versucht, was man so findet, also abfragen ob Eingebe leer ist.
Hallo Thomas,
ich habe vor Jahren mal das kleine is_endl gebaut, welches Dein Problem lösen sollte:
#include "is_endl.h" // (s.u.)
#include <iostream>
#include <limits> // numeric_limits
int main()
{
using namespace std;
int x, y;
cout << "Bitte Von-Bis eingeben! " << endl;
cout << "Von: "; cin >> x;
cin.ignore( numeric_limits< streamsize >::max(), '\n' ); // überliest den Rest der 1.Zeile
y=x;
cout << "Bis: ";
if( !is_endl( cin ) ) // keine leere Eingabe
cin >> y; // dann lese den 'bis'-Wert
cout << x << " " << y << endl;
return 0;
}
Das Thema IO unter C++ krankt auch ein wenig am vorhandenen Input - sprich Literatur. Aber das ist ein anderes Thema.
anbei noch mal der aktuelle Code von is_endl (ich lerne auch noch dazu):
// -- Datei is_endl.h
#ifndef SAM_IS_ENDL_H_
#define SAM_IS_ENDL_H_
#include <istream>
#include <locale>
#include <streambuf>
template< typename E, typename Traits >
bool is_endl( std::basic_istream< E, Traits >& in )
{
if( in.eof() ) // EOF vorher abfangen, da sentry den Stream auf fail setzt, falls eof()==true ist!
return false;
typename std::basic_istream< E, Traits >::sentry ok( in, true ); // true := noskipws
if( ok )
{
std::ios_base::iostate state = std::ios_base::goodbit;
try
{
const bool skipws_ = in.flags() & std::ios_base::skipws;
const std::ctype< E >& ct = std::use_facet< std::ctype< E > >( in.getloc() );
for( typename std::basic_istream< E, Traits >::int_type m = in.rdbuf()->sgetc(); ; m = in.rdbuf()->snextc() )
{
if( Traits::eq_int_type( Traits::eof(), m ) )
{
state |= std::ios_base::eofbit;
break;
}
const E c = Traits::to_char_type( m );
if( c == ct.widen( '\n' ) )
{
in.rdbuf()->sbumpc(); // consume the char (here LF)
return true;
}
if( !skipws_ || !ct.is( std::ctype_base::space, c ) )
break; // noskipws || readable char follows
}
}
catch( ... )
{
state |= std::ios_base::badbit;
if( in.exceptions() & std::ios_base::badbit )
throw; // re-throw
}
in.setstate( state );
}
return false;
}
#endif // ifndef SAM_IS_ENDL_H_
Gruß
Werner