I set up a string filled solely with numbers and using a for loop iterated through it in order to add them together mathematically (Wanted to see if the language would allow this), as a result I got some weird Numbers as the result. Can someone explain why this is happening?
int main() { std::string word = "2355412"; for (int i = 0; i<word.size(); i++){ int sum = word[i]+word[i+1]; std::cout << sum << std::endl; } return 0; } The code when run results in:
101 104 106 105 101 99 50 Due to the way I wrote my code I also believe that it should have resulted in an out of bounds error due word[i+1] on the final value resulting in the calling of a value that does not exist. Can someone explain why it did not throw an error?
word[i+1]didn't throw error because out-of-bound check is not performed there. Tryword.at(i+1)instead.