good day everyone,
i have make program suppose following:
1) asks user input string.
2) asks user input integer (lets call 'n').
3) replaces each character in string character ahead 'n' in alphabet.
4) prints new string console.
for example: if string = abc , integer = 1 result bcd.
if string = hello , integer = 4 result lipps.
if string = welcome-2-c++ , integer = 13 result jrypbzr-2-p++ (special characters $,+,/ remain unchanged).
i've written code works properly:
(code removed)
except works 1 time. console looks like:
please insert string convert: abc please enter modification integer: 2 resulting string is: cde please insert string convert: abc please enter modification integer: 2 resulting string is: please insert string convert:
as can see, second time program runs, there's no result.
why program work first time runs?
p.s. i've done of own debugging , seems "for loop" skipped second time program runs. (?)
edit: think might have memory allocation?
you aren't initializing loop variable:
for (int i; < input_string.length(); i++) {
should int = 0;
.
also, convertstring
can massively simplified:
string convertstring(string input_string, int mod_int) { (char& c : input_string) { if (std::isupper(c)) { c = 'a' + (c - 'a' + mod_int) % 26; } else if (std::islower(c)) { c = 'a' + (c - 'a' + mod_int) % 26; } } return input_string; }
Comments
Post a Comment