c++ letters to numbers, weird error

I'm getting a really weird error on my program.
when I punch my input, I get

"Debug Assertion Failed!
[program stuff]
line 1685
expression: string subscript out of range"
and the options
"Abort, Retry, Ignore"

I don't have 1685 lines in my code.


I've included the first digit's translation, then jumped to the output.

I'm sorry if I'm not very clear, I'm really tired and words are failing me :p

the question that I'm being asked to do:

To make a telephone number easier to remember, some companies use letters to show their telephone number.
In some cases, to make a telephone number meaningful, companies might use more than seven letters.
Write a program that prompts the user to enter a telephone number expressed in letters and outputs the corresponding telephone number in digits.
If the user enters more than seven letters, then process only the first seven letters. Also output the - (hyphen)after the third digit.
Allow the user to use both uppercase and lowercase letters as well as spaces between words.



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

#include <iostream>
#include <string>


using namespace std;
int main()
{
	cout << "Please input your phone number to translate" << endl;
	string digits;
	getline(cin, digits);
	int i = 0;
	string actual_digits;
	for (unsigned int i = 0; i < 12; i++)
		int actual_digits = 0;
	int length = 12;
	for (int i = 0; i < length; i++)

		switch (digits[1])	{
		(digits); {
		default: actual_digits = digits;
		case 'a':
		case 'b':
		case 'c':
			actual_digits = '2';
			break;

		case 'd':
		case 'e':
		case 'f':
			actual_digits = '3';
			break;

		case 'g':
		case 'h':
		case 'i':
			actual_digits = '4 ';
			break;

		case 'j':
		case 'k':
		case 'l':
			actual_digits = '5';
			break;

		case 'm':
		case 'n':
		case 'o':
			actual_digits = '6';
			break;

		case 'p':
		case 'q':
		case 'r':
		case 's':
			actual_digits = '7';
			break;

		case 't':
		case 'u':
		case 'v':
			actual_digits = '8';
			break;

		case 'w':
		case 'x':
		case 'y':
		case 'z':
			actual_digits = '9';
			break;

		}
	}

if (i == 4 || i == 8){
		cout << "-";
	}
	cout << "the number you have entered is, in all numbers, " << actual_digits[i] << endl;


	system("pause");
	return 0;
}
line 1685
expression: string subscript out of range"
I don't have 1685 lines in my code.

The range checking happens in a different file.

cout << "the number you have entered is, in all numbers, " << actual_digits[i] << endl;
I guess i is greater than the length of the string.
closed account (48T7M4Gy)
scalezors Your program runs in VS2015 successfully. Try a clean and rebuild maybe?
If the user just hits enter at the prompt on line 11, then 1 will not be a valid index for digits on line 19.
closed account (48T7M4Gy)
Given that line 1681 is not in this program, that (unintended) input reveals it is a Debug assertion at line 1681 in ... xstring

Problem solved! Blank input not allowed for. Check for zero length or similar.
closed account (48T7M4Gy)
1
2
if (digits.length() == 0)
		cout << "Error";
sorry, y'all, I haven't been near my laptop.
thanks for the help! I feel really silly, haha
Topic archived. No new replies allowed.