i m making a hotel database management system and i have to add records, update and delete them.i m having problem in getting the input,how will i use double pointers?? and i have to get a certain number of records from the user.

here is my code:

void addcustomer()
{
	int n;

	cout << " ------- Add Customer Record --------" << endl << endl;

	cout << "Please Enter the number of records you want to Enter: " << endl;

	cin>>n;

	char ** c=new char*[n];

	cout << "Please Enter Customer Name: " << endl;
	
	// dont know how to get input!

	cout << "Please Enter Customer NIC Number: " << endl;

	
	cout << "Please Enter Customer Telephone: Number " << endl;


	cout << "Please Enter Customer Room Number: " << endl;


	cout << "Please Enter Customer Entry Date and Time: " << endl;


	cout << "Please Enter Customer Exit Date and Time: " << endl;


	cout << "Please Enter Customer Payments: " << endl;


	cout << "Please Enter Customer Status: " << endl;


	





}

how would i get input in the 2d array?

and one more thing,if i get input in one function,would i be able to use the same record in another function?

Recommended Answers

All 18 Replies

-> You get input using 'cin' (e.g. : 'cin >> var;')

-> If you store your results you got from the user into an array declared in the function, it is only accessible by the function, not the whole program, you can declare a global array (NOT RECOMMENDED!) but it could work, you could also declare the array into your main() function and pass it via a reference / pointer to your function...

-> A 2-dimensional array is declared as follows:

[I]type[/I] [I]array[/I][[I]x[/I]][[I]y[/I]]; /* where 'x' and 'y' are the indexes of the array */

i want to know about dynamic 2d array.
you are telling me about simple static array,thanks anyways

I wrote a snippet of code wich can dynamically allocate an array
(! doesn't preserve the existing data !)

void redim(int *&ptr_to_data, int newdim)
{
	delete[] ptr_to_data;
	ptr_to_data = new int[newdim];
}

This is a full example where you see the redim() function working:

#include <iostream>

using namespace std;

void redim(int *&ptr_to_data, int newdim);

int main(void)
{
	int * testptr = new int[1]; // reserve some memory

	testptr[0] = 23;

	cout << testptr[0] << endl;

	redim(testptr, 5); // change the dimension

	testptr[4] = 203;

	cout << testptr[4] << endl;

	delete[] testptr;

	return 0;
}

void redim(int *&ptr_to_data, int newdim)
{
	delete[] ptr_to_data;
	ptr_to_data = new int[newdim];
}


REMARK: You have adapt the code to your needs, this example only shows you how to do this with a one-dimensional array ...

I also would like to advise you to put the data of every customer in a struct (you can create an array of structs) ...

A struct looks like this:

struct customer_struct
    {
        string name;
        string nic_number;
        string telephone_number;
        int room_number;
        string entry_date_time;
        string exit_date_time;
        string payments;
        string status;
    };

The following you have to do is:

. . .
    customer_struct customers;
    customers.name = "John Smit";
    customers.telephone_number = "0123-456-789";
    . . .
#include <iostream>

using namespace std;

int main()
{
	char ** my_arrays;

	my_arrays = new char * [10]; // after this you will have 10 "arrays" - pointers
	my_arrays[3] = new char [10]; // allocate memory for the 4th array - 10 characters for my_array[3]; - 9 can be used last is '\n'

	cout << "Say sg: " << flush;
	
	cin.getline( my_arrays[3], 10 );
	//get 10-1 characters into the 4th "array" of my_arrays
	
	cout << my_arrays[3] << endl; //print out;

	delete[] my_arrays[3]; // free the 4th array
	delete[] my_arrays; // free my_arrays

	cin.get();
	return 0;
}

Hope this helps.

I personally think it's much more simple to use an array of structs, and dynamically reallocate it ...
You don't need an array of pointers in that way...

I personally think it's much more simple to use an array of structs, and dynamically reallocate it ...
You don't need an array of pointers in that way...

I agree, but he is interested in 2D arrays, so I tried to give him an example to show him, how it works. I would create a structure as well, and then go for a vector. :)

arshad115, are you putting all the user input into a 2D array?

-> I can say only one thing about that: It will become a big mess ...

i know,i can do it with structures very easily,but i am not allowed to use structures!..

so if i need to get input of 5 records,how would i know that the record no.2's name and all the other attributes?

so if i need to get input of 5 records,how would i know that the record no.2's name and all the other attributes?

By reading your posted code I assume you want to store 8 attributes (telephone number, name, etc.) per customer ...

So if you need 5 records, you declare an array with 40 (5*8) elements in it, please make sure you put the attributes always in the same order, otherwise it won't work...

e.g: If you want to read out record 2, you do the following: the first element of record 2 is located in the array at position 2*8 = 16, now you increment this value (16) by the number of the attribute and you have the data you want ...

BTW, Why do you need a two-dimensional array?
You can also do it with a one-dimensional ...

i get what u say,but now i m having another problem of accesing the array in the main,when

cout << c[0];

i get an error c undelared identifier!
here is my code:

void addcustomer()
{
	int n;

	cout << " ------- Add Customer Record --------" << endl << endl;

	cout << "Please Enter the number of records you want to Enter: " << endl;

	cin>>n;

	 char **c=new char[n];

	cin.ignore();
	
 for(int i=0;i<n;i++)
 {
	cout << "Please Enter Customer Name: " << endl;
	
	cin.getline(c[i]=new char[20],20);
	
	cin.ignore();

	cout << c[i]<< endl;
	
	cout << "Please Enter Customer NIC Number: " << endl;

	cin.getline(c[i+1]= new char[20],20);
	
	cin.ignore();

	cout << c[i+1]<< endl;

	cout << "Please Enter Customer Telephone: Number " << endl;


	cout << "Please Enter Customer Room Number: " << endl;


	cout << "Please Enter Customer Entry Date and Time: " << endl;


	cout << "Please Enter Customer Exit Date and Time: " << endl;


	cout << "Please Enter Customer Payments: " << endl;


	cout << "Please Enter Customer Status: " << endl;


	
 }




}

how would i access the array in main()??

i cannot declare it globbaly!

That's because you've declared 'c' in your function addCustomer() and not in main() ...

'c' is only accessible in function addCustomer(), when the execution of addCustomer() has finished, the array doesn't exist anymore ...

That's why it's better to declare the whole array in main and pass it to the function via a pointer or a reference ...

The following program demonstrates the principle of passing an array to a function:

#include <iostream>

using namespace std;

void test(int array[]);
long arraysum(int array[], int el_in_arr);

int main(void)
{
    int numbers[] = {1, 2, 3, 4, 5, 6, 7};
    test(numbers);
    int sum = arraysum(numbers, sizeof(numbers)/sizeof(int)); /* this variable sum is another one than the one in arraysum() */
    cout << "The sum of all the elements in the array is: " << sum << endl;
    cin.get();
    return 0;
}

long arraysum(int array[], int el_in_arr)
{
    /* This function returns the sum of all the elements in the array
    passed as argument */

    long sum = 0;

    for(int i = 0; i < el_in_arr; i++)
        sum = sum + array[i];

    return sum;
}

void test(int array[])
{
    /* Change a value in the array */
    array[0] = 5;
}

thanks a lot,but i cant seem to get 2d array input in the function,i have declared it in the main function.

The following code illustrates what you asked for:

#include <iostream>

using namespace std;

void func(char **& ref_to_ptr); /* Function declaration */

int main(void)
{
    /* Declare the '2D Array' */
    char ** ptr = new char * [5];
    ptr[3] = new char[20];

    /* Put some data in the array */
    ptr[3] = "k";

    /* Print the first value on the screen */
    cout << "First value: " << ptr[3] << endl;

    /* Pass the array by reference to the function 'func()' */
    func(ptr);

    /* Again we print on the screen what's in the '2D Array' */
    cout << "Second value: " << ptr[3] << endl;

    /* Wait for the user to press ENTER */
    cin.get();

    /* Tell the Operating System that everything went well */
    return 0;
}

void func(char **& ref_to_ptr)
{
    /* This function demonstrates how to change the value of it's argument(s) */
    ref_to_ptr[3] = "tux4life";
}

In the above code we declare a '2d array' inside the main function, then we pass it to the function 'func()' which changes the value of it ...

Maybe it's also useful to read the following article about pointers and references: http://www.codeguru.com/cpp/cpp/cpp_mfc/pointers/article.php/c4089/

Hope this helps !

The following code is a working example of what you were trying to achieve:

#include <iostream>

using namespace std;

void addcustomer(char **& ref_to_ptr);

int main(void)
{
    char ** customers;

    addcustomer(customers);

    cout << "All the customer data from record 1 = ";

    /* Show Record 1 */
    for(int i = 0; i < 8; i++)
    {
        cout << customers[i] << " ";
    }

    cout << endl;

    delete[] customers;
    return 0;
}

void addcustomer(char **& c)
{
    int n = 0;
    const int ATTR = 8;
    const int MAX_STR_LEN = 25;

    cout << "-------- Add Customer Record --------" << endl << endl;
    cout << "Please Enter the number of records you want to Enter: ";
    cin >> n;
    cout << endl;

    delete[] c;
    c = new char * [(n*ATTR)];

    for(int i = 0; i < (n*ATTR); i++)
    {
        c[i] = new char[MAX_STR_LEN];
    }

    for (int i = 0; i < n; i++)
    {

        cout << "Please Enter Customer Name: ";
        cin.getline(c[i*ATTR], MAX_STR_LEN);
        cout << endl;

        cout << "Please Enter Customer NIC Number: ";
        cin.getline(c[i*ATTR+1], MAX_STR_LEN);
        cout << endl;

        cout << "Please Enter Customer Telephone: Number ";
        cin.getline(c[i*ATTR+2], MAX_STR_LEN);
        cout << endl;

        cout << "Please Enter Customer Room Number: ";
        cin.getline(c[i*ATTR+3], MAX_STR_LEN);
        cout << endl;

        cout << "Please Enter Customer Entry Date and Time: ";
        cin.getline(c[i*ATTR+4], MAX_STR_LEN);
        cout << endl;

        cout << "Please Enter Customer Exit Date and Time: ";
        cin.getline(c[i*ATTR+5], MAX_STR_LEN);
        cout << endl;

        cout << "Please Enter Customer Payments: ";
        cin.getline(c[i*ATTR+6], MAX_STR_LEN);
        cout << endl;

        cout << "Please Enter Customer Status: ";
        cin.getline(c[i*ATTR+7], MAX_STR_LEN);
        cout << endl;
    }
}

REMARK: There's a little bug in this code, I couldn't fix it up until now, when creating the first record, the program always skips the 'Customer Name', in all the records after the first this isn't the case ...

commented: thanks a lot for youy help! +1

thanks a lot, i also made a similar code and it also gave an error similar to your's,it skips the first input and gives a runtime error on the second one.....

and gives a runtime error on the second one.....

With my code I don't get a runtime error on the second one ...
Success with your program !!!

P.S: Maybe you should post your code into a new thread and explain that there's a bug, but that you don't know how to fix it ...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.