Need Some Help
I am in a C++ class and one of the assignments is going to be below. I have read through the chapter and gone over the lecture and I am still having problems. If someone could explain this to me it would be greatly appreciated. Ok here is the assignment.
Create a text file with a letter on the first line, and two double-digit numbers on the second line. Write a program using fstream that reads in your text file, creates variables to manipulate that input and outputs the following results to a different text file:
"The ASCII value of your character: " __ " is " ____"
(you will need to cast from a char to an int)
"The sum of " ___ " and " _____ " = " _____ "
(add the two numbers, display both the numbers and final result)
"The product of " ___ " and " _____ " = " _____"
(multiply the two numbers, display both the numbers and final result)
Remember to comment your code to explain what you are doing, and remember to close both the input and output file when you are done manipulating them.
I'm not asking for the answer. Would just like someone to explain it to me so that I can do it.
Your assignment already is a step-by-step explanation of the problem. 1. Use fstream to read in your text file (presumably into variables you've made); 2. Make more variables to be the sum, product, and ASCII value; 3. Write out the three sentences given to you in the problem, using the values of the variables to fill in the blanks; 4. Close both the input and output files.
And which part(s) of the problem have you already done, and which have you got a problem with? If the answer is "none" and "all", then you probably need to go over your lecture notes or read the chapter(s) in the book you should know by now.
I'm going to try not to do your homework for you and at the same time help you out.
do some homework on fstream/iostream/ and common string functions like atof()
all file input/output will be in TEXT format. not Binary
hope this helps. but as they said eariler the porblem is perty much self explanitory(forgive my spelling.)
float number_1; // i'm using float to make you do some research.
//refer to other parts of this site for error checking.
//first lets get some room to work.... by that i mean storage space.
p = new char;
//error checking again
memset(p,0,100); //clear the work space;
inputfile.read( p ); //<< more homework here.
//now your buffer p should contain your entire text file.
//TIP 1: the lines in the file will be sepraerated by '\n' and not \0
//now lets read the first line.
magical_function_you_need_to_write_to_find_where_newline_is( p );
//Image we have another file called outputfile
//please note the above function.
//anyway enought TIPS 2:
//Now we just got the numbers.
number_1 = atof(p);
number_2 = atof(p);
number_3 = atof(p);
if( (number_1 + number_2) != number_3)
exit(1)//10+20 != 30 then something is wrong. but i am using float's. hehehe
std::cout "The sum of \"" << number_1 << "\" and \"" << number_2 " = \"" << number_3;
basicly the excersize is to get you familer with file input/output and get you using the std libary. after you learn how to get JUST what you want out of a file you can start learning the printf and _snprintf() functions.
What horrible mixture of C++ and C did you learn and are spreading around? And why bother with filestreams? that's nowhere near the theoretical foundation OP would need to fill in the blanks for his program here ...