Answer the question
In order to leave comments, you need to log in
How to correctly output the structure to a file?
The essence of the task is to read lines from the file that contain data and the condition is met in the line, then output it to another file.
I decided to read the line one by one, filling in the fields of the structure and, if the condition is met, display these fields.
The problem is that the program doesn't work and I can't even figure out why, because visual studio just reports that there are build errors. As a rule, after such it underlines erroneous places or displays an exception, but not in this case.
Please help me debug the program and indicate what the error is, thanks in advance.
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("E:\\inputStruct.txt");
ofstream out("E:\\outputStruct.txt");
struct List
{
char Mark[20], Num[10], ExName[20];
int Year, PastRoad;
void Show();
void Print();
};
void List :: Show()
{
cout << Mark[20] << '\t' << Num[10] << '\t' << ExName[20] << '\t' << Year << '\t' << PastRoad << endl;
}
void List :: Print()
{
out << Mark[20] << '\t' << Num[10] << '\t' << ExName[20] << '\t' << Year << '\t' << PastRoad << endl;
}
List **PutInStruct(ifstream& in, ofstream& out, int pastRoad);
int main()
{
if (!in)
{
cout << "Error" << endl;
system("pause");
return -1;
}
cout << "Enter required PastRoad " << endl;
int pastRoad;
cin >> pastRoad;
**PutInStruct(in, out, pastRoad);
system("pause");
return 0;
}
List **PutInStruct(ifstream& in, ofstream& out, int pastRoad)
{
List AutoList;
while (in.peek() != EOF)
{
in >> AutoList.Mark;
in >> AutoList.Num;
in >> AutoList.ExName;
in >> AutoList.Year;
in >> AutoList.PastRoad;
if (AutoList.PastRoad < pastRoad)
{
AutoList.Print();
}
}
}
Answer the question
In order to leave comments, you need to log in
A List constructor with default parameters would be nice.
And instead of char* use std::string
Typo out
void List :: Print()
{
out << Mark[20] << '\t' << Num[10] << '\t' << ExName[20] << '\t' << Year << '\t' << PastRoad << endl;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question