Please use C++ programming! This is my code that I made but there have some erro
ID: 3824266 • Letter: P
Question
Please use C++ programming! This is my code that I made but there have some errors! please change whatever you want and make improvement! Thank you for helping~:)
<File Tools >
1. File "HEAD" program:
Write a program that asks the user for the name of a file. The program shold display the first 10 lines of the file on the screen (the "head" of the file). If the file has fewer than 10 lines, the entire file should be displayed, with a message indicating the entire file has been displayed.
2. File Display Program.
Write a program that asks the user for the name of a file. The program should display the contents of the file on the screen. If the file's contents won't fit on a single screen, the program should display 24 lines of output at a time, and then pause. Each time the program pauses it should wait for the user to strike a key before the next 24 lines are displayed.
3. Data Conversion Program
Write a program that reads data from a file, performs a units conversion, and writes the data to a new file. Offer the user the following choices of conversions: degrees F to degrees C, degrees C to degrees F, feet to meters, meters to feet, ounces to grams, grams to ounces. In the new file, the first line of the file should indicate the original units and the altered units, and the next lines should have both the input and the converted data.
----------------------------------------------------------------------------------------------------------------
//1. HEAD Program
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main (int argc, char *argv[])
{
string filename;
ifstream in_stream;
//Input the name of the input file from user
cout<<"/nEnter the name of your input file:";
cin>>filename;
//open the file
in_stream.open("filename");
string data;
//count the no of lines read and printed
int count=0;
//loop till end of the file
while(!in_stream.eof())
{
//read the file data
getline(in_stream,data);
count++;
//if no of lines printed are less than 10 then print
if(count <=10)
cout<<data<<endl;
count ++;
}
if(count <= 10)
cout<<"Entire file has been displayed" <<endl;
//close the stream
in_stream.close();
}
------------------------------------------------------------------------------------
//2.Display Program
#include <iostream>
#include <fstream>
#include <cstdio>
using namespace std;
int main (int argc, char *argv[])
{
ifstream in_stream;
string Filename;
cout << "Enter the name of the file: ";
cin >> Filename;
//open the file
in_stream.open(Filename.c_str());
string data;
//count the no of lines read and printed
int count=0;
//loop till end of the file
while(!in_stream.eof())
{
//read the file data
count++;
getline(in_stream,data);
//if no of lines printed are more than 24 then ask for a key
if(count > 24)
{
cout << "Press any key to continue displaying the rest of the lines: ";
fflush(stdin);
getchar();
count=0;
cout << data << endl;
}
else
{
cout << data << endl;
}
}
//close the stream
in_stream.close();
}
------------------------------------------------------------------------------------
//3 Conversion Program
#include <iostream>
#include <fstream>
#include <conio.h>// please change this header to C++ code
using namespace std;
int main (int argc, char *argv[])
{
ifstream in_stream;
ofstream out_stream;
//open the file
in_stream.open("input.txt");
//open output file
out_stream.open("output.txt");
string data;
int ch;
//This array will store the data from the file.
double conv[] = {0.0,0.0,0.0}; //0 for first line, 1 for second,2 for third
//loop till end of the file and get the data in array conv[]
for(int i = 0; i < 3; i++)
{
if(!in_stream.eof())
{
getline(in_stream,data); //Get 1 line from the file and place into data
conv[i] = atod(data.c_str()); //set the conv variable to the double version of the line
}
}
//Ask the user to enter their choice
cout <<"Enter your choice for data conversion ";
cout <<" Enter 1 for degrees F to degrees C";
cout <<" Enter 2 for degrees C to degrees F";
cout <<" Enter 3 for feet to meters";
cout <<" Enter 4 for meters to feet";
cout <<" Enter 5 for ounces to grams";
cout <<" Enter 6 for grams to ounces ";
cin >> ch;
double out;
//use switch case ,when selected case loop through whole cov[] array and convert the data as oer requirement
//and then print it to ouput file.
switch(ch)
{
case 1:
out_stream << "degrees F degrees C"<<" ";
for (int j=0;j<3;j++)
{
out=(conv[j]-32)/1.8;
out_stream <<conv[j]<<" "<<out<<" ";
}
break;
case 2: out_stream << "degrees C degrees F"<<" ";
for (int j=0;j<3;j++)
{
out=(conv[j]*1.8)+32;
out_stream <<conv[j]<<" "<<out<<" ";
}
break;
case 3: out_stream << "feet meters"<<" ";
for (int j=0;j<3;j++)
{
out=(conv[j]/3.28);
out_stream <<conv[j]<<" "<<out<<" ";
}
break;
case 4: out_stream << "meters feet"<<" ";
for (int j=0;j<3;j++)
{
out=(conv[j]*3.28);
out_stream <<conv[j]<<" "<<out<<" ";
}
break;
case 5: out_stream << "ounces grams"<<" ";
for (int j=0;j<3;j++)
{
out=(conv[j]/0.035);
out_stream <<conv[j]<<" "<<out<<" ";
}
break;
case 6: out_stream << "grams ounces"<<" ";
for (int j=0;j<3;j++)
{
out=(conv[j]*0.035);
out_stream <<conv[j]<<" "<<out<<" ";
}
break;
}
//close the streams
in_stream.close();
out_stream.close();
}
Explanation / Answer
head program
#include <list>
#include <string>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <iterator>
struct Line
{
std::string theLine;
operator std::string const& () const { return theLine; }
friend std::istream& operator>>(std::istream& stream, Line& l)
{
return std::getline(stream, l.theLine);
}
};
class Buffer
{
public:
Buffer(size_t lc)
: lineCount(lc)
{}
void push_back(std::string const& line)
{
buffer.insert(buffer.end(),line);
if (buffer.size() > lineCount)
{
buffer.erase(buffer.begin());
}
}
typedef std::list<std::string> Cont;
typedef Cont::const_iterator const_iterator;
typedef Cont::const_reference const_reference;
const_iterator begin() const { return buffer.begin(); }
const_iterator end() const { return buffer.end();}
private:
size_t lineCount;
std::list<std::string> buffer;
};
int main()
{
std::ifstream file("Plop");
Buffer buffer(10);
Copy the file into the special buffer.
std::copy(std::istream_iterator<Line>(file), std::istream_iterator<Line>(),
std::back_inserter(buffer));
Copy the buffer (which only has the last 10 lines)
std::cout
std::copy(buffer.begin(), buffer.end(),
std::ostream_iterator<std::string>(std::cout, " "));
file display program
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
cout << "Please enter the name of the file: ";
string fileName;
getline(cin, fileName);
ifstream file(fileName.c_str(), ios::in);
string line;
for (int count = 1; !file.eof(); ++count)
{
getline(file, line);
cout << line << endl;
if (count % 24 == 0) system("Pause");
}
system("Pause");
}
data conversion
3 Conversion Program
#include <iostream>
#include <fstream>
#include <conio.h>// please change this header to C++ code
using namespace std;
int main (int argc, char *argv[])
{
ifstream in_stream;
ofstream out_stream;
//open the file
in_stream.open("input.txt");
//open output file
out_stream.open("output.txt");
string data;
int ch;
//This array will store the data from the file.
double conv[] = {0.0,0.0,0.0}; //0 for first line, 1 for second,2 for third
//loop till end of the file and get the data in array conv[]
for(int i = 0; i < 3; i++)
{
if(!in_stream.eof())
{
getline(in_stream,data); //Get 1 line from the file and place into data
conv[i] = atod(data.c_str()); //set the conv variable to the double version of the line
}
}
//Ask the user to enter their choice
cout <<"Enter your choice for data conversion ";
cout <<" Enter 1 for degrees F to degrees C";
cout <<" Enter 2 for degrees C to degrees F";
cout <<" Enter 3 for feet to meters";
cout <<" Enter 4 for meters to feet";
cout <<" Enter 5 for ounces to grams";
cout <<" Enter 6 for grams to ounces ";
cin >> ch;
double out;
//use switch case ,when selected case loop through whole cov[] array and convert the data as oer requirement
//and then print it to ouput file.
switch(ch)
{
case 1:
out_stream << "degrees F degrees C"<<" ";
for (int j=0;j<3;j++)
{
out=(conv[j]-32)/1.8;
out_stream <<conv[j]<<" "<<out<<" ";
}
break;
case 2: out_stream << "degrees C degrees F"<<" ";
for (int j=0;j<3;j++)
{
out=(conv[j]*1.8)+32;
out_stream <<conv[j]<<" "<<out<<" ";
}
break;
case 3: out_stream << "feet meters"<<" ";
for (int j=0;j<3;j++)
{
out=(conv[j]/3.28);
out_stream <<conv[j]<<" "<<out<<" ";
}
break;
case 4: out_stream << "meters feet"<<" ";
for (int j=0;j<3;j++)
{
out=(conv[j]*3.28);
out_stream <<conv[j]<<" "<<out<<" ";
}
break;
case 5: out_stream << "ounces grams"<<" ";
for (int j=0;j<3;j++)
{
out=(conv[j]/0.035);
out_stream <<conv[j]<<" "<<out<<" ";
}
break;
case 6: out_stream << "grams ounces"<<" ";
for (int j=0;j<3;j++)
{
out=(conv[j]*0.035);
out_stream <<conv[j]<<" "<<out<<" ";
}
break;
}
//close the streams
in_stream.close();
out_stream.close();
}