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
I think your problem is solved using new operator. See http://www.cplusplus.com/doc/tutorial/dynamic.html . If this does not solve your problem of an arbitrary number of customers, do get back to me.

When you find time, do go through Bruce Eckel's book on C++, which has both an online and offline versions, or some other C++ book.

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Amit Kumar

Expertise

I can answer Cplusplus language and library questions, including STL, ACE, Boost. I have a good background in Mathematics.

Experience

Programming in CPP for about 8 years. Industry experience of 4 years.

Education/Credentials
Bachelors and Masters in Computer Science from Indian Institute of Technology Delhi.

©2016 About.com. All rights reserved.