KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I am learning C++ for the first time. I have no previous programming background. In the book I have I saw this example. #include <iostream> using::cout; using::endl; int main() { int x = 5; char y = char(x); cout << x << endl; cout << y << endl; return 0; } The example makes sense: print an integer and the ASCII representation of it. Now, I created a text file with these values. 48 49 50 51 55 56 75 I am writing a program to read this text file -- "theFile.txt" -- and want to convert these numbers to the ASCII value. Here is the code I wrote. #include <iostream> #include <fstream> using std::cout; using std::endl; using std::ifstream; int main() { ifstream thestream; thestream.open("theFile.txt"); char thecharacter; while (thestream.get(thecharacter)) { int theinteger = int(thecharacter); char thechar = char(theinteger); cout << theinteger << "\t" << thechar << endl; } system ("PAUSE"); return 0; } This is my understanding about the second program shown. The compiler does not know the exact data type that is contained in "theFile.txt". As a result, I need to specify it so I choose to read the data as a char. I read the each digit in the file as a char and converted it to an integer value and stored it in "theinteger". Since I have an integer in "theinteger" I want to print it out as a character but char thechar = char(theinteger); does not work as intended. What am I doing incorrect?
Tags (comma-separated)
Save Edits
Cancel