Problem in c++ in writing an algorithm

In summary, the conversation was about a program that reads hotel visitor information from a file and contracts similar room numbers while also writing down the overall number of days spent in each room. The conversation includes the code for the program and the input file. The expert summarizer provided a revised and easier-to-read version of the code, which successfully fixed the bug in the original program. The conversation ended with gratitude and a mention of further work on the program.
  • #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)...

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

  • in.txt
    468 bytes · Views: 551
Last edited:
Technology news on Phys.org
  • #2
where is "in.txt" ?
 
  • #3
zyh said:
where is "in.txt" ?

It's inserted as attachment :-)
 
  • #4
This works fine for me, since you code is VERY HARD to read. I change the variable names. And it take me 30 minutes to find your bug through debugging in the code:cry:.
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 CPerson {
           char name[MAX];
           int monthArrive, dayArrive;	//arrive
           int monthLeave, dayLeave;	//leave
           int roomNumber;		//room number
           int days;	//days in hotel
	}A[MAX]; int numberPerson;      
    
    struct CRecord {
           int roomNumber;
           int days;
	}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;
    numberPerson=0;
    while(!fd.eof() ) {
		fd.get(A[numberPerson].name, sizeof A[numberPerson].name);
		fd>>A[numberPerson].monthArrive>>A[numberPerson].dayArrive>>A[numberPerson].monthLeave>>A[numberPerson].dayLeave>>A[numberPerson].roomNumber;
		fd.ignore();
		A[numberPerson].days=((A[numberPerson].monthLeave)-(A[numberPerson].monthArrive))*30+((A[numberPerson].dayLeave)-(A[numberPerson].dayArrive));
		numberPerson++;
	}
	fd.close();
	
	//data output------------------------------------------------------------
	
	ofstream fr("out.txt", ios::app);
	
	fr<<"-------------------------------------------------------------------"<<endl;
	fr<<" Hotel \"Smart house\" visitors information (2008)   "<<endl;
	fr<<"-------------------------------------------------------------------"<<endl;
	fr<<"room roomNumber.  days "<<endl;
	fr<<"-------------------------------------------------------------------"<<endl;
	fr.setf(ios::fixed);
	
	//Algorithm-------------------------
	
	int matchNumbers=0;  
	int recordSize = 0; 
	int i;
	B[0].roomNumber=0;

	for(int m=0;m<numberPerson;m++) { 
		matchNumbers = 0;

		//check the current room number has been recorded.
		for ( i=0;i<recordSize;i++) { 
			if(A[m].roomNumber==B[i].roomNumber ){
				B[i].days += A[m].days; 
				matchNumbers++;
				break;//You just break the loop, because there is no match anymore.
			}
				
		}


		//If there is no record, add a new record.
		if(matchNumbers==0){     
			B[recordSize].roomNumber=A[m].roomNumber;
			B[recordSize].days=A[m].days;
			recordSize++;
		}
		
		cout<<"galas"<<" matchNumbers "<<matchNumbers<<" m "<<m<<" i "<<i<<" recordSize "<<recordSize<<endl;
	}
	
	for(i=0;i<recordSize;i++){
		fr<<setw(8)<< B[i].roomNumber <<setw(8) << B[i].days <<setw(8)<<endl;
	}
	fr<<"-------------------------------------------------------------------"<<endl;
	fr.close();
	
	system("PAUSE");
	return EXIT_SUCCESS;
}
 
  • #5
Thank you very much. It's working! It was difficult to read because I'm new and i use Lithuanian language to identify my variables cause I'm Lithuanian :-) By the way, it's not the end of this program but I hope to understand it by miself... :-) Thank you once again!
 
Last edited:

What is a problem in c++ in writing an algorithm?

A problem in c++ in writing an algorithm refers to a specific issue or difficulty that a programmer may encounter when attempting to create a step-by-step procedure for solving a problem using c++ programming language.

What are some common problems that arise when writing an algorithm in c++?

Some common problems that may arise when writing an algorithm in c++ include issues with syntax errors, logic errors, and implementation errors. Additionally, a programmer may face challenges in optimizing the algorithm for efficiency or ensuring the algorithm runs correctly for all possible inputs.

How can I troubleshoot a problem in c++ when writing an algorithm?

To troubleshoot a problem in c++ when writing an algorithm, it is important to carefully review the code and check for any syntax errors or logical errors. Using debugging tools and running the algorithm with different inputs can also help identify and resolve issues. Seeking help from other programmers or referencing online resources can also provide valuable insights and solutions.

What are some strategies for avoiding problems in c++ when writing an algorithm?

Some strategies for avoiding problems in c++ when writing an algorithm include carefully planning and designing the algorithm before coding, regularly testing and debugging the code, and using best practices and coding conventions. Additionally, staying updated with c++ language updates and seeking advice from experienced programmers can also help prevent potential problems.

How important is problem-solving in c++ when writing an algorithm?

Problem-solving is a crucial aspect of writing an algorithm in c++. It involves breaking down a complex problem into smaller, more manageable steps and finding efficient solutions. Strong problem-solving skills are essential for creating effective algorithms and for debugging any issues that may arise during the coding process.

Similar threads

Replies
10
Views
951
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
2
Replies
66
Views
4K
  • Programming and Computer Science
Replies
20
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
749
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Programming and Computer Science
3
Replies
73
Views
4K
Back
Top