input/ output files

im having problems with my input files. It will output, but my file wont accept an input.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <iomanip>
using namespace std;
#include <string>
#include <fstream>
/**************************************************************************

 *************************************************************************/

int main ()
{
	ifstream inFile;
	ofstream outFile;
	int a;
	inFile.open("input.txt");
	outFile.open("output.txt");


	inFile >> a;
	outFile << "what is your age? ";

	outFile << endl << "your age is: " << a;
}



the output is

what is your age? 
your age is: 0
Input files read data from a file, and output files write data to a file.
So, unless you open a file containing an integer value, inFile >> a; doesn't read anything.

EDIT: spelling
Last edited on
oh, how would you input the value for age; would you just use a cin? i was trying to avoid cins and couts though.
You could create a file called input.txt with a number in it. You could do this with a standard text editor.
Last edited on
If you want to get user input from the console then yes, you have to use cin >>.
Once you've opened the file (ie. you have said ifstream inFile;) you can just use in >> a;.
Topic archived. No new replies allowed.