I'm doing a homework assignment that requires me to write a program that pulls from text document A and put that info and more into text document B.
I have to include a letter in the A doc. That letter needs to be displayed as it's ASCII code in doc B. I've read the tutorial here on cplusplus.com, but can't seem to figure it out. Any help would be appreciated.
#include <iostream>
#include <fstream>
usingnamespace std;
int main()
{
ifstream infile;
ofstream outfile;
infile.open ("l5435.txt");
outfile.open("l5435part2.txt");
char l;
outfile << "The ASCII value of your character: l = " << (l) = static_cast<int>(l) << endl;
outfile << "The sum of 53 and 35 = " << 53 + 35 << endl;
outfile << "The product of 53 and 35 = " << 53 * 35 << endl;
outfile.close();
infile.close();
return 0;
}
i'm pretty sure that the file I/O part is right, but not sure. i started it out by learning how to open a file and save over it, but not opening a file and then pulling info out and putting it into a different file. then i tried moving on to opening one and saving a second while also adding in the casting part.
ok....i've got a second question now.....but first, i switched out "(l) = static_cast<int>(l)" with "int l" and came up with an error saying that "type 'int' was not expected" so does that mean i need to declare int? if so, what do i make it? the ASCII value? or do i enter something like this---> l=ASCII code after i declare char l?
so i have made another couple changes and now it works........kinda. it doesn't save anything to the output file and i need it to do that. here's what i have now. also, please review my second post (the first with code from me).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
#include <fstream>
usingnamespace std;
int main()
{
ifstream infile;
ofstream outfile;
infile.open ("l5435.txt");
outfile.open("l5435part2.txt");
char l;
l=0x6C;
outfile << "The ASCII value of your character: l = " << int (l) << endl;
outfile << "The sum of 53 and 35 = " << 53 + 35 << endl;
outfile << "The product of 53 and 35 = " << 53 * 35 << endl;
outfile.close();
infile.close();
return 0;
}
also of note--i have tried placing "static_cast<int> (l)" into the statement in place of just "int (l)" and that also produces the same results.
#include <iostream>
#include <fstream>
usingnamespace std;
int main()
{
ifstream infile;
ofstream outfile;
infile.open ("l5435.txt");
outfile.open("l5435part2.txt");
char l;
outfile << "The ASCII value of your character: l = " << (static_cast<int> (l)) << endl;
outfile << "The sum of 53 and 35 = " << 53 + 35 << endl;
outfile << "The product of 53 and 35 = " << 53 * 35 << endl;
outfile.close();
infile.close();
return 0;
}
it comes up during my debug as saying "l is used without being initialized" i need it to be able to output the value of the char variable (any variable, not just "l") as an ASCII value. any ideas?
haplo, I am just a noob also, but I tried your code. I put the letter A into file l5435.txt. I compiled with c++ haplo.c -o halpo. This is the output:
1 2 3 4 5 6 7 8 9
PlayGround $ cat l5435.txt
A
PlayGround $ c++ halpo.c -o halpo
PlayGround $ ./halpo
PlayGround $ cat l5435part2.txt
The ASCII value of your character: l = -73
The sum of 53 and 35 = 88
The product of 53 and 35 = 1855
PlayGround $
I use linux, gcc 4.1.2 and everything seemed to work ok. (except maybe the ASCII value of l (letter A ))
EDIT: Looks like it is not reading in the file l5435.txt.
Ok, changed it to read 1 character from input file l5345.txt. Here is the code and outputfile.
PlayGround $ cat halpo.c
#include <iostream>
#include <fstream>
usingnamespace std;
int main()
{
ifstream infile;
ofstream outfile;
infile.open ("l5435.txt", ios::in); //open for reading
outfile.open("l5435part2.txt", ios::out); //open for writing
char l;
infile >> l; //read first character
outfile << "The character is :"<< l << endl; //extra write to see what character l is
outfile << "The ASCII value of your character: l = " << (static_cast<int> (l)) << endl;
outfile << "The sum of 53 and 35 = " << 53 + 35 << endl;
outfile << "The product of 53 and 35 = " << 53 * 35 << endl;
outfile.close();
infile.close();
return 0;
}
PlayGround $ cat l5435part2.txt
The character is :A
The ASCII value of your character: l = 65
The sum of 53 and 35 = 88
The product of 53 and 35 = 1855
PlayGround $