Oct 12, 2014 at 3:28am UTC
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 Oct 12, 2014 at 3:34am UTC
Oct 12, 2014 at 3:38am UTC
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
Oct 12, 2014 at 3:41am UTC
Just tried using the "==" ... still doesn't output anything. -.-
Oct 12, 2014 at 3:44am UTC
cout<<mystr[i]
only execute once when i is 0 --
So when i is more or less than 0 it'll not executed
Oct 12, 2014 at 3:49am UTC
Why is that?
If it is printing out mystr[i], and i = mystr.size()-1, then shouldn't it print
mystr[mystr.size()-1] ?
Oct 12, 2014 at 3:53am UTC
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--)