exception: std::out_of_range at memory location

So I am trying to write a program that input's the name of a baby, then outputs the name of the baby as well as up to 2 statements based on the baby's name.

The program compiles just fine, but when I debug it in Visual Studio 2017, I get this error at line 82:
exception: std::out_of_range at memory location 0x004FF3BC.

Looked everywhere and could not find a solution, any help would be appreciated.


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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include<iostream>
#include<string>
#include<assert.h>

using namespace std;

void welcome();
void getBabyName(string &babyName);
int findVowel(char, string);
void findHighest(int, int, int, int, int);




int main()
{

	string babyName;
	int vowelA, vowelE, vowelI, vowelO, vowelU;
	int first = 0;
	string firstString = "";
	int second = 0;
	string secondString = "";


	cout << welcome;
	cout << getBabyName;
	vowelA = findVowel('a', babyName);
	vowelE = findVowel('e', babyName);
	vowelI = findVowel('i', babyName);
	vowelO = findVowel('o', babyName);
	vowelU = findVowel('u', babyName);

	if (firstString != secondString)
	{
		cout << babyName.substr(0, babyName.find(' ')) << firstString << "," << secondString << ".";

	}
	else
	{
		cout << babyName.substr(0, babyName.find(' ')) << firstString << ".";

	}




}

void welcome()
{

	cout << "******************************************************************" << endl;
	cout << "                       BABY SAYINGS PROGRAM                       " << endl;
	cout << "                 Please Enter Your Baby’s Full Name               " << endl;
	cout << "             Program Developed by: Austin Schlusemeyer            " << endl;
	cout << "******************************************************************" << endl;

}

void getBabyName(string &babyName)
{

	cout << "Please enter the Baby's name: ";
	getline(cin, babyName);

	do
	{
		cout << "Try again";
		getline(cin, babyName);
	} while (babyName.empty() == true);
}

int findVowel(char x, string babyName)
{
	int counter = 0;
	int counterX = 0;


	do
	{
		if (babyName.at(counter) == x)
			counterX++, counter++;
		else
			counter++;
	} while (counter < babyName.length());

	return counterX;
}


void findHighest(int vowelA, int vowelE, int vowelI, int vowelO, int vowelU)
{

	int first = 0;
	string firstString = "";
	int second = 0;
	string secondString = "";
	
	if (vowelA > first)
	{
		first = second;
		vowelA = first;
		firstString = "a little bit of heaven sent down to earth";
		secondString = "this is the start of your own sweet story";

	}
	else
		if (vowelA > second)
		{
			vowelA = second;
			firstString = "this is the start of your own sweet story";
			secondString = "a little bit of heaven sent down to earth";


		}
		else
		{
			firstString = "this is the start of your own sweet story";
			secondString = "this is the start of your own sweet story";

		}

	if (vowelE > first)
	{
		first = second;
		vowelE = first;
		firstString = "everything is beautiful because we have you now";

	}
	else
		if (vowelE > second)
		{
			vowelE = second;
			secondString = "everything is beautiful because we have you now";

		}

	if (vowelI > first)
	{
		first = second;
		vowelI = first;
		firstString = "in everything you do, dream big";

	}
	else
		if (vowelI > second)
		{
			vowelI = second;
			secondString = "in everything you do, dream big";

		}

	if (vowelO > first)
	{
		first = second;
		vowelO = first;
		firstString = "oh, don't you know you will be my greatest adventure";

	}
	else
		if (vowelO > second)
		{
			vowelO = second;
			secondString = "oh, don't you know you will be my greatest adventure";

		}

	if (vowelU > first)
	{
		first = second;
		vowelU = first;
		firstString = "u are my happy";

	}
	else
		if (vowelU > second)
		{
			vowelU = second;
			secondString = "u are my happy";

		}


}
Hello Harry/Austin,

On line 18, you declare
string babyName;
The default constructor for a string creates an empty string.

The string is still empty on line 28 when you call
vowelA = findVowel('a', babyName);

Inside findVowel, you call
(babyName.at(counter) == x)
with counter == 0. This is out of range, because your string is empty.

I'm not sure why you're trying to call findVowel on an empty string. I suggest using C++ string's built in find method.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// Example program
#include <iostream>
#include <string>

int main()
{
    std::string name = "alligator";
    
    std::size_t index = name.find('i');
    if (index != std::string::npos)
    {
        std::cout << "letter i was found at index " << index << std::endl;   
    }
    else
    {
        std::cout << "letter i was not found" << std::endl;   
    }
  
}


I commend your use of a debugger; you made the problem immediately visible for us, and so I was able to give a fast answer. (Many other users don't bother saying what goes wrong when their code doesn't work.)


__________________________________________


Edit: I just realized what you were actually trying to do.
1
2
	cout << welcome;
	cout << getBabyName;

This is not how you call a function.
Please read: http://www.cplusplus.com/doc/tutorial/functions/

You should do something like this:
1
2
3
std::string babyName;
welcome();
getBabyName(babyName);


Last edited on
That worked, thank you!

(Now I have to deal with the logical errors but oh well.)
Topic archived. No new replies allowed.