C++ String Problem Write a program that allows the user to enter a part number t
ID: 3696138 • Letter: C
Question
C++ String Problem
Write a program that allows the user to enter a part number that consists of four or five characters. The second and third characters represent the delivery method, as follows: “MS” represents “Mail—Standard”, “MP” represents “Mail – Priority”, “FS” represents “FedEx – Standard”, “FO” represents “FedEx – Overnight”, and “UP” represents “UPS”. Display an appropriate message when the part number does not contain either four or five characters. Also display an appropriate message when the second and third characters are not one of the delivery methods. If the part number is vaild, the program should display the delivery method. Use the sentinel value to end the program. Save and then run the program. Test the program using the following part numbers: 7MP6, 3fs5, 2UP7, 7mS89, 9FO8, 9fo89, 8ko89, and 1234MS
Explanation / Answer
int main()
{
//Declare variables
string partNO = "";
int character = 0;
cout << "Please enter a 4 or 5 character part #: "
<< " The 2nd and 3rd characters should contain the delivery method below: "
<< " MS=Mail-Standard, MP=Mail-Priority, FS=FedEx-Standard, FO=FedEx-Overnight, UP=UPS "
<< " (enter -1 to end program) ";
cin >> partNO;
//Determine if part # is 4 or 5 characters
if (partNO.length() == '4' || partNO.length() == '5')
{
//Determine if 2nd or 3rd character is letters
if (partNO[1] == 'M' && partNO[2] == 'S')
cout << "The delivery method is Mail-Standard. ";
else if (partNO[1] == 'M' && partNO[2] == 'P')
cout << "The delivery method is Mail-Priority. ";
else if (partNO[1] == 'F' && partNO[2] == 'S')
cout << "The delivery method is FedEx-Standard. ";
else if (partNO[1] == 'F' && partNO[2] == 'O')
cout << "The delivery method is FedEx-Overnight. ";
else if (partNO[1] == 'U' && partNO[2] == 'P')
cout << "The delivery method is UPS. ";
else
cout << "Error: Wrong delivery method. ";
system("pause");
return 0;
}
O/p
Test the below strings
7MP6, 3fs5, 2UP7, 7mS89, 9FO8, 9fo89, 8ko89, and 1234MS
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.