new here

because length starts counting from 1 so the first character is outside the string (since it's backwords)

s o m e t h i n g (random ascii character here)
0 1 2 3 4 5 6 7 8 9

counting from 9 to 1

� g n i h t e m o
9 8 7 6 5 4 3 2 1


note the missing 'S'


length() starts from 1

when you do line = ("something") with line.length() it returns 9 then when you loop with cout << line[9]; you're reading memory from something else which you haven't written, thus it explains the weird random character. the loop completes before the last character (which would be the 10th cycle) is shown .

to fix that you could do while (counter > -1) Which would let it read the first character 'S'

and to fix the random ascii character you could do

counter = line.length();
counter--;
while (counter > 0) {
cout << line[counter];
if (counter == -1);
else counter--;}

notice the bold and italic, this line sets the range from 8 to 0. which you want.

hopefully that helps and sorry if it was too late
 
Back
Top Bottom