I am writing a code that prompts the user to enter a part # that is 4 or 5 characters long. The 2nd and 3rd characters must contain either of: MS, MP, FS, FO or UP. If the user doesn't enter atleast 4 or 5 characters or the 2nd and 3rd characters don't containt one of the following above then an Error message should be displayed. If the part # is correct then the program will display the appropriate delivery method. I have just started this program and am stuck on how to code the program where it identifies the 2nd and 3rd characters correctly. My code is below. Thanks for any input!
/*Intermediate26.cpp - This program allows the user to enter a
part number that consist of four or five characters in which the
second and third characters represents the following delivery method:
MS=Mail-Standard, MP=Mail-Priority, FS=FedEx-Standard, FO=FedEx-Overnight & UP=UPS.
If neither guidelines are met an appropriate message is displayed.
Created by Scott Knight on April 22, 2014*/
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
//Declare variables
string partNO = "";
int character = 0;
//Get part # from user
cout << "Please enter a 4 or 5 character part #: "
<< "\nThe 2nd and 3rd characters should contain the delivery method below: "
<< "\nMS=Mail-Standard, MP=Mail-Priority, FS=FedEx-Standard, FO=FedEx-Overnight, UP=UPS "
<< "\n(enter -1 to end program) " << endl;
cin >> partNO;
//Begin loop
while (partNO != "-1")
{
//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] && partNO[2] == 'MS')
cout << "The delivery method is Mail-Standard. ";
elseif (partNO[1] && partNO[2] == 'MP')
cout << "The delivery method is Mail-Priority. ";
elseif (partNO[1] && partNO[2] == 'FS')
cout << "The delivery method is FedEx-Standard. ";
elseif (partNO[1] && partNO[2] == 'FO')
cout << "The delivery method is FedEx-Overnight. ";
elseif (partNO[1] && partNO[2] == 'UP')
cout << "The delivery method is UPS. ";
}
cout << "Please enter a 4 or 5 character part #: "
<< "\nThe 2nd and 3rd characters should contain the delivery method below: "
<< "\nMS=Mail-Standard, MP=Mail-Priority, FS=FedEx-Standard, FO=FedEx-Overnight, UP=UPS "
<< "\n(enter -1 to end program) " << endl;
cin >> partNO;
}//end while
system("pause");
return 0;
}
int main()
{
//Declare variables
string partNO = "";
int character = 0;
//Get part # from user
cout << "Please enter a 4 or 5 character part #: "
<< "\nThe 2nd and 3rd characters should contain the delivery method below: "
<< "\nMS=Mail-Standard, MP=Mail-Priority, FS=FedEx-Standard, FO=FedEx-Overnight, UP=UPS "
<< "\n(enter -1 to end program) " << endl;
cin >> partNO;
//Begin loop
while (partNO != "-1")
{
//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. ";
elseif (partNO[1] == 'M' && partNO[2] == 'P')
cout << "The delivery method is Mail-Priority. ";
elseif (partNO[1] == 'F' && partNO[2] == 'S')
cout << "The delivery method is FedEx-Standard. ";
elseif (partNO[1] == 'F' && partNO[2] == 'O')
cout << "The delivery method is FedEx-Overnight. ";
elseif (partNO[1] == 'U' && partNO[2] == 'P')
cout << "The delivery method is UPS. ";
else
cout << "Error: Wrong delivery method. ";
}
cout << "Please enter a 4 or 5 character part #: "
<< "\nThe 2nd and 3rd characters should contain the delivery method below: "
<< "\nMS=Mail-Standard, MP=Mail-Priority, FS=FedEx-Standard, FO=FedEx-Overnight, UP=UPS "
<< "\n(enter -1 to end program) " << endl;
cin >> partNO;
}//end while
system("pause");
return 0;
}