Erasing a string array
Feb 4, 2016 at 9:16pm UTC
I'm reversing C-string inputs, which I got working fine. My problem is when I try to apply the code to a while loop to enter new inputs. The new inputs keep adding to the old ones.
How do I get the string array to clear all values?
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
#include<iostream>
#include<string.h>
using namespace std;
string inputString, outputString, again;
int main(){
cout << "Enter a phrase you want reversed: " ;
cin >> inputString;
cout << endl;
int nLength = inputString.length();
nLength--;
while (1){
for (int nIndex = nLength; nIndex >= 0; nIndex--);
{
outputString+=inputString[nIndex];
}
cout << "The reversed phrase is: " << outputString << endl;
cout << "Again? (y/n)" ;
cin >> again;
if (again == "y" ){
cout << "Enter a phrase you want reversed: " ;
cin >> inputString;
}
if (again !="y" ){
cout << "Please close the window." << endl;
break ;
}
}
return 0;
}
I've attempted
1 2 3 4
if (again == "y" ){
memset(outputString, 0, nIndex)
cout << "Enter a phrase you want reversed: " ;
cin >> inputString;}
But that has the error:
[Error] name lookup of 'nIndex' changed for ISO 'for' scoping [-fpermissive]
Feb 4, 2016 at 9:19pm UTC
1 2 3 4 5
if (again == "y" ){
outputString.clear();
cout << "Enter a phrase you want reversed: " ;
cin >> inputString;
}
Feb 4, 2016 at 9:30pm UTC
Thanks but the error at line 17 is still there.
[Error] name lookup of 'nIndex' changed for ISO 'for' scoping [-fpermissive]
Feb 4, 2016 at 9:32pm UTC
You have a semi-colon that shouldn't be there at the end of line 17 15.
Last edited on Feb 4, 2016 at 9:33pm UTC
Feb 4, 2016 at 9:33pm UTC
You have a semicolon at line 15, remove it.
for (int nIndex = nLength; nIndex >= 0; nIndex--); // this one.
Feb 4, 2016 at 9:38pm UTC
Thanks!!
Topic archived. No new replies allowed.