Question

this is my first SO post. I am very new to programming, and with C++ I thought I might try and make a program that allows the user to submits a block of text (max 500 characters), allows them to enter a 4 letter word and the program return with the amount of times it picks that word up in the text. I am using X-code and it keeps making a green breakpoint and pausing the program at the 'for' loop function. my code is shown below:

#include <iostream>
#include <string>
#include <math.h>
#define SPACE ' '(char)
using namespace std;

//Submit text (maximum 500 characters) and store in variable
string text;
string textQuery(string msgText) {
do {
cout << msgText << endl;
    getline(cin, text); } while (text.size() > 500);
return text;
}
//Query word to search for and store as variable
string word;
string wordQuery(string msgWord) {

cout << msgWord << endl;
cin >> word;
return word;
}
//Using loop, run through the text to identify the word
int counter = 0;
bool debugCheck = false;
int searchWord() {

for (int i = 0; i < text.size(); i++) {
    char ch_1 = text.at(i);
    char ch_2 = text.at(i + 1);
    char ch_3 = text.at(i + 2);
    char ch_4 = text.at(i + 3);
    cout << i;

    if(ch_1 == word.at(0) &&
       ch_2 == word.at(1) &&
       ch_3 == word.at(2) &&
       ch_4 == word.at(3) )
    {

        counter++;
        debugCheck = true;

    }


}

return counter;
}
//cout the result
int main() {
string textUserSubmit = textQuery("Please submit text (max 500 characters): ");
string wordUserSubmit = wordQuery("Please select a word to search for: ");
int counterResponse = searchWord();
cout << debugCheck << endl;
cout << "The number of times is: " << counterResponse << endl;
return 0;
}

I get the error at the for loop. Any other advice about how i can make my program work for different words, multiple lengths of words and also how i can highlight the words in text would be helpful. I really would appreciate if someone could aid me with my problem. Thanks!

Was it helpful?

Solution

I get the error at the for loop.

You should describe the error you get. I happen to have access to Xcode so I can run your code and see what happens, but you should try to spare that of people from whom you want help.

In this case you should describe how the debugger stops the program at the line:

char ch_4 = text.at(i + 3);

includes the message: "Thread 1: signal SIGABRT" and the console output shows

libc++abi.dylib: terminating with uncaught exception of type std::out_of_range: basic_string

Your problem is this: the for loop checks to make sure that i is in the correct range for the string text before using it as an index, but then you also use i+1, i+2, and i+3 as indices without checking that those values are also valid.

Fix that check and the program appears to run fine (given correct input).


Some miscellaneous comments.

  • Use more consistent indentation. It makes the program easier to read and follow. Here's how I would indent it (using the tool clang-format).
  • #define SPACE ' '(char) looks like a bad idea, even if you're not using it.
  • using namespace std; is usually frowned on, though as long as you don't put it in headers it usually won't cause too much trouble. I still could though, and because you probably won't understand the resulting error message you may want to avoid it anyway. If you really don't like writing std:: everywhere then use more limited applications such as using std::string; and using std::cout;.
  • global variables should be avoided, and you can do so here by simply passing textUserSubmit and wordUserSubmit to searchWord().
  • there's really no need to make sure text is less than or equal to 500 characters in length. You're using std::string, so it can hold much longer input.
  • You never check how long word is even though your code requires it to be at least 4 characters long. Fortunately you're using at() to index into it so you don't get undefined behavior, but you should still check. I'd remove the check in textQuery and add one to wordQuery.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top