Print out a string backwards

I don't understand why this is not working...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>

using namespace std;

int main(){

	string mystr = "";
	cout << "Please enter something: ";
	getline(cin,mystr);

	for (int i = mystr.size() - 1; i = 0; i--){
		cout << mystr[i];
	}

	system("pause");
	return 0;
}
Last edited on
i=0 is assignment operator not a relatonal, so it shoould be for(i=mystr.size-1;i==0;i++)
And if you do that it will only loop once when i is equal to 0, I think
Just tried using the "==" ... still doesn't output anything. -.-
cout<<mystr[i] only execute once when i is 0 --
So when i is more or less than 0 it'll not executed
Why is that?

If it is printing out mystr[i], and i = mystr.size()-1, then shouldn't it print
mystr[mystr.size()-1] ?

Sorry, I just realized I was being a total idiot. I understand now. Thanks.
It should have been:
for (int i = mystr.size() - 1; i >=0; i--)
Ahaha no prob :)
Topic archived. No new replies allowed.