Re: How can i convert
Hello,
In this program, I open a text file with a username and password on two lines which are in ASCII value and turn them back into characters so they become words again. In my case, my numbers are (98 108 117 101 122 111 114) and I want to turn it back into the word (bluezor) (and leave out the white-spaces along the way?). I'm having trouble changing it back to letters.
logOnCharacter.c
void logOnCharacter()
{
std::string username = "";
std::string password = "";
//initialises the arrays to store username and password and sets every slot NULL
char usernameArray_char[100];
char passwordArray_char[100];
int usernameArray_int[100];
int passwordArray_int[100];
for (int i = 0; i < 100; i++)
{
usernameArray_char[i] = ' ';
passwordArray_char[i] = ' ';
usernameArray_int[i] = 0;
passwordArray_int[i] = 0;
}
ClearScreen();
std::ifstream charFile ("gameData.txt");
if (charFile.is_open())
{
getline(charFile, username); //gets first line and enters it as username
getline(charFile, password); //gets second line and enters it as password
for (int i = 0; i < username.length(); i++) //** I think I need help here **
{
usernameArray_char[i] = username[i];
std::cout << usernameArray_char[i];
}
charFile.close();
}
else
{
std::cout << "Unable to locate or open gameData.txt." << std::endl;
std::cout << "Please create a new game file by running the program again" << std::endl;
}
}
This outputs the ASCII values (98 108 117 101 122 111 114) correctly, so this is where it starts to get wrong I believe?
for (int i = 0; i < username.length(); i++) //** I think I need help here **
{
usernameArray_char[i] = username[i];
std::cout << char(usernameArray_char[i]);
}
When I try to do this to change the value into a character it still stays as an ASCII value so I don't know why.
|