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?
cin >> plainTextreads from the input up to, but excluding, the first whitespace character. You probably wantstd::getline(cin, plainText)instead.References: