C++ in Xcode pausing -


this first post. new programming, , c++ thought might try , make program allows user submits block of text (max 500 characters), allows them enter 4 letter word , program return amount of times picks word in text. using x-code , keeps making green breakpoint , pausing program @ 'for' loop function. code shown below:

#include <iostream> #include <string> #include <math.h> #define space ' '(char) using namespace std;  //submit text (maximum 500 characters) , store in variable string text; string textquery(string msgtext) { { cout << msgtext << endl;     getline(cin, text); } while (text.size() > 500); return text; } //query word search , store variable string word; string wordquery(string msgword) {  cout << msgword << endl; cin >> word; return word; } //using loop, run through text identify word int counter = 0; bool debugcheck = false; int searchword() {  (int = 0; < 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 result int main() { string textusersubmit = textquery("please submit text (max 500 characters): "); string wordusersubmit = wordquery("please select word search for: "); int counterresponse = searchword(); cout << debugcheck << endl; cout << "the number of times is: " << counterresponse << endl; return 0; } 

i error @ loop. other advice how can make program work different words, multiple lengths of words , how can highlight words in text helpful. appreciate if aid me problem. thanks!

i error @ loop.

you should describe error get. happen have access xcode can run code , see happens, should try spare of people whom want help.

in case should describe how debugger stops program @ line:

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

includes message: "thread 1: signal sigabrt" , console output shows

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

your problem this: for loop checks make sure i in correct range string text before using index, use i+1, i+2, , i+3 indices without checking values valid.

fix check , program appears run fine (given correct input).


some miscellaneous comments.

  • use more consistent indentation. makes program easier read , follow. here's how indent (using tool clang-format).
  • #define space ' '(char) looks bad idea, if you're not using it.
  • using namespace std; frowned on, though long don't put in headers won't cause trouble. still though, , because won't understand resulting error message may want avoid anyway. if don't writing std:: everywhere use more limited applications such using std::string; , using std::cout;.
  • global variables should avoided, , can here passing textusersubmit , wordusersubmit searchword().
  • there's no need make sure text less or equal 500 characters in length. you're using std::string, can hold longer input.
  • you never check how long word though code requires @ least 4 characters long. fortunately you're using at() index don't undefined behavior, should still check. i'd remove check in textquery , add 1 wordquery.

Comments

Popular posts from this blog

c++ - How to add Crypto++ library to Qt project -

jQuery Mobile app not scrolling in Firefox -

How to use vim as editor in Matlab GUI -