flaymond
April 21st, 2015, 09:46 AM
Hello, I still can't figure this out of how to get the value string length and save in a variable, in C++ like in Python.
C++ code:
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
using namespace std;
// Program that print strings backward
void revencrypt() {
string translate;
string message;
int i;
cout << "Please enter message to encrypt: ";
cin >> message;
output = "";
i = std::string str(message) -1; // This not working :(
// I want to save string length value in i, but not working. It produce error 'expected ";" in std::string<here> str(message)'
while ( i >= 0)
output = output + message[i];
i = i - 1;
cout << output << endl;
}
int main() {
revencrypt();
return 0;
}
In Python, this code working:
def encrypt():
message = raw_input ("Please enter your message to encrypt: ")
if message != 0:
output = ''
i = len(message) -1 # This code not working in C++ program above
while i >= 0:
output = output + message[i]
i = i - 1
print (output)
def main()
encrypt()
main() # code starts here
I expected the both program produce same output, Unfortunately, the data type and STL functions is confusing me in C++. I don't know which function or method will fix this out. Anyone can help? Thanks.
C++ code:
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
using namespace std;
// Program that print strings backward
void revencrypt() {
string translate;
string message;
int i;
cout << "Please enter message to encrypt: ";
cin >> message;
output = "";
i = std::string str(message) -1; // This not working :(
// I want to save string length value in i, but not working. It produce error 'expected ";" in std::string<here> str(message)'
while ( i >= 0)
output = output + message[i];
i = i - 1;
cout << output << endl;
}
int main() {
revencrypt();
return 0;
}
In Python, this code working:
def encrypt():
message = raw_input ("Please enter your message to encrypt: ")
if message != 0:
output = ''
i = len(message) -1 # This code not working in C++ program above
while i >= 0:
output = output + message[i]
i = i - 1
print (output)
def main()
encrypt()
main() # code starts here
I expected the both program produce same output, Unfortunately, the data type and STL functions is confusing me in C++. I don't know which function or method will fix this out. Anyone can help? Thanks.