I'm currently working on a simple encryption program. The basic principles work and I've already implemented one method of encryption, and I want to add another.

The only problem is, for this, I need to know how to convert a string (inserted by the user of the program) into individual characters, which need to be converted to their respective ASCII characters.

I have no clue how to go about it... I've been thinking about maybe an array for the results, but I have no idea how arrays work yet, and on top of that, I don't know how to get a specific character in a character string.

So my question: How can I substract individual characters from a string, and put these into 'a' variable, for later use?

01-18-2007

7stud

Quote:

individual characters, which need to be converted to their respective ASCII characters.

If you assign a char type to an int variable, the char's ascii code will be stored in the int variable.

but I have no idea how arrays work yet, and on top of that, I don't know how to get a specific character in a character string.

Some people believe you should forget about arrays when first starting with C++ and instead learn about vectors. Yes, I know, "vector" is a scary sounding word, and the syntax can be intimidating, but they are much easier to use than arrays. Here is a short lesson:

1) Declaring a vector:

Code:

#include<iostream>
#include <vector>
using namespace std;

int main()
{
vector<int> myVector;

return 0;

}

You declare the type of the things you plan on storing in the vector between the angled brackets. Then, you follow that with a variable name. Don't forget to #include <vector>.

2) Adding an element to a vector:

Code:

myVector.push_back(3);

That says to add the integer 3 to the end of the vector. A vector will automatically grow in size as you add more elements. That is why vectors are easier to use than arrays. Arrays have a fixed size.

3) Displaying the contents of a vector:

Code:

for(int i=0; i < myVector.size(); ++i)
{
cout<<myVector[i];
}

You can get the number of elements in a vector at any time by calling its size() function. To access the elements of a vector, you use brackets with the index position of the element. The elements of a vector start at index position 0. If you write: