für einige mag dies einfach scheinen, aber für mich kann ich nicht wirklich scheinen, herauszufinden, warum das nicht funktioniert. Ich weiß, dass ich jedes Mal innerhalb der while-Schleife kopieren und einfügen kann, um das gewünschte Ergebnis zu erhalten, aber mir wurde gesagt, dass wenn man etwas mehr als einmal wiederholen muss, um eine Funktion zu schreiben! Mein Code wird die Nummer doppelt drucken und obwohl jemand 8 eingeben würde, wird es trotzdem in die While-Schleife gehen. Hoffe, dass jemand erklären kann, warum mir das passiert.Funktion für Validierung Eingabe C++ einfach
int main()
{
int option = selectionHelper();
cout << selectionHelper() << endl;
cout << endl;
if(option == 8)
{
cout << "Exiting program..." << endl;
cout << endl;
cin >> option;
}
while (option != 8)
{
if (option == 1){
cout << selectionHelper() << endl;
cout << endl;
cin >> option;
}else if(option == 2){
cout << selectionHelper() << endl;
cout << endl;
cin >> option;
}else if(option == 3){
cout << selectionHelper() << endl;
cout << endl;
cin >> option;
}else if(option == 4){
cout << selectionHelper() << endl;
cout << endl;
cin >> option;
}else if(option == 5){
cout << selectionHelper() << endl;
cout << endl;
cin >> option;
}else if(option == 6){
cout << selectionHelper() << endl;
cout << endl;
cin >> option;
}else if(option == 7){
cout << selectionHelper() << endl;
cout << endl;
cin >> option;
}else{
cout << "Invalid input... Please try again..." << endl;
cout << endl;
cout << selectionHelper() << endl;
cout << endl;
cin >> option;
}//end else if statement
}//end while loop
}//end function main
und jetzt meine Funktion:
int selectionHelper()
{
int option;
cout << "1. Initialize seating for new performance." << endl;
cout << "2. View seating chart." << endl;
cout << "3. Reserve seats." << endl;
cout << "4. Calculate tickets remaining in row." << endl;
cout << "5. Calculate tickets remaining in theater." << endl;
cout << "6. Calculate total tickets sold." << endl;
cout << "7. Calculate ticket sales." << endl;
cout << "8. Exit program." << endl;
cout << "Option: " << endl;
cin >> option;
return option;
}//end selectionHelper
Vielen Dank für auf meinem Posten suchen!
Dies half mir auch zu sehen, welcher Teil des Codes den Fehler verursacht hat. Danke für die Antwort auf meinen Beitrag! – Austin
Großartig.Bitte upvote die Antworten, die Sie nützlich finden (klicken Sie auf den nach oben gerichteten Pfeil links von der Antwort). –