Here is something I put together. I used a vector to store all of the ASCII values that are to be generated. We first ask the user for a string. Then we use type casting and add the values to a vector. We also use a while loop to prevent the user from not entering nothing.
# include <iostream>
# include <string>
# include <vector>
std::vector<int> converttoASCII (std::string s) //used a vector to store all our ASCII values
{
std::vector <int> vals; //vectpr creation
int ascChar;
for (int i = 0; i < s.length(); i++) //We interate through string passed and add to vectors
{
ascChar = s[i];
vals.push_back(ascChar);
}
return vals;
}
int main()
{
std::string toencode;
std::cout << "Please enter in a string to encode: ";
std::getline(std::cin, toencode);
while (toencode.length() == 0) //we used a for loop to prevent user from entering nothing.
{
std::cin.clear();
std::cout << "Must not be empty! Try Again.\n";
std::cout << "Please enter in a string to encode: ";
std::getline(std::cin, toencode);
}
std::vector <int> asciivals = converttoASCII(toencode);
for (int i : asciivals) //Print out the results of the vector
{
std::cout << i << "\n";
}
return 0;
}
References:
enter link description here
enter link description here
enter link description here