String function help

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!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*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>
using namespace 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. ";
				else if (partNO[1] && partNO[2] == 'MP')
					cout << "The delivery method is Mail-Priority. ";
				else if (partNO[1] && partNO[2] == 'FS')
					cout << "The delivery method is FedEx-Standard. ";
				else if (partNO[1] && partNO[2] == 'FO')
					cout << "The delivery method is FedEx-Overnight. ";
				else if (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;
}
Instead of
 
if (partNO[1] && partNO[2] == 'MS')
write
 
if (partNO[1] == 'M' && partNO[2] == 'S')


You can also use the compare member function if you want.
http://en.cppreference.com/w/cpp/string/basic_string/compare
 
if (partNO.compare(1, 2, "MS") == 0)
Ahh, thank you Peter!! I will give it a try and update. ;)
Peter, all it is doing is looping back???

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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. ";
				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. ";
		}
		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;
}
Compare the string length to an integer instead of a character.
Great! Thank you Peter, That was it!
Topic archived. No new replies allowed.