Reputation: 29
I searched the site but I am unable to find a solution to my problem. I tried making minor changes but nothing has solved it. I keep getting "string subscript out of range" error. I do not know why. Maybe I'm blind and I'm missing a small error somewhere. Now I'm here requesting aid.
Info on program: This program will input first and last name, validate it and apply case conversion. After the first name and last name have been entered by the user, it will clear the screen and display the names the user has entered. It will then input the product rating, validate it and apply case conversion.Display a heading followed by a bar chart corresponding to the 5 product values.
Edit: I want to say thank you to the people that helped me. I solved the issue finally thankfully to you guys. I have to say that there is a great community here and the response was superb. I'm going to class now but I will post my updated code for people in the future who might have the same problem. Thank you very much again guys.
#include <iostream>
#include <string>
using namespace std;
void Name (string, string&, const int);
void Rating (string&, int[], const int);
void main()
{
const int MAX_FIRST_NAME = 20;
const int MAX_LAST_NAME = 25;
const int MAX_PRODUCTS = 5;
string firstNameQuestion = "First Name";
string lastNameQuestion = "Last Name";
string firstName;
string lastName;
string ratingString;
int ratingInt [MAX_PRODUCTS];
while (true)
{
Name (firstNameQuestion, firstName, MAX_FIRST_NAME);
if (firstName == "Quit")
break;
Name (lastNameQuestion, lastName, MAX_LAST_NAME);
if (lastName == "Quit")
break;
system ("cls");
cout << "First Name: " << firstName;
cout << endl;
cout << "Last Name: " << lastName;
cout << endl;
cout << endl;
Rating (ratingString, ratingInt, MAX_PRODUCTS);
}
}
void Name (string question, string& answer, const int MAX)
{
int count;
do
{
cout << question << " (" << MAX << " chars max. type \"quit\" to stop): ";
getline (cin, answer);
}
while (answer.empty() || answer.length() > MAX);
answer[0] = toupper (answer[0]);
for (count = 1; count < answer.length(); count++)
answer[count] = tolower ( answer[count] );
}
void Rating (string& ratingString, int ratingInt[], const int MAX)
{
int count;
int who;
for (count = 0; count < MAX; count++)
{
do
{
cout << "Rating for product no." << count + 1 << " (A to E): ";
cin >> ratingString[count];
ratingString[count] = toupper (ratingString[count]);
}
while (ratingString.empty() || ratingString.length() > 1 || ratingString[count] > 'E');
}
for (who = 0; who < MAX; who++)
{
if (ratingString[who] == 'A')
ratingInt[who] = 10;
if (ratingString[who] == 'B')
ratingInt[who] = 8;
if (ratingString[who] == 'C')
ratingInt[who] = 6;
if (ratingString[who] == 'D')
ratingInt[who] = 4;
else
ratingInt[who] = 2;
}
cout << endl;
cout << endl;
cout << "Consumer satisfaction bar chart: ";
cout << endl;
for (count = 0; count > MAX; count++)
{
cout << endl;
cout << "Product #" << count + 1 << " ";
for (who = 0; who > ratingInt[count]; who++)
cout << "*";
}
}
Upvotes: 2
Views: 239
Reputation: 1172
Line 45
Rating (ratingString, ratingInt, MAX_PRODUCTS);
the ratingString is empty. When it runs to Line76
cin >> ratingString[count];
you are referencing an index out of the boundary.
How about this edit:
char cc;
cin >> cc;
ratingString.push_back(cc);
Upvotes: 1
Reputation: 34367
I believe in the loop below, count reached to MAX
for (count = 0; count < MAX; count++)
Then again in loop below, you are using count++
and its going beyond the string ratingString
length.
for (who = 0; who < MAX; count++)
To fix the issue, either use correct the index+increment or put a check on string length as well.
for (who = 0; who < MAX && who < ratingString.length(); who++)
It's better to put the string length check in all the loops where character at string index is used.
Upvotes: 0