The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Page 2 -
Hotel Management Program
Page 2 - Discuss Hotel Management Program in the C Programming forum on Dev Shed. Hotel Management Program C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

March 13th, 2013, 11:11 PM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 22
Time spent in forums: 5 h 28 m 20 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by salem
I gave you a problem to solve - namely the chec = 0 ... chec == 1 issue.
|
Sir, thats not a problem at all. Because in the accounts function, where i declare the int chec, i set it to 1. Everytime.
And i want that function to run only and only if every1 has paid the payment.
Therefore i have written a code which sets the chec to 0, if even i single person has not paid.
The logic behind this, is that the payments of these members come on a monthly basis, and i dont know how to integrate database in C.(I am searching how to do that) but as a substitute, what i had done is, when every1 has paid the payment, lets say in June; then i pop up a message saying "All payments received" and in order to keep running the same prg, is set all the payment flags of every1 to 0. So that next month, say July it can take the payments
|

March 19th, 2013, 12:01 AM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 22
Time spent in forums: 5 h 28 m 20 sec
Reputation Power: 0
|
|
|
See page 2
|

March 19th, 2013, 02:12 PM
|
 |
Contributed User
|
|
|
|
> because, this should reset all the payment flags, to 0 when everyone has given the payment.
But you're not doing that are you.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef __GNUC__
// redefine all the conio rubbish
#define clrscr()
#define cprintf printf
#define getch getchar
char *strupr ( char *s ) {
//!! strupr() is a non-standard function, so I settled for
//!! just typing in the name correctly
return s;
}
#else
#include <conio.h>
#endif
struct person {
int pay;
char name[21];
int flag;
};
void accounts()
{
int ttt=0;
struct person payment;
char aname[21], oname[21];
char *namea;
int chec=1, ver=0;
long int recsize;
recsize=sizeof(payment);
FILE *f=fopen("HOTEL.DAT", "rb+");
if(f == NULL){
clrscr();
cprintf("File could not be opened!\n");
sleep(4);
exit(0);
}
clrscr();
cprintf("\n *** Pearl Guest House - Payments ***\n\n");
cprintf("\r\r\rEnter the Name :");
scanf("%[^\n]s", aname);
namea=strupr(aname);
strcpy(oname, namea);
while(fread(&payment, recsize, 1, f) == 1){
if((payment.flag == 1) && (strcmp(payment.name,oname) == 0)){
payment.pay=1;
printf("\n\n Payment Received\n");
getch();
ver=1;
fseek(f, -recsize, SEEK_CUR);
fwrite(&payment, sizeof(payment), 1, f);
fflush(f);
break;
} else {
printf("DEBUG1: Flag=%d, name=%s\n", payment.flag, payment.name);
}
}
if(ver!=1){
printf("\n\n Record not Found!!!\n");
getch();
}
rewind(f);
while(fread(&payment, recsize, 1, f) == 1){
if(payment.pay==0){
printf("DEBUG2: %s already paid\n", payment.name);
chec=0;
break;
} else {
printf("DEBUG2: still to pay=%d, name=%s\n", payment.pay, payment.name);
}
}
rewind(f);
if(chec==1){
while(fread(&payment, recsize, 1, f) == 1){
payment.pay=0;
printf("DEBUG3: %s has paid\n", payment.name);
fseek(f, -recsize, SEEK_CUR);
fwrite(&payment, recsize, 1, f);
ttt++;
printf("%d\n", ttt);
fflush(f); //new one
}
printf("\n\n All payments recieved...\n\nSo, the payments flags are set to 0\n");
getch();
} else {
printf("DEBUG4: chec is ZERO\n");
}
fclose(f);
}
int main ( ) {
struct person data[4] = {
{ 0, "Fred", 0 },
{ 0, "Barney", 1 }, // Only Barney has yet to pay
{ 0, "Wilma", 0 },
{ 0, "Betty", 0 },
};
FILE *fp = fopen("HOTEL.DAT","wb");
fwrite(data,sizeof(data),1,fp);
fclose(fp);
accounts();
return 0;
}
And some results
Code:
$ ./a.out
*** Pearl Guest House - Payments ***
Enter the Name :Goofy
DEBUG1: Flag=0, name=Fred
DEBUG1: Flag=1, name=Barney
DEBUG1: Flag=0, name=Wilma
DEBUG1: Flag=0, name=Betty
Record not Found!!!
DEBUG2: Fred already paid
DEBUG4: chec is ZERO
$ ./a.out
*** Pearl Guest House - Payments ***
Enter the Name :Wilma
DEBUG1: Flag=0, name=Fred
DEBUG1: Flag=1, name=Barney
DEBUG1: Flag=0, name=Wilma
DEBUG1: Flag=0, name=Betty
Record not Found!!!
DEBUG2: Fred already paid
DEBUG4: chec is ZERO
$ ./a.out
*** Pearl Guest House - Payments ***
Enter the Name :Barney
DEBUG1: Flag=0, name=Fred
Payment Received
DEBUG2: Fred already paid
DEBUG4: chec is ZERO
You see, it doesn't matter at all what you type in.
Your "All payments recieved...So, the payments flags are set to 0" is NEVER going to happen given this code.
You should check your use of the break; statement in some of your loops, if you're intent on checking ALL records, instead of bailing out at the first match.
|

March 22nd, 2013, 11:55 AM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 22
Time spent in forums: 5 h 28 m 20 sec
Reputation Power: 0
|
|
|
Hey sir; I am using turbo c++ compiler on a windows machine.
I couldnt understand many lines of your code like:
$ ./a.out
#ifdef __GNUC__
and the way u did file io.
Please help me.
Can you please explain this to me in detail.
maybe u can give me link of a page
Thank You in advance and for what u helped till now
|

March 22nd, 2013, 05:15 PM
|
 |
Contributed User
|
|
|
|
|
> I couldnt understand many lines of your code like:
> $ ./a.out
This runs the program on my machine - maybe yours is called prog.exe
> #ifdef __GNUC__
Well last time you complained that the code wouldn't work on your compiler.
So this time, I put the code which is specific to my environment inside conditional compilation tests (go read about them using a web search).
Since your compiler doesn't define __GNUC__, it means you get the
#else
#include <conio.h>
#endif
part of the code, which is what you need for your old compiler.
> and the way u did file io.
The way I did it IS the way you did it.
All I added were a bunch of printf("DEBUG") calls to show the program flow (something you should have done a couple of weeks ago).
|

March 22nd, 2013, 10:39 PM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 22
Time spent in forums: 5 h 28 m 20 sec
Reputation Power: 0
|
|
|
Sir if u pls could tell me which os and compiler u r using, it would be very helpful
Thanks
|

March 22nd, 2013, 11:54 PM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 22
Time spent in forums: 5 h 28 m 20 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by salem
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef __GNUC__
// redefine all the conio rubbish
#define clrscr()
#define cprintf printf
#define getch getchar
char *strupr ( char *s ) {
//!! strupr() is a non-standard function, so I settled for
//!! just typing in the name correctly
return s;
}
#else
#include <conio.h>
#endif
struct person {
int pay;
char name[21];
int flag;
};
void accounts()
{
int ttt=0;
struct person payment;
char aname[21], oname[21];
char *namea;
int chec=1, ver=0;
long int recsize;
recsize=sizeof(payment);
FILE *f=fopen("HOTEL.DAT", "rb+");
if(f == NULL){
clrscr();
cprintf("File could not be opened!\n");
sleep(4);
exit(0);
}
clrscr();
cprintf("\n *** Pearl Guest House - Payments ***\n\n");
cprintf("\r\r\rEnter the Name :");
scanf("%[^\n]s", aname);
namea=strupr(aname);
strcpy(oname, namea);
while(fread(&payment, recsize, 1, f) == 1){
if((payment.flag == 1) && (strcmp(payment.name,oname) == 0)){
payment.pay=1;
printf("\n\n Payment Received\n");
getch();
ver=1;
fseek(f, -recsize, SEEK_CUR);
fwrite(&payment, sizeof(payment), 1, f);
fflush(f);
break;
} else {
printf("DEBUG1: Flag=%d, name=%s\n", payment.flag, payment.name);
}
}
if(ver!=1){
printf("\n\n Record not Found!!!\n");
getch();
}
rewind(f);
while(fread(&payment, recsize, 1, f) == 1){
if(payment.pay==0){
printf("DEBUG2: %s already paid\n", payment.name);
chec=0;
break;
} else {
printf("DEBUG2: still to pay=%d, name=%s\n", payment.pay, payment.name);
}
}
rewind(f);
if(chec==1){
while(fread(&payment, recsize, 1, f) == 1){
payment.pay=0;
printf("DEBUG3: %s has paid\n", payment.name);
fseek(f, -recsize, SEEK_CUR);
fwrite(&payment, recsize, 1, f);
ttt++;
printf("%d\n", ttt);
fflush(f); //new one
}
printf("\n\n All payments recieved...\n\nSo, the payments flags are set to 0\n");
getch();
} else {
printf("DEBUG4: chec is ZERO\n");
}
fclose(f);
}
int main ( ) {
struct person data[4] = {
{ 0, "Fred", 0 },
{ 0, "Barney", 1 }, // Only Barney has yet to pay
{ 0, "Wilma", 0 },
{ 0, "Betty", 0 },
};
FILE *fp = fopen("HOTEL.DAT","wb");
fwrite(data,sizeof(data),1,fp);
fclose(fp);
accounts();
return 0;
}
|
Sir i tried to compile and run this code that you had given in my compiler. But it have a error in compilation in line no: 36;
the code on this line is:
Code:
FILE *f=fopen("HOTEL.DAT", "rb+");
//Sir, the above line is in the accounts function
It showed a message: "DECLARATION NOT ALLOWED HERE"
Please help
Thanks
|

March 23rd, 2013, 12:09 AM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
C does not allow declarations after executable code. C++ and C99 do, but not C.
Place all your declarations at the top of the function and follow them with the lines of code. So for that fopen, the FILE *f would go at the top of the function and the f=fopen() would come later.
Quote: | Originally Posted by Daksh Sir if u pls could tell me which os and compiler u r using, it would be very helpful |
I looked back through the thread and did not see where you had provided us with the same information that you now request. It would help us to help you if we knew what you were using.
By the syntax displayed, I would say that salem is using Linux and the GNU compiler, gcc, that comes with Linux.
|

March 23rd, 2013, 12:48 AM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 22
Time spent in forums: 5 h 28 m 20 sec
Reputation Power: 0
|
|
|
@dwise1_aol: I have mentioned that in post #19, anyways i am using a Windows 8 machine with turbo c++ compiler (using dosBox) and also tried by a TurboC++ Compiler on a Windows machine
|

March 23rd, 2013, 12:50 AM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 22
Time spent in forums: 5 h 28 m 20 sec
Reputation Power: 0
|
|
|
Hey salem can u please see my post #22 and reply to it
Thanks
|

March 23rd, 2013, 01:22 AM
|
 |
Contributed User
|
|
|
|
|
So apply a bit of thought (how did YOU declare f to begin with in your code - some global variable I guess).
Don't just simply copy/paste code - think about what you're doing.
Actually, I think I'm done with this thread, it's just getting too much like hard work.
|

March 23rd, 2013, 08:48 AM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 22
Time spent in forums: 5 h 28 m 20 sec
Reputation Power: 0
|
|
|
@salem: Sorry, please dont leave this thread; i will try it myself and then post only and only after working very hard
|

March 23rd, 2013, 10:20 AM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 22
Time spent in forums: 5 h 28 m 20 sec
Reputation Power: 0
|
|
Sir,
I studied the given code thoroughly; Now I have something that you know wasn't OK.
Just see this:
Code:
struct person {
int pay;
char name[21];
int flag;
};
In this; the flag was not used for payments, rather it was used for seeing whether a person is still present in the hotel or not. So there was no need of adding "int flag;"
In the main function: you have set the value of flag to 0; which means the person has left the hotel. So that is why accounts is giving problem.
Here is what i could get after deleting that part:
Code:
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
#include<dos.h>
FILE * f;
struct person{
char name[21];
int pay;
};
void main(){ //main function begins
//declaring all the functions
void accounts(); //add comments
void list();
void delrec();
void addrec();
void exit1();
char code1[11]="amitsirrox"; //password
char code[11];
int pos=0, cod, i;
textcolor(RED);
textbackground(BLACK);
clrscr();
for(cod=0; cod<=2;cod++){ //check loop begins
clrscr();
gotoxy(24,22);
cprintf("WELCOME TO PEARL GUEST HOUSE");
gotoxy(24,24);
gotoxy(24, 23);
fflush(stdin);
cprintf("\n\n Password :");
while(1){
char p=getch();
printf("*");
code[pos]=p;
pos++;
//When Enter is pressed....
if(p=='\r'){
pos=pos-1;
code[pos]='\0';
pos=0;
break;
}
}
if(strcmp(code,code1)==0)
break;
else if (!(strcmp(code,code1)==0)){
clrscr();
gotoxy(26,22);
printf("WRONG CODE\a");
gotoxy(26, 23);
if(cod==0)
printf("2 Tries Left");
else if(cod==1)
printf("1 Try Left");
else if(cod==2){
printf("No Tries Left");
sleep(3);
exit(0);
}
sleep(3);
continue;
}
}
textcolor(WHITE);
textbackground(BLUE);
clrscr();
while(1){
clrscr();
// textcolor(5);
printf("\n\t\t\t PEARL GUEST HOUSE SOFTWARE");
// textcolor(3);
printf("\n\n\n\n\t1. Accounts\n");
printf("\n\t2. Add Records\n");
printf("\n\t3. List Record\n");
printf("\n\t0. Exit\n");
printf("\n\n\tEnter your choice (Like - '1' for accounts) :");
scanf("%d", &i);
switch(i)
{
case 1:
accounts();
break;
case 2:
addrec();
break;
case 3:
list();
break;
case 0:
exit1();//fclose in this func.
}
}
}
void accounts()
{
int ttt=0;
struct person payment;
char aname[21], oname[21];
char *namea;
int chec=1, ver=0;
long int recsize;
recsize=sizeof(payment);
f=fopen("C:\\HOTEL.DAT", "rb+");
if(f == NULL){
clrscr();
cprintf("File could not be opened!");
sleep(4);
exit(0);
}
clrscr();
cprintf("\n *** Pearl Guest House - Payments ***\n\n");
cprintf("\r\r\rEnter the Name :");
fflush(stdin);
scanf("%[^\n]s", &aname);
namea=strupr(aname);
strcpy(oname, namea);
while(fread(&payment, recsize, 1, f) == 1){
if((strcmp(payment.name,oname) == 0)){
payment.pay=1;
printf("\n\n Payment Received");
fflush(stdin);
getch();
ver=1;
fseek(f, -recsize, SEEK_CUR);
fwrite(&payment, sizeof(payment), 1, f);
fflush(f);
break;
}
else{
printf("DEBUG1(No Match of Name): Name%s\n Payment:%d", payment.name, payment.pay);
}
}
if(ver!=1){
printf("\n\n Record not Found!!!");
fflush(stdin);
getch();
}
rewind(f);
while(fread(&payment, recsize, 1, f) == 1){
if(payment.pay==0){
printf("DEBUG2A: %s NOT Paid", payment.name);
chec=0;
break;
}
else{
printf("DEBUG2B: Paid=%d, Name=%s\n", payment.pay, payment.name);
}
}
rewind(f);
if(chec==1){
while(fread(&payment, recsize, 1, f) == 1){
payment.pay=0;
printf("DEBUG3: %s has paid\n", payment.name);
fseek(f, -recsize, SEEK_CUR);
fwrite(&payment, recsize, 1, f);
ttt++;
printf("%d", ttt);
fflush(f); //new one
}
printf("\n\n All payments recieved...\n\nSo, the payments flags are set to 0");
getch();
}
else{
printf("DEBUG4: Chec is ZERO\n");
}
fclose(f);
}
void list(){
struct person l;
int y = 5;
int temp=0; //temp variable to be deleted later
int i;
f=fopen("C:\\HOTEL.DAT", "rb");
if(f == NULL){
clrscr();
gotoxy(24,22);
cprintf("File could not be opened"); //not open message..
getch();
exit(0);
}
clrscr();
printf("\n How do you wanna list the records");
printf("\n\n 1. Records with the rent paid");
printf("\n\n 2. Records which didnt give the rent");
printf("\n\n 3. All records");
fflush(stdin);
scanf("%d", &i);
switch(i){
case 1:
clrscr();
textcolor(15);
cprintf("\n NAME PHONE NO. ROOM NO.\n\n");
textcolor(GREEN);
while(fread(&l, sizeof(l), 1, f) != 0){
if(l.pay==0)
continue;
gotoxy(5, ++y);
cprintf("%s", l.name);
temp++; //to be deleted later
}
textcolor(14);
cprintf("\n\n PRESS ANY KEY !");
cprintf("\r\n\nrecords read %d times", temp);
getch();
textcolor(WHITE);
clrscr();
textcolor(WHITE);
break;
case 2:
clrscr();
textcolor(15);
cprintf("\n NAME PHONE NO. ROOM NO.\n\n");
textcolor(GREEN);
while(fread(&l, sizeof(l), 1, f) != 0){
if(l.pay==1)
continue;
gotoxy(5, ++y);
cprintf("%s", l.name);
temp++; //to be deleted later
}
textcolor(14);
cprintf("\n\n PRESS ANY KEY !");
cprintf("\r\n\nrecords read %d times", temp);
getch();
textcolor(WHITE);
clrscr();
textcolor(WHITE);
break;
case 3:
clrscr();
textcolor(15);
cprintf("\n NAME PHONE NO. ROOM NO.\n\n");
textcolor(GREEN);
while(fread(&l, sizeof(l), 1, f) != 0){
gotoxy(5, ++y);
cprintf("%s", l.name);
temp++; //to be deleted later
}
textcolor(14);
cprintf("\n\n PRESS ANY KEY !");
cprintf("\r\n\nrecords read %d times", temp);
getch();
textcolor(WHITE);
clrscr();
textcolor(WHITE);
break;
}
fclose(f); //closing the file....
}
void addrec(){
char tname[21], tplace[21], tfather[21], taddr[100], tename[21], terel[11], toname[16];
struct date d;
int z;
struct person add;
char checkin[8];
char checkout[8];
char *add1, *place1, *father1, *addr1, *ename1, *erel1, *oname1;
int ci0, ci1, ci2, ci3, ci4, ci5, ci6, ci7;
char stat, acc;
int co0, co1, co2, co3, co4, co5, co6, co7;
// fseek(f, 0, SEEK_END);
f=fopen("C:\\HOTEL.DAT", "ab+");
if(f==NULL){
cprintf("File/Disk Corrupted!!!");
sleep(4);
exit(0);
}
getdate(&d);
memset(&add, '\0', sizeof(struct person));
clrscr();
textcolor(14);
clrscr();
textcolor(15);
cprintf("\n *** Pearl Guest House - Add Records ***\n\n");
textcolor(5);
printf("\n\nCURRENT DATE : %d-%d-%d ",d.da_day,d.da_mon,d.da_year);//displays current date;
textcolor(15);
printf("\n\n\n Tenant Form");
printf("\n\n Your Name :");
fflush(stdin);
scanf("%[^\n]s", &tname);
add1=strupr(tname);
strcpy(add.name,add1);
fflush(stdin);
while(1){
printf("\n\n Have you recieved the payment? (Y\\N)");
scanf("%c", &acc);
if(acc=='Y'||acc=='y'){
add.pay=1;
break;
}
else if(acc=='N'||acc=='n'){
add.pay=0;
break;
}
else{
printf("\n Enter a valid choice!!!");
}
}
fflush(f);
fwrite(&add, sizeof(add), 1, f);
clrscr();
gotoxy(30,12);
printf("\nAll Details Added Successfully!!!!\n\nPRESS ANY KEY");
getch();
if (fclose(f) != 0){ //closing the file....
clrscr();
gotoxy(30,12);
printf("\nFile not closed Successfully!!!");
}
printf("\nName :%s", add.name);
printf("\nPayment: %d", add.pay);
getch();
}
void exit1()
{
fclose(f);
exit(0);
}
Sir i have figured out where the problem is.
but i dont know what problem is there. The problem is in this while loop, it doesnt work for all the entries....!!!!
Code:
if(chec==1){
while(fread(&payment, recsize, 1, f) == 1){
payment.pay=0;
printf("DEBUG3: %s has paid\n", payment.name);
fseek(f, -recsize, SEEK_CUR);
fwrite(&payment, recsize, 1, f);
ttt++;
printf("%d", ttt);
fflush(f); //new one
}
printf("\n\n All payments recieved...\n\nSo, the payments flags are set to 0");
getch();
}
Please REPLY; Please
Daksh
Thanks
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|