The following is a project that i'm working on at the moment and as i say is work in progress but having huge problem in trying to have a dynamicly re-sized array once the array gets too big.

in the programme which will mean i'll miss out in a huge amount of marks. i'm using const at moment but don't have clue how to do dynamic array:?:
it would be a great help if some one here could show me how to do this in my programme.

thanks alot if any one does


p.s. i know there are bugs in it i'll sort them out only need help with the setting up of dynamic arrays


cheers

Attachments
#include <iostream>
#include <iomanip>
#include<string>
#include <fstream>
#include <stdio.h>

using namespace std;

//#define MAX_PERSONS 100
const int size = 30;
struct bsupplyer

{
    string name, type ,hourlylimit;
    
    int mbs, contention, downloadlimit, contactnum ; 
    
    double connectionfee, excesscharge , cost;
    
};

typedef  bsupplyer BSUPPLYER;   


string name1, type1 ,hourlylimit1; 
int mbs1, contention1, downloadlimit1, contactnum1 ,choice;   
double connectionfee1, excesscharge1 , cost1;
void load_package(BSUPPLYER package_info[]) ;
void list_package(BSUPPLYER package_info[]);
void add_package(BSUPPLYER package_info[]);
void delete_package(BSUPPLYER package_info[]);
int menu();


BSUPPLYER supply[size];


 
 int main()
 {
     system("mode con cols=130 lines=30");
     system("color 2a");
    
    
    while(choice != 5)
    {choice = menu(); 
        load_package( supply) ;
        switch ( choice)
        {
            
        case 1 :    system ( " cls " );
                    add_package(supply);
                    break;
            
        case 2 :    system ( " cls " );
                    delete_package(supply);
                    break;
            
        case 3 :    system ( " cls " );
                    break;
            
        case 4 :    system ( " cls " );
                    list_package( supply) ;
                    break;
            
        case 5 :    
                    return 0;
                    break;
            
        }   
    }    
}



void load_package(BSUPPLYER package_info[]) 
{
    
    ifstream data("packages.txt", ios::in);
   data.seekg(0);
    
    for(int i = 0; !data.eof(); i++)
    {
       data>>package_info[i].name>>package_info[i].type>>package_info[i].mbs>>package_info[i].hourlylimit
       >>package_info[i].contention>>package_info[i].downloadlimit>>package_info[i].connectionfee
       >>package_info[i].excesscharge>>package_info[i].contactnum>>package_info[i].cost;
     
       
    }
}



int menu()
{           int c;    
            system("CLS");
            cout << endl << endl << endl << endl ;
            cout << setw (65) << " Welcome to callcosts.ie       " << endl;
            cout <<endl << setw (40) << " 1 : Add packages details  " << endl<<endl;
            cout << setw (40) << " 2 : Delete package details    " << endl<<endl;
            cout << setw (40) << " 3 : amend details on a package" << endl<<endl;
            cout << setw (40) << " 4 : List packages             " << endl<<endl;
            cout << setw (40) << " 5: Quit                       " << endl;
            cout << endl << endl << endl << setw (40) << " Please enter choice :  "; 
            cin >> c;  
            
            return c;  
    
}

void list_package(BSUPPLYER package_info[])
{
       
   for( int i =0;   package_info[i].mbs != 0; i++)
    {    
        
        cout<<package_info[i].name <<" "<<package_info[i].type<<" "<<package_info[i].mbs<<" "
        <<package_info[i].hourlylimit<<" "<<package_info[i].contention<<" "<<package_info[i].downloadlimit
        <<" "<<package_info[i].connectionfee<<" "<<package_info[i].excesscharge<<" "<<package_info[i].contactnum<<" "
        <<package_info[i].cost<<endl;
    
    } 
    system("pause");
}



void add_package(BSUPPLYER package_info[])
{
    
    int i =0;
    
      while ( package_info[i].mbs !=  0 && i < size)

              i++;

  
     
   if ( i == size )

          cout << "\nsorry, the database is full\n" ;

   else             

      {
          
          

         cout <<  "\nPlease enter broadband provider : ";
         cin >>  package_info[i].name  ; 

         cout <<  "Please enter type of service e.g. wireless/dsl: " ;
         cin >>  package_info[i].type;

         cout <<  " Broadband speed :" ;
         cin >>  package_info[i].mbs ;

         cout <<  "  Hourly limit ? "  ;
         cin >>  package_info[i].hourlylimit;

         cout <<  "   contention ratio ?"  ;
         cin >>  package_info[i].contention;

         cout <<  "Download limit ";
         cin >>  package_info[i].downloadlimit ;

         cout <<  "what is the connection fee"  ;
         cin >> package_info[i].connectionfee ;

         cout <<  "  what is the excess charge for going over the limit " ;
         cin >> package_info[i].excesscharge;

         cout <<  "   what is the contact number"  ;
         cin >> package_info[i].contactnum ;
         
         cout <<  "   total cost of broadband package"  ;
         cin >> package_info[i].cost ;
         
     }
     ofstream data("packages.txt", ios::out);

    for( int i =0;   package_info[i].mbs != 0 ; i++)
    { 
       data<<" "<<package_info[i].name<<" "<<package_info[i].type<<" "<<package_info[i].mbs
       <<" "<<package_info[i].hourlylimit<<" "<<package_info[i].contention<<" "<<package_info[i].downloadlimit<<" "
       <<package_info[i].connectionfee<<" "<<package_info[i].excesscharge<<" "<<package_info[i].contactnum<<" "<<package_info[i].cost
       <<endl;
    }
  
        
}


    
void delete_package(BSUPPLYER package_info[])
{
  
  char decision;
  int limit = 10; 
  int i =0; 
  
  
  
    while ( decision != 'Y' )
    {
    system("CLS");
        
        
        
        
        while(    i < limit && package_info[i].mbs != 0 )
        {    
            
            cout<<package_info[i].name <<" "<<package_info[i].type<<" "<<package_info[i].mbs<<" "
            <<package_info[i].hourlylimit<<" "<<package_info[i].contention<<" "<<package_info[i].downloadlimit
            <<" "<<package_info[i].connectionfee<<" "<<package_info[i].excesscharge<<" "<<package_info[i].contactnum<<" "
            <<package_info[i].cost<<endl;
            i++;
        
        } 
            limit = limit + 10;
            cout << " Is the package you want to delete on the screen y/n or e to exit ";
            cin >> decision;
            decision = toupper(decision);
            if ( package_info[i].mbs != 0 )
            {
            break;
}            
    }    
    
    
    int reset = -1;
    
    cout << " Please enter the package details that you want deleted " << endl;
    cin >> name1 >> type1 >> mbs1 >> hourlylimit1 >> contention1 >> downloadlimit1 
        >> connectionfee1 >> excesscharge1 >>contactnum1 >> cost1;
        
        while( ! cin )
        {  
            cin.clear();
           cin.ignore(80,'\n');
           cout << " Please enter the package details that you want deleted " << endl;
           cin >> name1 >> type1 >> mbs1 >> hourlylimit1 >> contention1 >> downloadlimit1 
               >> connectionfee1 >> excesscharge1 >>contactnum1 >> cost1;
        }
    
    
    for ( int f =0; package_info[f].mbs != 0;f++)
    {
        if ( package_info[f].name == name1, package_info[f].type == type1, package_info[f].mbs == mbs1, package_info[f].hourlylimit == hourlylimit1,
             package_info[f].contention == contention1, package_info[f].downloadlimit == downloadlimit1, package_info[f].connectionfee == connectionfee1,
             package_info[f].excesscharge == excesscharge1, package_info[f].contactnum == contactnum1,package_info[f].cost == cost1)
        {
             package_info[f].name = reset, package_info[f].type = reset, package_info[f].mbs = reset, package_info[f].hourlylimit = reset,
             package_info[f].contention = reset, package_info[f].downloadlimit = reset, package_info[f].connectionfee = reset, package_info[f].excesscharge = reset, 
             package_info[f].contactnum = reset,package_info[f].cost = reset;
             
        }
        else
        {
        cout << " You have entered the wrong package details " ;  
        break; 
        }
    
    } 
    
    
    
    
   
      ofstream data("packages.txt");
    
    for( int i =0;   package_info[i].mbs != 0; i++)
    { 
       data<<package_info[i].name<<" "<<package_info[i].type<<" "<<package_info[i].mbs<<" "
       <<package_info[i].hourlylimit<<" "<<package_info[i].contention<<" "<<package_info[i].downloadlimit<<" "
       <<package_info[i].connectionfee<<" "<<package_info[i].excesscharge<<" "<<package_info[i].contactnum<<" "<<package_info[i].cost<<endl;
    }
      
    
    system("pause");
    
}
churus dsl 3 40 30 40 99 5 2642412 20
perlico dsl 3 33 30 40 99 5 2642412 20
churus wireless 3 33 30 40 99 5 2342412 20
ntl dsl 3 33 30 40 99 5 2642812 20
btireland wireless 1 80 35 20 49 10 3434343 23
sky wireless 5 yes 30 40 99 5 2642032 23
ntl dsl 5 yes 30 40 99 5 2645032 24
smart dsl 1 yes 30 80 110 0 3747376 25
perlico dsl 1 2 3 4 5 6 67676 28
ntl wireless 4 22 30 40 99 5 2642912 30
eircom wireless 1 2 3 4 5 6 57485 31
perlico wireless 1 2 3 4 5 6 7654456 33
eircom dsl 1 2 3 4 5 6 67676 34
eircom dsl 1 yes 3 6 5 6 4343 37
smart dsl 1 2 3 4 5 6 4343 37
smart dsl 4 yes 30 50 90 5 8765755 40
smart dsl 9 8 7 6 5 4 3545 42
magnet wireless 5 22 30 40 99 5 2642834 46
magnet wireless 3 22 30 40 99 5 2642834 46
ntl dsl 5 22 30 40 99 5 2642834 46
churus wireless 3 33 30 40 99 5 2642812 50
eircom dsl 1 20 30 50 99 10 2647012 50
magnet dsl 4 22 30 40 99 5 2642912 51
eircom dsl 4 22 30 40 99 5 2642912 51
eircom dsl 6 30 30 40 99 5 2642012 52
ntl dsl 1 2 3 4 5 6 55555 55
magnet dsl 6 yes 20 50 200 5 8675858 59
steve dsl 3 2 3 4 5 6 7 6

trying to have a dynamicly re-sized array

That's not possible, just make sure it's big enough

Edited 6 Years Ago by Nick Evan: n/a

That is a c++ program so why don't you just use a vector of BSUPPLYER objects

vector<BSUPPLYER> supply;

The vector is an array that will increase and shrink itself as you desire. You don't have to bother about memory allocation.

The following is a project that i'm working on at the moment and as i say is work in progress but having huge problem in trying to have a dynamicly re-sized array once the array gets too big.

in the programme which will mean i'll miss out in a huge amount of marks. i'm using const at moment but don't have clue how to do dynamic array:?:
it would be a great help if some one here could show me how to do this in my programme.

thanks alot if any one does


p.s. i know there are bugs in it i'll sort them out only need help with the setting up of dynamic arrays


cheers

Since the memory is not allocated during run time for arrays, you cannot resize it. The option is to use Linked list or vectors.
(if you know the concept of smart pointers) you can create the linked list and using the smart pointers you can make your program access the data like an array. (using increment and decrement operators to point to the next/prev item...)

Regards
Murthy

Since the memory is not allocated during run time for arrays, you cannot resize it. The option is to use Linked list or vectors.
Regards
Murthy

what gave you the idea that arrays can not be reallocated? new, malloc, or realloc() can be used to do that. Linked lists and vectors are better opions, but not the only options.

// supplySize indicates the current size of the array.  When elementsUsed equals supplySize then the array must be increased
int supplySize = 25;
// elementsUsed intidates how many elements in the array have been used.  It should be increased each time a new element in the array is used
int elementsUsed = 0;

// array with initial allocation.
BSUPPLYER * supply = new BSUPPLYER[supplySize];

// now increase the array size

if( (elementsUsed+1) == supplySize)
{
   // add one new element to the array
   BSUPPLYER * temp = new  BSUPPLYER[supplySize+1];
   // copy data from old to new array
   for(int i = 0; i < arraySize; ++)
   {
        temp[i] = supply[i];
   }
   // delete old array
   delete[] supply
   // rename old to new
   supply = temp;
   // bump array counter
   ++supplySize;
}

what gave you the idea that arrays can not be reallocated? new, malloc, or realloc() can be used to do that.

Hello.

Actual arrays can't be resized, what you are talking about is data pointers being allocated enough memory to be used as arrays.

The important differentiation here being used as arrays and actual arrays.

Thank you.

The important differentiation here being used as arrays and actual arrays.

Thank you.

But they can be used as arrays. No difference except the allocation method. (and a couple other minor differences).

[edit]I reread your statement again and see the distinction you were trying to make. I believe the distinction should be between statically allocated and dynamically allocated arrays. The are both arrays, its just how they are allocated is the difference. It's a matter of getting the terminology correct. statically allocated arrays can not be reallocated. Others can. [/edit]

I read it in a thread on google group that mixing arrays and pointers interchangbly was not good so just thought I would point it out.

There is a lot of difference between something which is an array and something which is used as an array.

Thank you.

I read it in a thread on google group that mixing arrays and pointers interchangbly was not good so just thought I would point it out.
Thank you.

Well, they were wrong. There are millions of programs out there that use dynamically allocated arrays as I posted in my example.

But in c++ programs it isn't necessary because, as mentioned by both of us mentioned previously, c++ has vector class which handles all the messy allocation stuff. And I believe that is how vector class is normally implemented -- dynamically allocating C-style arrays.

There is a lot of difference between something which is an array and something which is used as an array.

Name one. Anything you can do with statically allocated arrays can be done with dynamically allocated arrays -- the reverse however is not true.

thanks alot i shall try that this evening and let you know how i get on tomorrow.

and the reason i'm not using vectors is because we are not allowed. we have to make dynamic arrays using a series of pointers layed out in a function. and call this function once the array gets full and then double size of array or multiply by 1.5 or something.

>Well, they were wrong.
It depends on what they were referring to, but I'm going to assume they were correct if it was comp.lang.c. Wrong information doesn't last long on that group.

>Name one.
I can name three:

1) Take the sizeof a dynamically allocated array.
2) Take the address of a dynamically allocated array.
3) Initialize a dynamically allocated array to a string literal.

Those are the three cases where an array is used in object context instead of value context, and because a dynamically allocated array is not an array, the effect is different.

This article has been dead for over six months. Start a new discussion instead.