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.
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?
1 2 3 4 5
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.
I use Code::Blocks and ClearScreen() is in a header file made by Duoas (see: Articles - How to Clear Screen). For me when I do the second code all I get is a bunch of random numbers together as opposed to the letters I am expecting.
#include <fstream>
usingnamespace std;
int main()
{
string text = "hello";
char array[5];
for ( int i = 0; i < 5; i++ )array[i] = text[i];
for ( int i = 0; i < 5; i++ )cout << array[i];
return 0;
}
Please try it.
If this code doesn't work then it is possible you should use change the compiler to mingw 4.4!
The problem is, i'm not trying to change from letters to ASCII value or letters to a character array. I'm changing each individual ASCII value on a line (each value / character is separated by one white-space) to its letter. As said, i'm trying to change (98 108 117 101 122 111 114) back to (bluezor) but (?)it's not working like I would think it would work.
That doesn't work, all that does is put the integers into the string which I already have. Instead of the integers, I'm trying to get the ASCII value for the integers.
Sorry if i'm not clear :x I suck at explaining stuff or asking questions
Is usernameLine a string formatted like this: "98 108 117 101 122 111 114" ?
If so, you need to convert it to a sequence of numbers ( "98" is a sequence of the characters '9' and '8' )
eg:
1 2 3 4 5 6
string line = "98 108 117 101 122 111 114";
string result;
stringstream ss ( line );
int temp;
while ( ss >> temp ) // read next integer
result += temp; // add it as char in the string