function - Taking a slice out of a string incorrectly C++ -
i attempting write function outputs string of 5 characters string of concatenated primes 1000 characters long starting @ point (n) user specifies. have gotten program compile seem running errors in program , have not yet been taught of tools me see issue in programming. believe issue in getting , displaying slice itself, not concatenated string...but again, may be/probably wrong. advice appreciated.
-aspiring computer crim. major
#include<iostream> #include<string> std::string get_concatenated_primes() { unsigned int base, test, cap=1000, prime=0; std::string plist; (base=0; base < cap; ++base) { (test=2; test < base; ++test) { if (base % test == 0) break; else { prime=1; } } if (prime == 1 && plist.length() < 1000) { plist += base; } } return plist; } std::string get_slice_of_5(const std::string concat_primes, const unsigned int n) { std::string slice; slice = concat_primes.substr (n, 5); return slice; } int main() { unsigned int n; while(std::cin >> n) { std::string concat_primes = get_concatenated_primes(); std::cout << get_slice_of_5(concat_primes, n) << std::endl; } return 0; }
terminate called after throwing instance of 'std::out_of_range' what(): basic_string::substr: __pos (which 998) > this->size() (which 997)
i did not want do, should check concat_primes has more characters n.
Comments
Post a Comment