Infile Outfile C++ Visual Studio

Mar 7, 2017 at 7:18am
Please help in terms of Visual studio:
Need to read an infile, square the numbers within the infile, and outfile those numbers to a new document. I AM LOST!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;

int main()
{
	char fileName;
	double number = 0;
	
	cout << "Enter an input file name:  ";
	cin >> fileName;


	ifstream inFile;
	ofstream outFile;

inFile.open("C:\\Users\\Josh\\Desktop\\numbers\\a.txt");
		outFile.open("C:\\Users\\Josh\\Desktop\\numbers\\squareroots.txt");

		inFile >> number;

		while (inFile)
		{
			sqrt(number);
			inFile >> number;
			outFile << number;
		}
		inFile.close();
		outFile.close();
return 0;
Last edited on Mar 7, 2017 at 7:19am
Mar 7, 2017 at 8:19am
1
2
3
4
5
6
while (inFile)
{
    sqrt(number);
    inFile >> number;
    outFile << number;
}

You have to order wrong.
You need to read in the number first, do whatever you want to the number, then output it to your file.

Also, it's recommended to write your while loop condition like so
while( inFile >> number )
Topic archived. No new replies allowed.