String subscript out of range error

I'm trying to create a program that takes in a number such as "12345678" or "12345678910" and formats them into "1-234-5678" or "1-(234)-567-8910" respectively. It shows no errors in the compiler but when I enter a number, I get the error message "expression string subscript out of range" and forced to abort.
Any ideas? Thank you so much in advanced.

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <iostream>
#include <string> 
#include <iomanip>
;using namespace std;

int main()
{
	string enterPhoneNumber();
	int isValid(string);
	string formatNumber(string);
	void displayResults(int, string);
	void printHeading();
	string validCode, phoneNumberCorrect, phoneNumberUnformed;
	char userAnswer = 'y';
	printHeading();
	int validCodeReturn;
	do
	{
	enterPhoneNumber();
	validCodeReturn = isValid(phoneNumberUnformed);
	   if (validCodeReturn == 0)
	 {
		 phoneNumberCorrect = formatNumber(phoneNumberUnformed);
		 displayResults(validCodeReturn, phoneNumberCorrect);
	 }
	 else 
	 {
		 displayResults(validCodeReturn, phoneNumberUnformed);
	 }
	 }while(userAnswer == 'y');
}
string enterPhoneNumber()
{
	string phoneNumberUnformed;
	cout << "Enter a phone number: ";
	getline(cin, phoneNumberUnformed);
return phoneNumberUnformed;
}
int isValid(string phoneNumber)
{
	int validCode, numberCount;
	numberCount = phoneNumber.length(); 
	if (numberCount >= 8 || numberCount <= 11)
	{
		validCode = 0;
	}
	else
	{
		validCode = 1;
	}
	return validCode;
}
string formatNumber(string phoneNumber)
{
	int numberCount;
	string temp = phoneNumber;
	int strlen = temp.length();
	numberCount = phoneNumber.length();
	if(numberCount == 8)
	{
		phoneNumber.resize(phoneNumber.length() + 2);

		phoneNumber[1] = '-';
	
		for (int i = 2; i < 5; i++)
		{
			phoneNumber[i] = temp[i - 1];
		}
		phoneNumber[5] = '-';
		for (int i = 6; i < strlen + 2; i++)
		{
			phoneNumber[i] = temp[i - 2];
		}	
		cout << phoneNumber;
	}
	else
	{
		phoneNumber.resize(phoneNumber.length() + 4);
		phoneNumber[1] = '-';
		phoneNumber[2] = '(';
	
		for (int i = 3; i < 6; i++)
		{
			phoneNumber[i] = temp[i - 2];
		}
		phoneNumber[6] = ')';
		phoneNumber[7] = '-';
		for (int i = 8; i < strlen + 3; i++)
		{
			phoneNumber[i] = temp[i - 4];
		}	
			phoneNumber[11] = '-';
		for (int i = 12; i < strlen + 5; i++)
		{
			phoneNumber[i] = temp[i - 5];
		}
	}
	return phoneNumber;
}

void displayResults(int validCode, string phoneNumber)
{
	if (validCode == 0)
	{
		cout << "Fomatted number is: " << phoneNumber << endl;
	}
	else if (validCode == 1)
	{
		cout << "Invalid phone number: " << phoneNumber << endl;
		if (phoneNumber.length() < 8 || phoneNumber.length() > 11)
		{
			cout << "A phone number must have 8 or 11 characters." << endl;
		}
		else if (*phoneNumber.begin() != '1')
		{
			cout << "A phone number must begin with a 1." << endl;
		}
		cout << "Please try again.";
	}
}
void    printHeading()
{
   cout << endl;

   cout << "Project/program name: U01_Telephone" << endl;
   cout << "by" << endl;
   cout << "Sebastian" << endl;

   cout << endl;

   // program title
   cout << "Telephone Number Verification Program";
   cout << endl << endl;

   // program introduction and directions
   cout << "\nWelcome to the Telephone Number Verification program. ";
   cout << "\nWhen prompted, please enter either an 8 digit telephone number ";
   cout << "\nor an 11 digit telephone number. Valid telephone numbers ";
   cout << "\nwill always begin with 1, always have 8 digits or 11 digits, and ";
   cout << "\ncontain only the digits 0-9." ;

   cout << endl << endl;

   return;
}
Last edited on
Without code-tags it is next to impossible to read your program and discern what you're doing wrong. Based on the headline statement of your problem here's a std::regex based approach: I'm not sure how familiar you're with std::regex, therefore I'm not sending any detailed explain at this stage but if you're interested you can start with this link:
http://www.cplusplus.com/reference/regex/ECMAScript/, google some more and then come back here if you have any queries:
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
#include <iostream>
#include <string>
#include <regex>

int main()
{
    std::regex re("1[0-9]{7}([0-9]{3})?");
    //regex object beings with 1, matches digits 8 times and, optionally, 3 more times
    std::string input{};

    bool match = false;
    while (!match)
    {
        getline(std::cin, input);
        if(std::regex_match(input,re))
        {
            match = true;
        }
        else
        {
            std::cout << "Incorrect input, try again \n";
        }
    }
    std::string phoneNumber{};
    if(input.size() == 8)
    {
        phoneNumber = input.substr(0,1) + "-" + input.substr(1,3) + "-" + input.substr(4);
    }
    else
    {
        phoneNumber = input.substr(0,1) + "-(" + input.substr(1,3) + ")-" + input.substr(4,3) + "-" + input.substr(7);
    }
    std::cout << phoneNumber << '\n';
}
Last edited on
can you isolate what line this happens @ via the debugger?
Forgive me this was my first time posting. I put in the code tags to help. The exact error message says

"Program: C:\WINDOWS\SYSTEM32\MSVCP110D.dll
File: c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring
Line: 1662

Expression: string subscript out of range"

This is all very confusing for me. Thank you gunnerfunner, I'll look into that. I'll try to use this more with my next program. Unfortunately, I have to sort of follow the pseudocode that's been given my instructor.

can you isolate what line this happens @ via the debugger?


Other than what's above I get this message following the debug:
'ConsoleApplication7.exe' (Win32): Loaded 'C:\Windows\SysWOW64\uxtheme.dll'. Cannot find or open the PDB file.
Along with several similar messages right below it.s
formatNumber is fed an empty string and you assume it was not.

Not helping the situation: isValid can only return a non-zero value since the condition on line 43 can never be false. Any value is either greater than seven or less than 12. Some values are both.

Also, on line 19 you do nothing with the value returned by enterPhoneNumber effectively ignoring whatever number the user gives you.
Topic archived. No new replies allowed.