HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppModerate

ASCII value converter

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
asciiconvertervalue

Problem

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 

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 > 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 < size; i++){
            //Return Ascii values of each character.
            cout << "The value of character " << chars[i] << " is " << (int)chars[i] << endl;
    }

    return 0;       
}

Solution

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 >> 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 (c)
            << std::endl;
}


This avoid getting the size altogether.

Code Snippets

int main(int argc, char *argv[]){
    //Declare Variables.
    string input;
    int size;
int main() { /* space or newline between paren and curly is usual */
cin >> input;
std::string input;
std::getline(std::cin, input);
//Copy the string into a character array.
const char * chars = input.c_str();

Context

StackExchange Code Review Q#103866, answer score: 12

Revisions (0)

No revisions yet.