home / study / engineering / computer science / questions and answers / I Need H
ID: 3858420 • Letter: H
Question
home / study / engineering / computer science / questions and answers / I Need Help On String/ Sequential Data, C++ ... Question: I need help on string/ sequential data, c++ includ... I need help on string/ sequential data, c++
////////////////////
////////////these are prototypes given //////what he wants........ Your job is to create and fill the functions with the necessary logic. Another file named cars.txt is provided for you to test with. The format of the file is as follows: [year] [make] [model] [price] [mileage] [category] . ///////and his cars he's owned in the past that need to be able to be in a file are
... Category| Number| Total Price| Total Mileage| New| 1| 33000| 250| Used| 2| 7000| 254000| /////any help much appriciated this is the into programing class but its like spanish to me, only code class i have to take i dont know how yaull do it,
id rather sit and do linear algebra all day..../////////any help MUCH APPRICIEATED!!!!
/////////////sorry here is a screen shot of the template!!!!
#include <iostream>
#include <ostream>
#include <istream>
#include <ostream>
#include <fstream>
#include <sstream>
#include <string>
#include <iomanip>
bool GetInputFileStream(std::ifstream * fin, std::string filename);
void GetOutputFileStream(std::ofstream * fout, std::string filename);
void SetGetPointer(std::istream & fin, int location);
void SetPutPointer(std::ostream & fout, int location);
void AnalyzeFile(std::istream & fin,
int & numUsed,
int & numNew,
double & newTotalPrice,
double & newTotalMileage,
double & usedTotalPrice,
double & usedTotalMileage);
void PrintLine(std::ostream & sout, std::string s);
void PrintNew(std::istream & fin, std::ostream & fout);
void PrintUsed(std::istream & fin, std::ostream & fout);
void PrintStatistics(std::ostream & fout,
int numUsed,
int numNew,
double newTotalPrice,
double newTotalMileage,
double usedTotalPrice,
double usedTotalMileage);
std::string FormatCarInfo(std::string year,
std::string make,
std::string model,
double price,
double mileage);
// DO NOT REMOVE THESE COMMENTS -- these comments are actually
// commands to the assessment tool.
//@include(main)
//@addRule(commentAll)
int main()
{
// You will need to put the provided cars.txt into
// the working directory of this program
std::string filename = "cars.txt";
std::ifstream fin;
bool isOpen = GetInputFileStream(&fin, filename);
if (isOpen == false)
{
std::cout << "Couldn't find file " << filename << "!" << std::endl;
}
else
{
double newTotalPrice = 0;
double newTotalMileage = 0;
double usedTotalPrice = 0;
double usedTotalMileage = 0;
int numUsed = 0;
int numNew = 0;
AnalyzeFile(fin, numUsed, numNew, newTotalPrice, newTotalMileage,
usedTotalPrice, usedTotalMileage);
PrintStatistics(std::cout, numUsed, numNew, newTotalPrice, newTotalMileage,
usedTotalPrice, usedTotalMileage);
}
// Now work on tests to make sure the rest of your functions work properly
std::cout << "Press ENTER to continue";
std::cin.get();
return 0;
}
// DO NOT REMOVE THESE COMMENTS -- these comments are actually
// commands to the assessment tool.
//@removeRule(commentAll)
// The function prototypes are provided above
// Copy them here, remove the semicolons at the end
// Add braces, and fill in the functionality
//////////////END of TEMPLATE
////////This is what my file should look like when looking for a car
Category| Number| Total Price| Total Mileage|
New| 1| 33000| 250|
Used| 2| 7000| 254000|
//////would the sample exe help?
Introduction
This assignment will introduce you to the capabilities of streams. We are mainly concerned with file streams but there are a couple of others worth noting as well. Strangely enough, you already know everything you need to know about them. You have been using them this entire semester since the very first assignment. Do you recall reading about cin and cout streams? Behind the scenes, cin and cout take care of a lot complicated things. For example, when you send cout a string (i.e. cout << “My string!”) it’s pretty obvious what needs to happen. Cout formats your string into its ASCII representation and makes it appear in the console. What about integers and doubles, though? Under the covers, cout converts your digits to ASCII and prints them as well. In this assignment, instead of sending all of our input to a console input or output (cin and cout, respectively), we will be sending it to either a ‘string’ stream or a ‘file’ stream. The difference now is that the information you send to the stream will show up either inside of a string or a file.
////////////// and this is some of the sample functionalities used with the tests and the expexted outputs
The caller will supply a pointer to an output file stream object. You will use this object to open an output file that has the name ‘filename.’ For this assignment we will assume that output files always open successfully. Once the code below runs you should have a file with the given name and the text that you inserted into the stream. You can then open it using notepad (or whatever) and see the output as shown below on the right.
Sample Usage Sample Output
std::ofstream fout; // 'f'ile out - fout std::string filename = "newFile.txt"; GetOutputFileStream(&fout, filename); fout << "This is my new file! "; fout << "This is on a new line!"; fout.close();
In this section, pay very close attention to the output. When specified, yours should match exactly. Also, this section will include hints on how to get the appropriate functionality to work.
bool GetInputFileStream(std::ifstream * fin, std::string filename); o fin: A pointer to an input file stream object. o filename: The name of the file you want to open o Returns: true or false, depending on whether the file opened successfully or not.
The caller will supply a pointer to an input file stream object. You will use this object to open an input file that has the name ‘filename.’ If the file doesn’t exist, alert the user by returning false.
Sample Usage Sample Output
std::ifstream fin; // 'f'ile in - fin std::string filename = "fake.txt"; bool isOpen = GetInputFileStream(&fin, filename); std::cout << filename << " open: "; std::cout << std::boolalpha << isOpen << std::endl; std::cout << std::endl; filename = "real.txt"; isOpen = GetInputFileStream(&fin, filename); std::cout << filename << " open: "; std::cout << std::boolalpha << isOpen << std::endl; std::cout << std::endl; fin.close();
Opening file fake.txt fake.txt open: false///output
Opening file real.txt real.txt open: true///output
void GetOutputFileStream(std::ofstream * fout, std::string filename); o fout: A pointer to an output file stream object o filename: The name of the file that you want to open o Returns: Nothing.
void SetGetPointer(std::istream & sin, int location); o sin: An input stream object o location: A location, with respect to the BEGINNING of the stream, to which you want to move the pointer to. o Returns: nothing
When you are reading from the stream, each time you read a character the pointer is incremented by one. That way you always get the next character. What if you want to read the same file twice? Well, you need to reset the ‘get’ pointer back to 0. In this example, pretend that the file numbers.txt exists and that it has inside it the numbers 1, 2, 3, 4, and 5 in order with a space in-between. You notice that the count resets back to 1 when the get pointer is put back at the 0 location of the file.
Sample Usage Sample Output
std::ifstream fin; // 'f'ile in - fin std::string filename = "numbers.txt"; bool isOpen = GetInputFileStream(&fin, filename); std::cout << filename << " open: "; std::cout << std::boolalpha << isOpen << std::endl; int number = 0; fin >> number; std::cout << "Read: " << number << std::endl;
Opening file numbers.txt//////////output sample
numbers.txt open: true
Read: 1
Read: 2
Read: 1
Read: 2
Read: 3
Read: 4
///////and so on with each of the prtototypes
////////Did i ask question better?
Explanation / Answer
hi please find the below program,
/* C++ Sequential Input and Output Operations with Files */
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<stdlib.h>
class student
{
char name[20];
char grade;
float marks;
public:
void getdata(void);
void display(void);
};
void student::getdata(void)
{
char ch;
cincountt.get(ch);
countt<<"Enter name: ";
cin.getline(name, 20);
countt<<"Enter grade: ";
cin>>grade;
countt<<"Enter marks: ";
cin>>marks;
countt<<" ";
}
void student::display(void)
{
countt<<"Name: "<<name<<" Grade: "<<grade<<" Marks: "<<marks<<" ";
}
void main()
{
clrscr();
char fname[20];
student cse[3]; // declare array of 3 objects
fstream fio; // input and output file
countt<<"Enter file name: ";
cin.get(fname, 20);
fio.open(fname, ios::in || ios::out);
if(!fio)
{
countt<<"Error occurred in opening the file..!! ";
countt<<"Press any key to exit... ";
getch();
exit(1);
}
countt<<"Enter details for 3 students: ";
for(int i=0; i<3; i++)
{
cse[i].getdata();
fio.write((char *)&cse[i], sizeof(cse[i]));
}
fio.seekg(0); /* seekg(0) resets the file to start, to access the file
* from the beginning */
countt<<"The content of "<<fname<<" file are shown below: ";
for(i=0; i<3; i++)
{
fio.read((char *)&cse[i], sizeof(cse[i]));
cse[i].display();
}
fio.close();
countt<<" Press any key to exit... ";
getch();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.