So I have this program full of header files / functions. And in my main it primarily consists of a switch menu along with all these functions.
The user selects what he wants based of an array of class objects that are printed to the screen.
When the user is ready to checkout with the selected item.
A function is called to process the transaction.
If the user continues within this checkout function, everything works as planned and a calculation is made as well thus returning to main and calling two other functions that regard structured data (writing & reading) aka a receipt.
My problem is: If the user decides to cancel the transaction in the Checkout function then what happens is that the function ends with return; but it returns right before two other functions that record user information and prints it such as a receipt. I want to skip these functions if the user canceled.
The following code is case 1 of my switch menu within my int main();
switch (UserSelection)
{
case 1: // METAL GEAR SOLID V: THE PHANTOM PAIN.
cout << setw(10) << "Metal Gear Solid V: The Phantom Pain Description: \n " << endl;
ReadMGSGameDescriptionInfo(); // FUNCTION THAT WILL FIND
//MGSV.txt AND READ IT'S CONTENTS.
cout << string( 1, '\n' );
cout << "Rent this game? (Y/N)." << endl;
cin >> YesorNo;
YesorNo = toupper(YesorNo);
cout << string( 2, '\n' );
while (YesorNo != 'Y' && YesorNo != 'N')
{
cout << "ERROR: Invalid input." << endl;
cout << "Please enter [Y/N]: ";
cin.clear();
cin.ignore(100, '\n');
cout << string( 2, '\n' );
cin >> YesorNo;
YesorNo = toupper(YesorNo);
}
if (YesorNo == 'Y' || YesorNo == 'y')
{
Checkout();
RecordCustomerInfo(); // NOTE: I WANT TO SKIP THIS.
ReadCustomerInfo(); // AND THIS, IF THE USER CANCELED IN Checkout();
}
elseif (YesorNo != 'N' || YesorNo != 'n')
ClearScreen(); // CALL FUNCTION THAT PRINTS x15 NEW LINES.
cout << "You have been returned to the main menu." << endl;
cout << string( 3, '\n' );
break;
This following code is my abort within my Checkout function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// THIS IS WHAT HAPPENS IF THE USER INPUTS 0 TO CANCEL THE FUNCTION.
// THUS, RETURNING TO MAIN.
cout << "For how many days would you like to rent this game? " << endl;
cout << "($1.50 per day, maximum of 7 days). Input (0) to cancel!" << endl;
cout << string( 2, '\n' );
cin >> NumberofDaystoRent;
cout << string( 2, '\n' );
if (NumberofDaystoRent == 0)
{
return;
}
You could let the Checkout() function return a value indicating if it was canceled or not, and then use an if statement to check if you should run the functions.
1 2 3 4 5
if (Checkout())
{
RecordCustomerInfo();
ReadCustomerInfo();
}