ASCII value converter I have written VERY simple code that converts a string to a character array and then displays the ASCII value of each character. Let me know if this is the most effective/safe way of doing what I described above.

#include &lt;iostream&gt;


using namespace std;


int main(int argc, char *argv[]){
    //Declare Variables.
    string input;
    int size;

    //Display message to user asking them to enter a string or character.
    cout &lt;&lt; "What character/string do you need the ascii value(s) of? " &lt;&lt; endl;

    //Assign user input to string input.
    cin &gt;&gt; input;   

    //Get string length of user input.
    size = input.length();

    //Copy the string into a character array.
    const char * chars = input.c_str();

    //Itterate through the character array using the string size.
    for(int i = 0; i &lt; size; i++){
            //Return Ascii values of each character.
            cout &lt;&lt; "The value of character " &lt;&lt; chars[i] &lt;&lt; " is " &lt;&lt; (int)chars[i] &lt;&lt; endl;
    }

    return 0;       
}
int main(int argc, char *argv[]){
    //Declare Variables.
    string input;
    int size;


Don't declare variables until you actually need them, and don't declare variables you don't need. In this case, you're not using the arguments passed into main, either don't name them, or use the version of main that takes no arguments:

int main() { /* space or newline between paren and curly is usual */




cin &gt;&gt; input;


I'm not sure this is what you want. If the user enters a "sentence" with spaces in it, you'll only get what they typed up to the first space (not included). If you want the whole line, use std::getline.

std::string input;
std::getline(std::cin, input);




//Copy the string into a character array.
const char * chars = input.c_str();


This comment is dangerously wrong. You don't get a copy at all, you get a pointer to std::string's internals. The string data is not copied, and the pointer returned will become invalid as soon as input's lifetime ends.

std::string's length() member returns an unsigned quantity. While there's no problem in this case assigning it to an int, the correct type to use is:

std::string::size_type size = input.length();


or more conveniently:

auto size = input.length();


(And you'll need to use an unsigned quantity for the loop counter too, to avoid warnings.)



A different way of doing your loop would be to use the range-based variant. It works for std::string.

for (auto c: input) {
  std::cout &lt;&lt; "Char \"" &lt;&lt; c &lt;&lt; "\" has value " &lt;&lt; static_cast&lt;int&gt;(c)
            &lt;&lt; std::endl;
}


This avoid getting the size altogether.