Austin Austin - 1 month ago 19
C++ Question

function for validation input c++ simple

to some this might seem simple, but to me I can't really seem to figure out why this isn't working. I know I could just copy and paste every time inside the while loop to get the result I want, but I've been told that if you have to repeat something more than one time to write a function! My code will double print the number and even though someone would type 8 it will still go into the while loop. Hoping someone can explain why this is happening to me.

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


and now for my function:

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


Thank you for looking at my post!

Answer

This answer and others will tell you what code to write to fix your problem. If you should encounter similar problems in the future, try walking through your code with a debugger. (Experience helps too.) Regarding code redundancy: first, it's good that you used the selectionHelper() method. That shows you have good instincts. Here's what you can do further:

  1. Look for lines of code that are the same or differ only by a variable.
  2. Consider condensing them in the following ways:
    • Can you use a for-loop?
    • Can you put the code in a method (possibly abstracting out a variable?)
    • Can you use a broader conditional statement? (That's what you have going on here: if a number is 1, 2, 3, 4, 5, 6, or 7, it's also >0 and <7. That's why we have ranges.)

The program keeps going when they enter 8 because you ask them to input a number after they input 8! So your first chunk of code can be simpler:

int option = selectionHelper();
cout << option << endl <<endl; // <-- output option, don't call selectionHelper() here again, that's why you get double printing
if(option == 8)
{
    cout << "Exiting program..." << endl;
    cout << endl;
    //cin >> option; // <-- don't input here again!
}

The key here to simplifying your code is to recognize that you only write different code in a few cases. So, here's your while loop:

while (option != 8)
{
    if (option > 0 && option < 8){

        cout << selectionHelper() << endl;
        cout << endl;
        cin >> option;
    }else{
        cout << "Invalid input... Please try again..." << ends << endl;
       cout << selectionHelper() << endl << endl;
       cin >> option;
    }//end else if statement
}