You are here:

C++/Cplusplushelp

Advertisement


Question
Hello, I am sorry that i gave you the source in greeklish, this is the english one. The code will be for a tourist office that will has these abilities:
1)New excurssion
2)New customer
3)New customer to an existing excursion
4)print for customer from an existing excursion
5)print the balance o all customers
6)print the balance for a specific customer.

Now i have done a source code which needs to be seen and i need some help on the 4th choice, take a look and please help me for any corrections or advice! Untill now this code, as i wrote it, it saves all in 2 different xls files, one for the excursion (1st column-ID of the excursion, 2nd-Name of country and 3rd,4th,5th,.... the id of the customers that are on the specific excursion). Now i want you please to help me get for each excursion the customers id and call the function printspec for viewing their names, my problem is that each excursion(row) may have random size of customers.

#include <iostream.h>
#include <fstream.h>
#include <string>
#include <stdlib.h>

class Excursions
{
  friend void printspec(int);
  char to[20];
  int customers[200];
  public:
  void new_excursion(int);
  void set(char[],int[],int);
  void print(int);
  void add(int);
};

void Excursions::set(char to_[],int id[],int k)
{
  int i;
  strcpy(to,to_);
  for(i=0;i<k;i++)
  customers[i]=id[i];
}

void Excursions::new_excursion(int k)
{
  int count=1,i;
  fstream file_op("Excursions.xls",ios::in);
  while(!file_op.eof())
  {
     if(file_op.get()=='|')
     {count++;}
  }
  file_op.close();
  fstream in_excursion("Excursions.xls",ios::app);
  in_excursion<<count<<"\t"<<to<<"\t";
  for(i=0;i<k;i++)
     in_excursion<<customers[i]<<" ";
  in_excursion<<"|\n";
  in_excursion.close();
  cout<<"\nSuccesful"<<endl;
}


void Excursions::print(int a)
{
  int id,i=1;
  char ch='a';
  fstream file_op("Excursions.xls",ios::in);
  customers[0]=0;
   while(!file_op.eof())
   {
     file_op>>id>>to;
     while(ch != '|'){
     file_op>>customers[i]>>ch;
     i++;}
     if(id==a)
     {
        for(i=1;customers[i]!=0;i++)
        {

         printspec(customers[i]);
        }

     }
  }
  file_op.close();
}

void Excursions::add(int a)
{
  int count=0,k,i,id;
  char ch;
  fstream file_op("Excursions.xls",ios::in);
  file_op.seekg(ios::beg);
  fstream temp("Temp.xls",ios::in|ios::out);
   while(!file_op.eof())
   {
     file_op.get(ch);
     temp<<ch;
  }
  temp.seekg(ios::beg);
  file_op.close();
  fstream file_opnew("Excursions.xls",ios::out);
  while(!temp.eof())
   {
     temp.get(ch);
     if(ch=='|'){count++;}
     if(count==a)
     {
        cout<<"\nHow many customers are you going to add?:";
        cin>>k;
        for(i=0;i<k;i++)
        {
         cout<<"\nGive tha Ids of the customers:";
         cin>>id;
         file_opnew<<id<<" ";
        }
        count++;
     }
     file_opnew<<ch;
  }

  file_opnew.close();
  temp.close();
  fstream tempclear("Temp.xls",ios::trunc);
  tempclear.close();
}
  








class customers
{
  char name[30],surname[30];
  int tel,balance;
  public:
  void new_customer();
  void set(char [],char [],int,int);
  void print();
  friend void printspec(int);
};

void customers::set(char name_[],char surname_[],int balance_,int tel_)
{
  strcpy(name,name_);
  strcpy(surname,surname_);
  balance=balance_;
  tel=tel_;
}

void customers::new_customer()
{
  int count=1;
  fstream op_customers("customers.xls",ios::in);
  while(!op_customers.eof())
  {
     if(op_customers.get()=='|')
     {count++;}
  }
  op_customers.close();
  fstream in_customers("customers.xls",ios::app);
  in_customers<<count<<"\t"<<name<<"\t"<<surname<<"\t"<<balance<<"\t"<<tel<<"\t|\n";
  in_customers.close();
  cout<<"Katxwrisi Epityxis"<<endl;
}

void customers::print()
{
  char str[2000];
   fstream file_op("customers.xls",ios::in);
  cout<<"ID\t|name\t\t|Surname     |balance    |Tel"<<endl;
   while(!file_op.eof())
   {
     file_op.getline(str,2000);
     cout <<str<<endl;
  }
  file_op.close();
   cout <<endl;
}

void printspec(int a)
{
  int id,balance,tel;
  char name[20],surname[20],ch;
   fstream file_op("customers.xls",ios::in);
  cout<<"ID\t|name\t\t|Surname     |balance    |Tel"<<endl;
   while(!file_op.eof())
   {
     file_op>>id>>name>>surname>>balance>>tel>>ch;
     if(id==a && ch=='|')
     {
        cout<<id<<"\t"<<name<<"\t"<<surname<<"\t"<<balance<<"\t"<<tel<<endl;
        id++;
     }
  }
  file_op.close();
   cout <<endl;
}













int main()
{
  int a,i,k,balance,tel,id[200];
  char name[30],surname[30];
  customers Pel;
  Excursions Ekd;

  while(a!=9)
  {
     cout<<"Pick a choice:\n\n1- New Excursions\n2- New Customer\n3- Add customers to an existing Excursion\n4- Print customers for a specific Excursion\n5- Print the balance of all customer\n6- Print the balance of a specific customer\n\n9- Exit\n\nChoose:";
     cin>>a;
     switch(a)
     {
     case 1:cout<<"You chose: New Excursions"<<endl;
        cout<<"\nGive the name of the new Excursion:";
        cin>>name;
        cout<<"\nHow many customers are you going to add?:";
        cin>>k;
        for(i=0;i<k;i++)
        {
         cout<<"\nGive the ID of the customers:";
         cin>>id[i];
        }
        Ekd.set(name,id,k);
        Ekd.new_excursion(k);break;
     case 2:
        cout<<"You chose: New Customer"<<endl;
        cout<<"\nGive the name and surname:";
        cin>>name;
        cin>>surname;
        cout<<"Phone Number:";
        cin>>tel;
        cout<<"Give the balance:";
        cin>>balance;
        Pel.set(name,surname,balance,tel);
        Pel.new_customer();break;
     case 3:cout<<"You chose: Add customers to an existing Excursion"<<endl;
        cout<<"\nGive the ID of th Excursion:";
        cin>>k;
        Ekd.add(k);break;
     case 4:cout<<"You chose: Print customer for a specific Excursion"<<endl;
        cout<<"\nGive the ID of the excursion:";
        cin>>k;
        Ekd.print(k);break;
     case 5:cout<<"You chose: Print the balance of all customer"<<endl;
        Pel.print();break;
     case 6:cout<<"You chose: Print the balance of a specific customer"<<endl;
        cout<<"\nGive the ID of the customer:";
        cin>>k;
        printspec(k);break;
     case 9:cout<<"You chose: Exit"<<endl;break;
     default:cout<<"Wrong number"<<endl;
     }
  }

  

  return 0;
}

Answer
Hi again Evagoras,

Thanks very much for the translation, that really helps a lot. This may be missing the point (and tell me if it is), but why not just add an element to your excursions class titled NumberOfCustomers, or something like that, which you increment each time you call the add customer function? Then in this part:

  while(!file_op.eof())
  {
file_op>>id>>to;
while(ch != '|'){
file_op>>customers[i]>>ch;
i++;}
if(id==a)
{
for(i=1;customers[i]!=0;i++)
{

printspec(customers[i]);
}


you could just call printspec using a for loop, while i < CustomerNumber, or whatever. Again, I apologize if I'm missing the issue entirely :). If  I am, please correct me and I'll spend as much time as it takes until we get this figured out.

P.S. I'm really sorry I've been taking so long to help you out, this week has been terribly hectic.

Thanks again for the translation, look forward to hearing from you soon,

Sean

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Sean Wcisel

Expertise

I can answer most beginner and intermediate level questions. My strengths are various algorithms and data manipulation. My background might be best suited to anything from basic CPP and file I/O, to algorithms involving recursion, hashing, binary search trees, and so on. I've done some tutoring in my free time, both online and in person. I also have quite a bit of debugging experience. I have some experience with game code, specifically using D3D based game engines. I only speak English, but I believe I do so very well :). I'm completely unable to answer questions about coding drivers for hardware, and many library specific issues.

Experience

I've studied CPP for roughly four years now, with a combined eight years of object oriented programming experience. I have worked professionally in the field for about a year. I have also had some tutoring experience in CPP and C#

Organizations
IEEE, Western Michigan University Computer Club

Education/Credentials
Western Michigan University

©2016 About.com. All rights reserved.