- #1
SimonasV
- 6
- 0
Hello,
please help me to find the problem in algorithm. My task is to read from file hotel visitor's info (name/surname, date of arrival, date of departure, room number). Than, to contract similar room numbers and write near the overall number of days in particular room. So everything is good except my algorithm which should contract and write down the overall number of days. Instead of this, it writes and counts correctly just first rooms data... (I have attached the input file) and my code bellow: (and sorry for perhaps inaccurate language)...
please help me to find the problem in algorithm. My task is to read from file hotel visitor's info (name/surname, date of arrival, date of departure, room number). Than, to contract similar room numbers and write near the overall number of days in particular room. So everything is good except my algorithm which should contract and write down the overall number of days. Instead of this, it writes and counts correctly just first rooms data... (I have attached the input file) and my code bellow: (and sorry for perhaps inaccurate language)...
Code:
#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#define MAX 30
using namespace std;
int main(int argc, char *argv[])
{//description of structures----------------------------------------------------------------
struct viesbutis {
char id[MAX];
int ma, da; //arrive
int mi, di; //leave
int nr; //room number
int paru; //days in hotel
}A[MAX]; int n;
struct lygina {
int nr1;
int paru1;
}B[MAX];
//data input----------------------------------------------------------
ifstream fd ("in.txt");
if(!fd) {
cout<<"Error with \"in.txt\" file."<<endl;
system("PAUSE");
return 0;
}
else cout<<"Data loaded to \"out.txt\""<<endl;
n=0;
while(!fd.eof() ) {
fd.get(A[n].id, sizeof A[n].id);
fd>>A[n].ma>>A[n].da>>A[n].mi>>A[n].di>>A[n].nr;
fd.ignore();
A[n].paru=((A[n].mi)-(A[n].ma))*30+((A[n].di)-(A[n].da));
n++;
}
fd.close();
//data output------------------------------------------------------------
ofstream fr("out.txt", ios::app);
fr<<"-------------------------------------------------------------------"<<endl;
fr<<" Hotel \"Smart house\" visitors information (2008) "<<endl;
fr<<"-------------------------------------------------------------------"<<endl;
fr<<"room nr. days "<<endl;
fr<<"-------------------------------------------------------------------"<<endl;
fr.setf(ios::fixed);
//Algorithm-------------------------
int k=0;
int size = 0;
int i;
B[0].nr1=0;
for(int m=0;m<n;m++)
{ k = 0;
for ( i=0;i<size;i++)
{
if(A[m].nr==B[i].nr1 )
B[i].paru1 += A[m].paru;
k++; }
if(k==0)
{
B[size].nr1=A[m].nr;
B[size].paru1=A[m].paru;
size++;
}
cout<<"galas"<<" k "<<k<<" m "<<m<<" i "<<i<<" size "<<size<<endl;
}
for(i=0;i<size;i++){
fr<<setw(8)<< B[i].nr1 <<setw(8) << B[i].paru1 <<setw(8)<<endl;}
fr<<"-------------------------------------------------------------------"<<endl;
fr.close();
system("PAUSE");
return EXIT_SUCCESS;
}
Attachments
Last edited: