I have a program that prints out the characters of a string using a for-loop. It must also print the same characters in reverse, which is where I'm having problems. Can someone help me figure out why the second for-loop isn't executing?
int main()
{
string myAnimal;
cout << "Please enter the name of your favorite animal.\n";
cin >> myAnimal;
// This loop works fine
int i;
for(i = 0; i < myAnimal.length(); i++){
cout << myAnimal.at(i) << endl;
}
// This one isn't executing
for(i = myAnimal.length(); i > -1; i--){
cout << myAnimal.at(i) << endl;
}
return 0;
}
@Lokno already provided you with the correct answer. However, let me nitpick your code a bit more to show you some other alternatives and to correct some minor mistakes.
First, you didn't actually post a compiling example, because you forgot to show the included headers
<iostream>
and<string>
and also didn't show theusing namespace std;
that was implicit in your code.Second, for the regular
for
loop, prefer to keep the loop variable inside the loop, unless you actually need to use it as a return value. Also prefer pre-increment++i
over post-incrementi++
. Furthermore, because you have made sure of the correct loop indices, there is no reason to use the bounds-checked element accessat()
over the unchecked[]
version.In C++11, you have the range-for loop which allows for even shorter and more fool-proof code, where I also used
auto
where you could have usedchar
. Unfortunately, there is no reverse range-for loop. The correct index-based reverse for loop is probably easier to read if you usei >= 0
rather thani > -1
.Then there is an algorithm based loop using
std::copy
where you use the iterator interface ofstd::string
(in particular the reverse iteratorsrbegin()
andrend()
) to copy each character through anostream_iterator
that is bound to standard output.BTW, I used the separator
"|"
rather than the newline to see stuff more easier, adapt to your taste. In any case, usingstd::endl
can have performance implications because it flushes the output buffer every time.Live Example. PS:
main()
implicitly returns0
upon success.You could use reverse iterators
Note that you have to increment the variable 'c' (and not decrement it) since this is a reverse iterator.
Instead of
write
In your code you try to access an element of the string that is beyond the acceptable range that is equal to [0, length() - 1]
Also instead of type int it is better to use the type that std::string provides for the return type of member function length that is std::string::size_type.
this doesn't do anything. you fetch the value and then throw it away.
Did you mean
i = myAnimal.length() - 1
?You need to assign i initially to the length minus one, or the last index value in the array.
Because the character positions start at 0, the last character of
myAnimal
is at position(myAnimal.length()-1)
notmyAnimal.length()
so you want to start the second loop there.