String subscript out of range..

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

string ReadAndValidateUserLastName (string userLastName)
{
	bool check = false;

	while (!check)
	{
		check = true;

		cout << "Enter Last Name: ";
		getline (cin, userLastName);

		if (userLastName.length () > LAST_NAME_LENGTH)
		{
			check = false;
		}
	
		for (unsigned int i = 0; i < userLastName.length(); i++)
		{
			if (!isalpha (userLastName [i]) || isspace (userLastName [i]))
			{
				check = false;
			}
		}

		if (!check)
		{
			cout << "Invalid Entry! Please try again." << endl;
		}
	}

	userLastName [0] = toupper (userLastName [0]);

	return userLastName;
}
this function is fine,u can also have check that entered name length is not zero and i don't know how exactly you are using this function

ok,i guess u are trying to do, using pass by reference..

if so
change function like

1
2
3
4
5
6
void ReadAndValidateUserLastName (string& userLastName) // remove return

//so in main()
string getTestName;
ReadAndValidateUserLastName(getTestName);


or if u trying to use this function by return type
1
2
3
4
string ReadAndValidateUserLastName () //function

//so in main()
string getTestName = ReadAndValidateUserLastName();
and also

There is problem with "getline"after a "cin>>" input ,u can check this below link and i have seen some topic's about this in this forum also

http://mathbits.com/MathBits/CompSci/APstrings/APgetline.htm
Last edited on
Topic archived. No new replies allowed.