01/10/2018, 14:50

Code bài tập đọc ghi sau khi nhập số lượng thì tắt luôn

Em có làm thửu bài tập là
Ghi vào file về tên smartphone và giá…
Em làm nhưng khi complie thì vừa nhấn số lượng smartphone (như ảnh) xong thì nó tắt luôn ạ…

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;

struct smartphone
{
	string name;
	int cost;
};

void writedata(smartphone DT, int &n)
{
	ofstream data("dienthoai", ios::in | ios::out);
	data.open("dienthoai");
	data << setw(50) << left << "Name";
	data << setw(20) << right << "Cost" << endl;
	for (int i = 0; i < n; i++)
	{
		data << setw(50) << left << DT.name;
		data << setw(20) << right << DT.cost << endl;
	}
	data.close();
}

void readdata(smartphone DT, int &n)
{
	ifstream data("dienthoai", ios::in | ios::out);
	data.open("dienthoai");
	if (data.fail())
	{
		cout << "File khong ton tai" << endl;
		exit(1);
	}
	else
	{
		cout << "Okela" << endl;
	}
	while (!data.eof())
	{
		cout << setw(50) << left << DT.name;
		cout << setw(20) << right << DT.cost << endl;
	}
	data.close();
}

int main()
{
	smartphone DT;
	int n;
	cout << "Nhap so smartphone : ";
	cin >> n;
	writedata(DT, n);
	readdata(DT, n);
	system("pause");
	return 0;
}

Như ảnh này ạ:

Mong mọi người giúp em với…
Em cám ơn <3

Bài liên quan
0