in C 1a Write a program that reads in file dataintxt contain
in (C++)
1a) Write a program that reads in file \"datain.txt\" containing numbers, and creates an output file containing the square of those numbers. For example, if the input file contains 6, 4, and 5, the output file will contain 36, 16, and 25.
b) Modify the program and data file to use sentinal signal. When it reads in a 0 from the file, it stops.
c) Modify the program and data file to use the specified number of records approach. The first line of the file should contain how many data points are in the file.
*****I understand for the most part how to do part 1A but i am having trouble with the last two pieces. Making the program as simple as possible for me to read and including comment lines would be greatly appreciates. Thank you!!!
Solution
Here is the code for part a:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream fin;
ofstream fout;
int num;
fin.open(\"dataIn.txt\");
fout.open(\"SquareDataOut.txt\");
while(!fin.eof())
{
fin>>num;
fout<<num * num <<\" \";
}
fout<<endl;
}
And the code for part b is:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream fin;
ofstream fout;
int num;
fin.open(\"dataIn.txt\");
fout.open(\"SquareDataOut.txt\");
while(true)
{
fin>>num;
if(num == 0)
break;
fout<<num * num <<\" \";
}
fout<<endl;
}
And finally, the code for part c is:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream fin;
ofstream fout;
int num;
fin.open(\"dataIn.txt\");
fout.open(\"SquareDataOut.txt\");
cout<<\"Enter the number of values to read: \";
cin>>count;
for(int i = 1; i <= count; i++)
{
fin>>num;
fout<<num * num <<\" \";
}
fout<<endl;
}

