Chào các anh chị...
Hiện em đang học C++ và gặp 1 bài bí không biết sai chỗ nào
để bài là nhập nhiều lần ngày tháng năm và xuất ra file.txt
ví du:
5
1 2 2000
2 1 2001
3 1 2002
4 1 2000
5 1 2006
xuất ra file :
5
1 2 2000
2 1 2001
3 1 2002
4 1 2000
5 1 2006
đây là code của e :
Code:
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
using namespace std;
typedef struct date
{
int d,m,y;
}date;
typedef struct list
{
int n;
date *dt;
}list;
void khoitao(list &l)
{
l.n=0;
l.dt=new date;
}
date nhap1lan()
{
date dt;
do
{
cout<<"nhap ngay: ";
cin>>dt.d;
}while(dt.d<0||dt.d>31);
do
{
cout<<"nhap thang: ";
cin>>dt.m;
}while(dt.m<0||dt.m>12);
cout<<"nhap nam: ";
cin>>dt.y;
return dt;
}
void nhap(list &l)
{
cout<<"nhap so lan ngay thang nam: ";
cin>>l.n;
for(int i=0;i<l.n;i++)
{
cout<<"lan thu "<<i+1<<": "<<endl;
l.dt[i]=nhap1lan();
}
}
void xuat1lan(const date &dt)
{
cout<<"\t+ ngay thang nam: "<<dt.d<<"/"<<dt.m<<"/"<<dt.y<<endl;
}
void xuat(const list &l)
{
for(int i=0;i<l.n;i++)
{
cout<<"lan thu "<<i+1<<": "<<endl;
xuat1lan(l.dt[i]);
}
}
void outfile(const list &l)
{
ofstream fout("inngay.txt",ios::out);
fout<<l.n<<endl;
for(int i=0;i<l.n;i++)
{
fout<<l.dt[i].d<<endl;
}
fout.close();
}
void main()
{
list l;
nhap(l);
xuat(l);
outfile(l);
_getch();
}
Mong anh chị giúp đỡ
Thân