i am getting three errors:
Error1error C2228: left of '.length' must have class/struct/union
Error2error C2228: left of '.substr' must have class/struct/union
Error3error C2228: left of '.replace' must have class/struct/union
I DON'T KNOW HOW TO FIX THEM! ANY MORE ERRORS?
this is my code:
#include %26lt;iostream%26gt;
#include %26lt;string%26gt;
#include %26lt;iomanip%26gt;
using namespace std;
int main()
{
//declare variables
int code = 0;
string currentChar = "";
int numChar = 0;
int subscript = 0;
//get code number
cout %26lt;%26lt; "Enter a code: " %26lt;%26lt; endl;
cin %26gt;%26gt; code;
//determine number of characters
numChar = static_cast%26lt;int%26gt;(code.length());
//replace all numbers with the character x
while (subscript %26lt; numChar)
{
currentChar = code.substr(substr, 1);
if (currentChar == "0 to 9")
{
code.replace(subscript, 1, "x");
numChar = numChar - 1;
}
else
subscript = subscript + 1;
//end if
} //end while
My code encrypted C++ small program is not working! help please!! i am just learning C++...?
The compile errors are because 'code' is an int, and you're trying to call its length, substring, and replace operations. As an int, of course, it doesn't have these. It looks like you want 'code' to be a string.
Some other comments:
if numChar is size_t, you don't need any cast:
numChar = static_cast%26lt;int%26gt;(code.length()...
Syntax of this: if (currentChar == "0 to 9")
is ok, since currentChar is a (poorly named) string, but I don't think it's doing what you want.
Not sure what you're trying to do with:
numChar = numChar - 1;
So, make 'code' a string, and you may be on your way to getting something working. Even with that, though, I think you're making this problem much harder than it needs to be. This will do what you want:
string s1;
cout %26lt;%26lt; "Enter string: ";
getline(cin,s1);
for (string::iterator i = s1.begin(); i != s1.end(); i++) {
if (isdigit(*i)) *i = 'x';
}
cout %26lt;%26lt; s1 %26lt;%26lt; endl;
#include %26lt;cctype%26gt; for isdigit( ).
You say you're encoding by substituting x for digits, how do you decode?
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment