Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Empfohlene Antworten

Veröffentlicht

Hi!

Beim folgenden Quelltext versuche ich eine integer Variable mit cin einzulesen und

ihre Gültigkeit zu überprüfen. Wenn man nen Buchstaben ausgibt wird das

cout << "Wrong Input (0-64000 kB)!" << endl;

zu ner Endlosschleife. Wie kann ich die Eingabe von anderen Zeichen als 1-64000

rausfiltern?

while(bElementSizeCorrect == false)
{
cout << "Input size (kB): ";
cin >> nElementSize;
if((nElementSize > 1) && (nElementSize < 64000))
{
bElementSizeCorrect = true;
}else{
cout << "Wrong Input (0-64000 kB)!" << endl;
}
}[/PHP]

MfG backdraft

Nur mal als Beispiel:

Is aus einer Anwendung:


for(int len=0; len<m_strWemoNr.GetLength(); len++) {
if(m_strWemoNr.GetAt(len)<48 || m_strWemoNr.GetAt(len)>57)
{
MessageBox("Bitte nur Ziffern eingeben !");
return;
}

}
[/PHP]

Original geschrieben von backdraft

Beim folgenden Quelltext versuche ich eine integer Variable mit cin einzulesen und

ihre Gültigkeit zu überprüfen. Wenn man nen Buchstaben ausgibt wird das

cout << "Wrong Input (0-64000 kB)!" << endl;

zu ner Endlosschleife. Wie kann ich die Eingabe von anderen Zeichen als 1-64000

rausfiltern?

Das ist wieder mal das Problem "cin und numerische Datentypen".

Dadurch, dass Du cin eine numerische Variable übergibst, verbleiben die Zeichen, die cin nicht in Zahlen packen kann, für immer im Eingabepuffer. Du musst diese Zeichen zuerst entfernen:

while(bElementSizeCorrect == false)
{
cout << "Input size (kB): ";
cin >> nElementSize;
if((nElementSize > 1) && (nElementSize < 64000))
{
bElementSizeCorrect = true;
}else{
cout << "Wrong Input (0-64000 kB)!" << endl;
[COLOR=darkred][B]cin.clear();
cin.ignore( 2000, '\n' );[/B][/COLOR]
}
}[/CODE]

2000 ist dabei ein willkürlicher Wert. Es geht nur darum, alle erfolgten Eingaben bis zum nächsten Linefeed zu verwerfen.

Hi!

ich hab das

cin.clear();

cin.ignore( 2000, '\n' );

ausprobiert. Es gibt immer noch ne Endlosschleife. :(

MfG backdraft

So gehts:


#include <iostream.h>


int main()
{

int nElementSize;
short bElementSizeCorrect=false;


while(bElementSizeCorrect == false)
{

cout << "Input size (kB): ";
cin >> nElementSize;

if((nElementSize > 1) && (nElementSize < 64000))
{
bElementSizeCorrect = true;
}else
{
cout << "Wrong Input (0-64000 kB)!" << endl;
if(!cin)
{
cin.clear();
cin.ignore(100,'\n');

}


}
}


return 0;

}

[/PHP]

Immer noch ne Endlosschleife... :(

MfG backdraft

Funktioniert!!! :D

Danke!

backdraft

Erstelle ein Konto oder melde dich an, um einen Kommentar zu schreiben.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.