Hi Everybody,

If someone could help me with this problem, I would really appreciate it. I'm a newbie and trying to finish an assignment. I've gotten stuck on one thing and can't seem to find a way out of it. I've already done the searches and spent at least 5-6 hours on this little problem alone. I imagine it's something easy to solve for you guys:

In the code below, every thing works fine until I try to increment the 'k' variable (trying to read through a string character by character). Then I get the following warning:

Expression: string subscript out of range

Does anybody know what I can do to solve this?

int main()
{

string keyword;
int check;
int i=0;
int k=0;


char abcd[ARRAY_SIZE] = "ABCDEFGHIJKLMNOPQRSTUVWXY";


ifstream infile;
ofstream outfile;
infile.open ("C:\\Users\\achirri\\Documents\\MP6proj.txt");
outfile.open ("I:\\C++\\MP6proj.txt");





infile >> keyword;



char passkey[ARRAY_SIZE];


for(i = 0; i < ARRAY_SIZE; i++)
    {
		k++;
	check = check_passkey(passkey, ARRAY_SIZE, keyword[k]);
    if (check == 0)   
       passkey[i] = keyword[k];
	abcd_erase(abcd, keyword[k], ARRAY_SIZE);


    }

Recommended Answers

All 6 Replies

Code tags:

[code]

// code goes here

[/code]

So ARRAY_SIZE is 25? 26? Is it defined somewhere? It's not listed here, and a lot of other stuff isn't listed either, so it's impossible to run and it's hard to see what's going on. We also don't know what the input file's contents are, so we have no idea what keyword is. But if keyword is "house", the program's going to crash if k is 5 or greater since "house" has 5 letters (therefore it has valid character indexes of 0 through 4).

Without seeing more, I'm guessing that's your problem. If the length of keyword is less than ARRAY_SIZE, it's not going to get through the loop without crashing.

Sorry about that. Here's the full code. I thought the problem could be
figured out with that snippet.

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <cctype>

using namespace std;

const int ARRAY_SIZE=26;
const int MAX_ROW=5;
const int MAX_COL=5;


int check_passkey(char p_key[], int length, char searchitem);
void abcd_erase(char a_to_y[], char searchitem, int length);



int main()
{

string keyword;
int check;
int i=0;
int k=0;

char abcd[ARRAY_SIZE] = "ABCDEFGHIJKLMNOPQRSTUVWXY";


ifstream infile;
ofstream outfile;
infile.open ("C:\\Users\\achirri\\Documents\\MP6proj.txt");
outfile.open ("I:\\C++\\MP6proj.txt");





infile >> keyword;



char passkey[ARRAY_SIZE];


for(i = 0; i < ARRAY_SIZE; i++)
    {
		
	check = check_passkey(passkey, ARRAY_SIZE, keyword[k]);
    if (check == 0)   
		passkey[i] = keyword[k];
	abcd_erase(abcd, keyword[k], ARRAY_SIZE);
	

    }


return 0;

}


int check_passkey(char p_key[], int length, char searchitem)
{
    int loc;
    bool found = false;

    for (loc = 0; loc < length; loc++)
    if (p_key[loc] == searchitem)
        {
           found = true;
           break;
        }

    if (found)
       return 1;
    else   
       return 0;
}


void abcd_erase(char a_to_y[], char searchitem, int length)
{
   

    int loc;
    bool found = false;

    for (loc = 0; loc < length; loc++)
    if (a_to_y[loc] == searchitem)
        {
           found = true;
           break;
        }




    if(found)
      for (loc; loc < length; loc++)          //Should be length - 1 ?
        a_to_y[loc] = a_to_y[loc + 1];     

}

Oh and "keyword" is being pulled from an external file and is just a
single word, which could be anything, in this case the word is 'Phenomenon'.

Again, use code tags. See post 2 for how. Code is hard to read otherwise.

You got rid of the k++ line, so now k is always 0. Accident? What is this program supposed to do? If you have a keyword called "Phenomenon", passkey and abcd are supposed to be changed to what when you're all done?

Okay, thanks for the tips on the code tags. I will look at that. And I accidentally dropped the k++ while trying to work on this problem. Didn't mean to post without it. Sorry this is confusing. The problem I am trying to solve is to input a word from a file, to pass the characters of that word one by one to an array, without repeating any letters, so a word like 'vacuum' has only one of the 'u's put in the array. I have a function defined that parses the array to see if a letter already exists in it and passes that info back to the main function to varify. That works okay. The problem I am having is figuring out how to increment the letters of the word I am putting in the array. I was trying to use the k++ to increment through the string letter by letter, but for some reason this is not allowed. I don't get an error unless I try to increment in this way. So, I assume I must be going out of range in some way, but don't have much of a clue of what to do about it. Any kind of work around would be helpful.
Thanks.

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <cctype>

using namespace std;

const int ARRAY_SIZE=26;
const int MAX_ROW=5;
const int MAX_COL=5;


int check_passkey(char p_key[], int length, char searchitem);
void abcd_erase(char a_to_y[], char searchitem, int length);



int main()
{

string keyword;
int check;
int i=0;
int k=0;

char abcd[ARRAY_SIZE] = "ABCDEFGHIJKLMNOPQRSTUVWXY";


ifstream infile;
ofstream outfile;
infile.open ("C:\\Users\\achirri\\Documents\\MP6proj.txt");
outfile.open ("I:\\C++\\MP6proj.txt");





infile >> keyword;



char passkey[ARRAY_SIZE];


for(i = 0; i < ARRAY_SIZE; i++)
{

check = check_passkey(passkey, ARRAY_SIZE, keyword[k]);
if (check == 0)
passkey[i] = keyword[k];
abcd_erase(abcd, keyword[k], ARRAY_SIZE);
k++

}


return 0;

}


int check_passkey(char p_key[], int length, char searchitem)
{
int loc;
bool found = false;

for (loc = 0; loc < length; loc++)
if (p_key[loc] == searchitem)
{
found = true;
break;
}

if (found)
return 1;
else
return 0;
}


void abcd_erase(char a_to_y[], char searchitem, int length)
{


int loc;
bool found = false;

for (loc = 0; loc < length; loc++)
if (a_to_y[loc] == searchitem)
{
found = true;
break;
}




if(found)
for (loc; loc < length; loc++) //Should be length - 1 ?
a_to_y[loc] = a_to_y[loc + 1];

}

One, if you are new to C++, I would suggest not using C-Strings at all for this. C-Strings require you to adjust the null terminator. Regular strings do not. You use both in this program. It's easy to get confused.. I would convert all C-strings to strings.

Two, some type of output would be informative. You have variables manipulated, but never displayed. A display like "Vacuum stripped of all duplicate letters is Vacum" would help. Say explicitly in comments what the display should be.

Three, keep in mind that C++ is case-sensitive. Your array is 'A' through 'Y'. 'U' is not 'u', so if you compare the two with the == operator, they are not the same. Consider using the toupper function from the cctype library to force capitalization.

Four, if you want to avoid illegal subscript errors, use the strlen function for C strings or the length function from string. If the index you are checking is greater than the length, you could have problems.

string a = "abcdefghij";
string b = "gghhi";
int i = 0;
int k = 0;

while (i < a.length() && k < b.length())
{
    char aChar = a[i];  // no subscript-out-of-range problem
    char bChar = b[k];  // no subscript-out-of-range problem
    // do stuff
}

cout << "b stripped is " << b;  // "gghhi stripped is ghi"
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.