I am currently writing a program in c++ and not sure what i am doing wrong but t
ID: 3579321 • Letter: I
Question
I am currently writing a program in c++ and not sure what i am doing wrong but the code will not print out.
Here is the problem.
1. File Previewer
Write a program that asks the user for the name of a text file. The program should display the first 10 lines of the file on the screen. If the file has fewer than 10 lines, the entire file should be displayed along 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 10 lines of output at a time and then pause. Each time the program pauses, it should wait for the user to type a key before the next 10 lines are displayed.
.cpp file that needs changes
#include "SnowData.h"
#include <iomanip>
/*
Class default constructor
Sets default values for class private variables
*/
SnowData::SnowData()
{
snow_date = "";
base_depth = 0;
}
/*
OverLoaded constructor
Parameters used to populate class private variables via set functions
*/
SnowData::SnowData(string _date, double _inches)
{
setSnowDate(_date);
base_depth = 0;
setBase_depth(_inches);
}
/*
print functions
prints out class private variables
*/
void SnowData::print()
{
for (int i = 0; i < 7; i++)
{
cout << setw(10) << left << snow_date[i] << setw(20) << left << base_depth;
}
/*cout << setw(15) << left << snow_date
<< setw(5) << fixed << showpoint << setprecision(2) << right
<< base_depth << endl;*/
}
/*
accessor function for snow_date
*/
string SnowData::getSnow_date()
{
return snow_date;
}
/*
accessor function for base_depth
*/
double SnowData::getBase_depth()
{
return base_depth;
}
/*
mutator functions for base_depth.
ensures that base_depth is not set to a negative value
*/
void SnowData::setBase_depth(double _inches)
{
if (_inches >= 0)
base_depth = _inches;
}
/*
mutator function for snow_date
*/
void SnowData::setSnowDate(string _date)
{
snow_date = _date;
}
************************************************************************
COMPLETED (.h)header file
#pragma once
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
/*
SnowData specification file
*/
class SnowData
{
private:
string snow_date;
double base_depth;
public:
SnowData();
SnowData(string _date, double _inches);
void print();
string getSnow_date();
double getBase_depth();
void setBase_depth(double);
void setSnowDate(string);
};
*******************************************************************************************
COMPLETED .cpp tester file
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
#include "Lab6_FileReader.h"
void display_file(string fname);
int main()
{
display_file("Lab6_A.txt");
display_file("Lab6_B.txt");
system("pause");
return 0;
}
void display_file(string fname)
{
Lab6_FileReader myfile(fname);
cout << " " << fname << " : # of records in file = "
<< myfile.getNumRrecords() << " ";
myfile.displayFirst10records();
myfile.displayLast10records();
myfile.displayAllRecords();
system("pause");
}
Explanation / Answer
//written display functions, it works as mentioned
//Lab6_FileReader.h
#include<fstream>
#include<string>
#include<iostream>
using namespace std;
class Lab6_FileReader
{
string fname;
int count;
public:
Lab6_FileReader()
{
fname = "";
count = 0;
}
Lab6_FileReader(string name)
{
ifstream in;
in.open(name);
string line;
count = 0;
fname = name;
if (!in)
{
cout << "Not able to open input file " << name << endl;
}
while (!in.eof())
{
getline(in, line);
++count;
}
in.close();
//in.clear();
//in.seekg(0, ios::beg);
}
int getNumRrecords()
{
return count;
}
void displayFirst10records()
{
ifstream in;
string line;
int i = 0;
in.open(fname);
while (!in.eof())
{
getline(in, line);
cout << line << endl;
if (i >= 10)
{
break;
}
++i;
}
system("pause");
}
void displayLast10records()
{
string line;
int i = 0;
ifstream in;
in.open(fname);
for (int i = 0; i < count - 10; i++)
{
getline(in, line);
}
while (!in.eof())
{
getline(in, line);
cout << line << endl;
}
system("pause");
in.close();
}
void displayAllRecords()
{
string line;
ifstream in;
in.open(fname);
while (!in.eof())
{
getline(in, line);
cout << line << endl;
}
}
};
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.