4

I was just trying something out and made the following code. It is supposed to take each individual letter in a string and print its ASCII equivalent. However, when there is a space, it stops converting. Here is the code:

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

void convertToASCII(string letter)
{
    for (int i = 0; i < letter.length(); i++)
    {
        char x = letter.at(i);
        cout << int(x) << endl;
    }
}

int main()
{
    string plainText;
    cout << "Enter text to convert to ASCII: ";
    cin >> plainText;
    convertToASCII(plainText);
    return 0;
}

Any ideas on why this happens?

6 Answers 6

12

cin >> plainText reads from the input up to, but excluding, the first whitespace character. You probably want std::getline(cin, plainText) instead.

References:

Sign up to request clarification or add additional context in comments.

Comments

3

Just Use getline and then no need such things you can just typecast to int a string letter to directly convert it to ascii.Here Is My Code.

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

void convertToASCII(string s)
{
    for (int i = 0; i < s.length(); i++)
    {
        cout << (int)s[i]<< endl;
    }
}

int main()
{
    string plainText;
    cout << "Enter text to convert to ASCII: ";
    getline(cin,plainText);
    convertToASCII(plainText);
    return 0;
}

Comments

2

The formatted input function operator>> on istream stops extraction from the stream if it hits a space. So your string doesn't contain the rest of the input.

If you wish to read until the end of the line, use getline instead:

string plainText;
cout << "Enter text to convert to ASCII: ";
getline(cin, plainText);
convertToASCII(plainText);

Comments

0

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

Comments

0

Simple and easy code

string mystring= "ABC DEF ++ -- ";
for (char c : mystring) cout << (int)c << endl;

This code will check characters one by one and output equivalent ascii value

Output: 65 66 67 32 68 69 70 32 43 43 32 45 45 32

Comments

-1
cin.ignore();
cin.getline(plaintext,100); // 100 (assumed) is the size of plaintext

Use these two lines of code to accept a string with blank spaces.

1 Comment

It's that "assumed" part that makes it a poor solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.