not getting space? reversing a string
This is what I have, but I have no idea why it doesn't work if I add a space to the string!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
#include <iostream>
#include <string>
using namespace std;
void reverse(string text);
int main()
{
string text;
cout << "Enter a string: ";
cin >> text;
cout << "Result : " << text;
reverse(text);
return 0;
}
void reverse(string text)
{
string reverse;
char msg;
for (int i = text.length() - 1; i >= 0; i--)
{
msg = text[i];
reverse += msg;
}
cout << reverse;
}
|
cin>> stops reading at whitespace. Use getline(cin, text); instead.
Ohhh... I didn't know that, thanks!
Topic archived. No new replies allowed.