i m having so many problems,i m trying to make an hotel database management program in win32 console and i m not allowed to use structures,only variables or pointer or Dynmaic arryas.

i m using 2d dynamic array,i have to pass it to a function to store values in it,i cant get input,
i have a main menu,and the input must be inside another function.please help!

here is my code:

#include "stdafx.h"
#include "iostream"
#include "windows.h"
#using <mscorlib.dll>

using namespace System;
using namespace std;


void record();
void trans();
void addcustomer(char *c,int n);
void addhotel();
void queries();
void sort();
void search();
void showittome();


void trans()
{
	int t;

	cout << "--------Transactions Menu------------" << endl << endl;

			cout << "To Add A Record,Press 1 " << endl;

			cout << "To Delete A Record,Press 2 " << endl;
			
			cout << "To Update A Record,Press 3 " << endl;

		cin >> t;

	switch(t)
	{
	case 1:
		{
			system("cls");
			record();
			break;
		}
	case 2:
		{
			system("cls");

			break;
		}
	case 3:
		{
			system("cls");

			break;
		}
	}
	


}

void record()
{
	int r;

	cout << "-----------Record Menu------------" << endl << endl;

	cout << "To Add A Customer Record,Press 1 " << endl;

	cout << "To Delete A Hotel Record,Press 2 " << endl;

	cin>> r;

	

	switch(r)
	{
	case 1:
		{
			system("cls");
			addcustomer("arshad",1);
			break;
		}
	case 2:
		{
			system("cls");
			addhotel();
			break;
		}

	}

}

void addcustomer(char *c,int n=0)
{
	

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

	

	
	
 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;


	
 }




}

void addhotel()
{
	cout << " ------- Add Hotel Record --------" << endl << endl;

	cout << "Please Enter Number Of Rooms Available: " << endl;


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


	cout << "Please Enter Room Type: " << endl;


	cout << "Please Enter Room Charges: " << endl;


	cout << "Please Enter Number Of Present Rooms: " << endl;




}

void queries()
{

	int q;

	cout << " ---------- Queries Menu --------" << endl << endl;

	cout << "To Check Availability Of a Given Room,Press 1 " << endl;

	cout << "To Check The Status of Customer by His Name or NIC,Press 2 " << endl;
			
	cout << "To Calculate Bill of a Customer Based On the Number of Days Stayed,Press 3 " << endl;

	cin >> q;

	switch(q)
	{
	case 1:
		{
			system("cls");
			
			break;
		}
	case 2:
		{
			system("cls");

			break;
		}
	case 3:
		{
			system("cls");

			break;
		}
	}
	


}

void sort()
{
	cout << " ---------- Sorting Menu --------" << endl << endl;



}

void search()
{
	cout << " ---------- Searching Menu --------" << endl << endl;


}
void showittome()
{
	cout << " ---------- Print All Data Menu --------" << endl << endl;



}
int _tmain()

{
	int choice,n;

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

	cin>>n;

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

	 cin.ignore();


    cout << "Database Hotel Management System ...." << endl << endl;


	cout << "To Go to Transactions Menu,Press 1 " << endl;

	
	cout << "To Go to Queries Menu,Press 2 " << endl;
	
	
	cout << "To Go to Sorting Menu,Press 3 " << endl;

	
	cout << "To Search The Existing Record,Press 4 " << endl;

	
	cout << "To Show All Database Information,Press 5 " << endl;

	cin >> choice;

	switch(choice)
	{
	case 1:
		{
			system("cls");
			trans();
			break;
		}
	case 2:
		{
			system("cls");
			queries();
			break;
		}
	case 3:
		{
			system("cls");
			sort();
			break;
		}
	case 4:
		{
			system("cls");
			search();
			break;
		}
	case 5:
		{
			system("cls");
			showittome();
			break;
		}



	}

		
	return 0;
}

Recommended Answers

All 7 Replies

i also tried to get input in a simple for but it didnt work either

// test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "iostream"

using namespace std;

char **b(char *p)
{

	cin.getline(p[0],20);
	
cout << "got that" << endl;

return &p;}

int _tmain(int argc, _TCHAR* argv[])
{
	
	char *p=new char [20];

	cin.ignore();

	;

	//cin.getline(p,20); 
	b(p);

	cout <<endl <<  *p << endl ;
	return 0;
}

This an awfully ambitious assignment to not allow structures. To do this with just the arrays, you'll probably need a few different data elements.

You need to know the status of all rooms, to include their costs and available/occupied status.

Secondly, you need to track the customers and their information.

You should get out pencil and paper and first diagram what you need to store, and how the two main data arrays will interact.

To allocate the 2D array of char, you've only done half the job.

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

	 char **c=new char*[n];  //this establishes how many rows
         for( int i = 0; i < n; i++ )
              c[i] = new char[size_of_row]; //allocate the rows

When you do anything with this array in a function, you should include the number of rows and the row size as parameters, so that you can write the function in a general way.

When you write an input function, like in your second sample, you don't need to (generally don't want to) return a char* or char**. You store data in the array that's passed to the function. So your function, corrected, might simply be:

void b(char *p)
{
	cin.getline(p,20);
	
       cout << "got that" << endl;
}

To fill a 2D array of char (which we'll treat as a 1D array of strings)

int fill_strings( char **p, int rows, int cols )
{
        int count = 0;
        while( count < rows && cin.getline( p[count], cols )
	    count++;
	
         return count;
}

When displaying a string (char array), you don't need to dereference the pointer. Iostream knows to treat a char array special.

cout <<endl <<  *p << endl ;   //wrong
cout <<endl <<  p << endl ; //correct
#include <iostream>

using namespace std;

void b( char** p )
{
	*p = "some text";
}

int main()
{
	char *p = new char [20];
	b( &p );
	
	cout << p << endl ;

	cin.get();
	return 0;
}

But, isn't it wiser to pass the pointer by reference? - Generally speaking of course. Because if you pass the pointer by parameter, you just make a copy of the original pointer. And by manipulating its copy, you would leave the original pointer in the main() function intact, and it would still have its old position. And it might cause you some headaches.

Bevox

But, isn't it wiser to pass the pointer by reference? - Generally speaking of course. Because if you pass the pointer by parameter, you just make a copy of the original pointer. And by manipulating its copy, you would leave the original pointer in the main() function intact, and it would still have its old position. And it might cause you some headaches.

You are passing a pointer by value, but the function will be manipulating the memory pointed to by the pointer. So yes, our function can change that memory allocated and owned by main, without having to introduce further complication in the function parameters.

Your code

#include <iostream>
using namespace std;

void b( char** p ) //passing in 2D array of char? or address of a pointer?
{
	*p = "some text"; //this works, but I'm not sure why!
}

int main()
{
	char *p = new char [20]; //allocate array of 20 char
	b( &p );  //pass the address of the pointer? double indirection
	
	cout << p << endl ;

	cin.get();
	return 0;
}

I think you'll find this a more common approach

#include <iostream>
using namespace std;

void b( char* p ) //passing a pointer, that is, an array
{
	strcpy( p,  "some text" );  //copy to the array
	//or, what you really need for an input function
	cin.getline( p, 20 ); //or get input storing to the array
}

int main()
{
	char *p = new char [20];
	b( p );
	
	cout << p << endl ;

	return 0;
}
commented: thanks a lot,you are quite helpful +1

ok got that,thanks a lot,esp vmanes,you have made things a bit less complicated for me ...lol..
and

b( &p );

would pass the address of the pointer,it aint neccessary,coz the name of an array is the address of its first location!

b(p);

would work fine!.
thanks anyways!

Thank you Vmanes! I am still confused with passing pointers. And now looking at my code, I am wondering how on earth it compiled. :D I will look into this matter more closely, and thanks again.

Bevox,
What I think is happening in the code you posted, at line 7 you are setting pointer p to point to the text that exists in the function's code. If you walk through with a debugger, you'll see the value of pointer p (where it's pointing) is quite different from that of any other data elements you allocate in your program. And, when you return to main( ), if you try to change the content that p points to, as in assigning other string content via strcpy or cin, you'll get a runtime error.

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.