I was testing out how input/output files worked so I made a simple program. When I ran it, a new file was created and it'd say something like "The number -9.25596e+061 was entered." The number in the input file was 777. I don't understand why it isn't getting that number.
I tried it and it ended up saying "The number 0.0 was entered. I don't think the thing I posted in the beginning was a good example, I'm trying to figure out how to use data from a text file and output it into a new file. Something like this:
#include <iostream>
#include <fstream>
using namespace std;
void main()
{
double average;
int midterm1, midterm2, midterm3;
ifstream input;
ofstream output;
input>>midterm1>>midterm2>>midterm3;
average=(midterm1+midterm2+midterm3)/3;
output<<"The average is "<<average;
input.close();
output.close();
}
When I run this I end up getting "The average is 5.72662e+008" as the output when the values in the input text file is "80 90 100." Sorry if I didn't explain it too well.
The problem you're having is that you have a streamed type in your string that may not be directly streamable to a double. You would either have to change your file to a binary file or you should consider using a stringstream.
Sorry, I'm having a hard time understanding the problem. Can you show my an example on how it works? My professor didn't really go over too much on this topic, only the basics. He said the thing I did should work fine, but it's not.
Edit: Also is the text file supposed to be typed out in a certain way? Right now I have "80 90 100" typed out exactly like that and the file in right in the root of my C drive.
There are two types of files: text files and binary files.
Text files contain strings.
Binary files contain actual data.
Streaming something from a text file to a double is doomed to fail, because you are trying to explicitly convert a string to a double, which is not defined.
Instead, try using either of the following:
A binary file instead of a text file.
Implementing a stringstream for the conversion from a string to a double.
(Opening a file as binary just requires you to add a flag to the .open() function - check the manual.)
There is no physical difference between a text and a binary file. If you try to open a "binary" file with a text editor, you can do it (seeing weird characters is irrelevant).
The difference is how the data is treated.
Streaming something from a text file to a double is doomed to fail, because you are trying to explicitly convert a string to a double, which is not defined.
There is no difference between the console or a file (they are both streams). So if you can do cin>>n; it should work with a file.
irrelevant, he/it/she is trying to read integers