gpt4 book ai didi

c++ - 如果 `ConfirmChoice` 函数为假,如何从 `main 中取消?

转载 作者:行者123 更新时间:2023-11-30 02:59:37 25 4
gpt4 key购买 nike

在开始其余代码之前,我需要从主函数中取消ConfirmChoice。我已经尝试了很多,但无法解决这个问题。

#include <iostream>
#include <iomanip>
using namespace std;

const char CASH = 'C';
const char CREDIT = 'D';
const char NOSEBLEED = 'N';
const char BOX_SEAT = 'B';
const char FIFTY_YARD_LINE = 'F';
const char STUDENT_SECTION = 'S';
const float NOSEBLEED_PRICE = 43.42;
const float BOX_SEAT_PRICE = 353.85;
const float FIFTY_YARD_LINE_PRICE = 94.05;
const float STUDENT_SECTION_PRICE = 19.99;

//---------------------------------------------------------------
// Function: ConfirmChoice
// Purpose: Confirms the users ticket purchase before processing payment
// Parameters: TicketType - The type of ticket selected
// Returns: true if the user confirms the selection, false otherwise
//--------------------------------------------------------------
bool ConfirmChoice(const char TicketType)
{
char Choice;
bool Confirmed;

// Print out their selection
cout << "\nYou have chosen to purchase ";
cout << fixed << setprecision(2);
switch (TicketType)
{
case NOSEBLEED:
cout << "Nosebleed ticket(s) at a price of $";
cout << NOSEBLEED_PRICE << ".\n";
break;
case BOX_SEAT:
cout << "Box Seat ticket(s) at a price of $";
cout << BOX_SEAT_PRICE << ".\n";
break;
case FIFTY_YARD_LINE:
cout << "Ticket(s) on the 50 yard line at a price of $";
cout << FIFTY_YARD_LINE_PRICE << ".\n";
break;
case STUDENT_SECTION:
cout << "Ticket(s) in the Student Section at a price of $";
cout << STUDENT_SECTION_PRICE << ".\n";
break;
}

// Confirm the selection
cout << "Do you wish to confirm your purchase? Enter Y or N: ";
cin >> Choice;
Choice = toupper(Choice);
while (Choice != 'Y' && Choice != 'N')
{
cout << "Invalid selection. Please enter either Y or N: ";
cin >> Choice;
Choice = toupper(Choice);
}
Confirmed = (Choice == 'Y');

// Check confirmation
if (Confirmed)
cout << "You have confirmed your choice.\n" << endl;
else
cout << "You not confirmed your choice.\n" << endl;
return (Confirmed);
}

//-------------------------------------------
// Function: CalculateChange
// Purpose: To output the change due
// Parameters: ChangeDue - The amount of change needed
// Returns: Nothing
//-------------------------------------------
void CalculateChange(const float ChangeDue)
{
int Change = 0;
int Dollars = 0;
int Quarters = 0;
int Dimes = 0;
int Nickels = 0;
int Pennies = 0;

// Compute change
Change = ChangeDue * 100;
Dollars = Change / 100;
Change = Change % 100;
Quarters = Change / 25;
Change = Change % 25;
Dimes = Change / 10;
Change = Change % 10;
Nickels = Change / 5;
Pennies = Change % 5;

// Print out change
cout << "Your change is \n\t";
cout << Dollars << " Dollars\n\t";
cout << Quarters << " Quarters\n\t";
cout << Dimes << " Dimes\n\t";
cout << Nickels << " Nickels\n\t";
cout << Pennies << " Pennies\n";
}

int main()
{
// Prints my name and UAID
cout <<"\n\nName: Ivory Newbern\n";
cout << "UAID: 010563918\n";

// Declarations

char TChoice ; // Ticket type: Nosebleed, box seats etc..
char PChoice = GetPaymentType(); // Payment choice: cash or credit card
bool Confirmed ; // Did the user confirm the selection
float Cost ; // The cost of the ticket puchased
float ChangeDue; // The amount of change owed (for cash purchases)



// Get the choice of payment type
PChoice;

// Get the choice of ticket type
GetTicketType( TChoice);
// Confirm the selection
ConfirmChoice( TChoice);

// If they confirm the purchase


// Calls functions to figure out the price of ticket purchase(s)

if (TChoice == BOX_SEAT)
Cost = CalculateCost(BOX_SEAT_PRICE);
if (TChoice == NOSEBLEED)
Cost = CalculateCost(NOSEBLEED_PRICE);
if (TChoice == STUDENT_SECTION)
Cost = CalculateCost(STUDENT_SECTION_PRICE);
if (TChoice == FIFTY_YARD_LINE)
Cost = CalculateCost(FIFTY_YARD_LINE_PRICE);

// Handles the payment

if( PChoice == 'C');
PayWithCash(Cost,ChangeDue) , CalculateChange(ChangeDue);
if( PChoice == 'D')
PayWithCredit(Cost);

// Say goodbye to the customer
if (TChoice == BOX_SEAT)
cout << "Thank you for the purchase of your 'BOX_SEAT' tickets ,have a nice day!" << endl;
if (TChoice == NOSEBLEED)
cout << "Thank you for the purchase of your 'NOSEBLEED' tickets ,have a nice day!" << endl;
if (TChoice == STUDENT_SECTION)
cout << "Thank you for the purchase of your 'STUDENT_SECTION' tickets ,have a nice day!" << endl;
if (TChoice == FIFTY_YARD_LINE)
cout << "Thank you for the purchase of your 'FIFTY_YARD_LINE' tickets ,have a nice day!" << endl;


// Else

// Cancel the purchase
system ("PAUSE");
return 0;
}

最佳答案

像这样:

if( ConfirmChoice( TChoice));

{

if (TChoice == BOX_SEAT)
Cost = CalculateCost(BOX_SEAT_PRICE);
if (TChoice == NOSEBLEED)
Cost = CalculateCost(NOSEBLEED_PRICE);
if (TChoice == STUDENT_SECTION)
Cost = CalculateCost(STUDENT_SECTION_PRICE);
if (TChoice == FIFTY_YARD_LINE)
Cost = CalculateCost(FIFTY_YARD_LINE_PRICE);

// Handles the payment

if( PChoice == 'C');
PayWithCash(Cost,ChangeDue) , CalculateChange(ChangeDue);
if( PChoice == 'D')
PayWithCredit(Cost);

// Say goodbye to the customer
if (TChoice == BOX_SEAT)
cout << "Thank you for the purchase of your 'BOX_SEAT' tickets ,have a nice day!" << endl;
if (TChoice == NOSEBLEED)
cout << "Thank you for the purchase of your 'NOSEBLEED' tickets ,have a nice day!" << endl;
if (TChoice == STUDENT_SECTION)
cout << "Thank you for the purchase of your 'STUDENT_SECTION' tickets ,have a nice day!" << endl;
if (TChoice == FIFTY_YARD_LINE)
cout << "Thank you for the purchase of your 'FIFTY_YARD_LINE' tickets ,have a nice day!" << endl;

}
else
{
system ("PAUSE");
return 0;
}<br/>

为了优化目的:
这里的 else 部分不是必需的,你可以简单地删除 else 部分,如下所示:

if( ConfirmChoice( TChoice));

{

if (TChoice == BOX_SEAT)
Cost = CalculateCost(BOX_SEAT_PRICE);
if (TChoice == NOSEBLEED)
Cost = CalculateCost(NOSEBLEED_PRICE);
if (TChoice == STUDENT_SECTION)
Cost = CalculateCost(STUDENT_SECTION_PRICE);
if (TChoice == FIFTY_YARD_LINE)
Cost = CalculateCost(FIFTY_YARD_LINE_PRICE);

// Handles the payment

if( PChoice == 'C');
PayWithCash(Cost,ChangeDue) , CalculateChange(ChangeDue);
if( PChoice == 'D')
PayWithCredit(Cost);

// Say goodbye to the customer
if (TChoice == BOX_SEAT)
cout << "Thank you for the purchase of your 'BOX_SEAT' tickets ,have a nice day!" << endl;
if (TChoice == NOSEBLEED)
cout << "Thank you for the purchase of your 'NOSEBLEED' tickets ,have a nice day!" << endl;
if (TChoice == STUDENT_SECTION)
cout << "Thank you for the purchase of your 'STUDENT_SECTION' tickets ,have a nice day!" << endl;
if (TChoice == FIFTY_YARD_LINE)
cout << "Thank you for the purchase of your 'FIFTY_YARD_LINE' tickets ,have a nice day!" << endl;

}

关于c++ - 如果 `ConfirmChoice` 函数为假,如何从 `main 中取消?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12774995/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com