illegal structure operation

what is the correct way to populate array structure in c

Page 1 of 1

3 Replies - 10240 Views - Last Post: 01 April 2008 - 01:50 AM Rate Topic: -----

#1 lenoy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 04-March 08

illegal structure operation

Posted 31 March 2008 - 01:08 AM

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <conio.h>

#define MAX 50
#define SMOKING 4000
#define NON_SMOKING 3000



struct Room           //structure declaration
{
   char RoomType[20];
   int RoomNum;
   int status;
}room;

struct Guest
{
	char FName[20];
   char LName[20];
   int lenght_stay;
   int CreditCardNo[16];
}guest;

//room[MAX]={" ",0,0};     //array structure


char menu();
void code_Change();
void Reservation();
int RmStatus();

int main()
{
   char option,q;

  	printf("\t**********Welcome to SouthCoast Serenity Inn database.**********\n\n\n");

   option = menu();
   
   while(option!='q')
   {
   	switch(option)
   	{
   		case'r':
   		case'R':
   		Reservation();
   		break;

   		case'c':
   		case'C':
   		break;

         case'q':
         exit(0);
         break;
         
   		default:
     		break;
   	}
      option=menu();
   }
   getch();
	return 0;
}


char menu()
{

   char opt;

	printf("\tPlease select character that represents your choice.\n\n");
   printf("\tSelect  to make resevation\n\tSelect  to cancle reservation");
   printf("\n\tSelect (i) to check in \n\tSelect (o) for checking out\n\t");
   printf("Select (x) to exit");
   printf("\n\t");
   scanf("%c",opt);
	return opt;
}

/******************************************************************************
					make reservation
********************************************************************************/


void Reservation()
{
	int x =0;
   int chk;

   printf("\t\tRoom now reserved.......\n");
	printf("\t\tEnter selection displayed\n\n");
   printf("First name: \n" );
   gets(guest[x].FName);
   printf("Last name: \n" );
   gets(guest[x].LName);
   printf("Credit card number\n");
   gets(guest[x].CreditCardNo);
  /*	FILE *freserv;

   if((freserv = fopen("reservation.txt","w+"))== NULL)
   {
   	printf("Error opening file....");

   }
   */
   chk =RmStatus();

   if(chk == 0)
   	printf("Room Empty\n");
     	room[x].status=1;



   //for(int i=0;i<50;i++)
   //fclose(freserv);
   x++;
}

/******************************************************************************
							check room status
*******************************************************************************/
int RmStatus()
{
	int i = 0;
	while(i <MAX){
		if(room[i].status==0){
   		return 0;
   	}

   	else if(room[i].status==1){
   		printf("Room Reserved");
   		return 1;
   	}
   	else if(room[i].status==2){
   		printf("Room occupied\n");
   		return 2;
   	}
   i++;
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: illegal structure operation

#2 martin_bg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 20-March 08

Re: illegal structure operation

Posted 31 March 2008 - 03:37 AM

Hello!

First, why don't you try declaring the arrays:

Room rooms[MAX];
Guest guests[MAX];



or allocate memory for them:

Room *rooms = malloc(sizeof(Room)*MAX);
Guest *guests = malloc(sizeof(Guest)*MAX);



(Do not forget to free the memory at the end!)

Hope this is going to be helpful!

This post has been edited by martin_bg: 31 March 2008 - 03:54 AM

Was This Post Helpful? 0
  • +
  • -

#3 lenoy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 04-March 08

Re: illegal structure operation

Posted 31 March 2008 - 12:50 PM

//thanks alot, i have made some more changes but a getting error saying strucute required on left side of . or .*
//what does this mean?



#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <conio.h>

#define MAX 50
#define SMOKING 4000
#define NON_SMOKING 3000

char smoking,nonsmoking;



struct Room //structure declaration
{
char RoomType[20];
int RoomNum;
int status;
};

struct Guest
{
char FName[20];
char LName[20];
int lenght_stay;
int CreditCardNo[16];
};

struct Room room[MAX];
struct Guest guest[MAX];

//room[MAX]={" ",0,0}; //array structure


char menu();
void code_Change();
void Reservation();
int RmStatus(char);

int main()
{
char option,q;

printf("\t**********Welcome to SouthCoast Serenity Inn database.**********\n\n\n");

option = menu();

while(option!='q')
{
switch(option)
{
case'r':
case'R':
Reservation();
break;

case'c':
case'C':
break;

case'q':
exit(0);
break;

default:
break;
}
option=menu();
}
getch();
return 0;
}


char menu()
{

char opt;

printf("\tPlease select character that represents your choice.\n\n");
printf("\tSelect ® to make resevation\n\tSelect © to cancle reservation");
printf("\n\tSelect (i) to check in \n\tSelect (o) for checking out\n\t");
printf("Select (x) to exit");
printf("\n\t");
scanf("%c",opt);
return opt;
}

/******************************************************************************
make reservation
********************************************************************************/


void Reservation()
{
int x =0;
int chk;


printf("\t\tEnter selection displayed\n\n");
printf("First name: \n" );
gets(guest[x].FName);
printf("Last name: \n" );
gets(guest[x].LName);
printf("Credit card number\n");
scanf("%d",&guest[x].CreditCardNo);
printf("Prefered room type\n");
gets(room[x].RoomType);
/* FILE *freserv;

if((freserv = fopen("reservation.txt","w+"))== NULL)
{
printf("Error opening file....");

}
*/

chk =RmStatus(room.RoomType);
printf("\t\tRoom now reserved.......\n");
if(chk == 0)
printf("Room Empty\n");
room[x].status=1;



//for(int i=0;i<50;i++)
//fclose(freserv);
x++;
}

/******************************************************************************
check room status
*******************************************************************************/
int RmStatus(char type)
{
if(type == smoking){

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

if(room[i].status==0){
return 0;
//break;
}


if(room[i].status==1){
return 1;
//break;
}

if(room[i].status==2){
return 2;
// break;
}
}
}


if(type == nonsmoking){

for(int j=0;j>24;j++){

if(room[j].status==0){
return 0;
//break;
}

if(room[j].status==1){

return 1;
//break;
}
if(room[j].status==2){

return 2;
//break;
}
}
}


}
Was This Post Helpful? 0
  • +
  • -

#4 martin_bg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 20-March 08

Re: illegal structure operation

Posted 01 April 2008 - 01:50 AM

When you use pointers to access a struture the dot (".") operatior is no longer used. You sould use the arrow :) "->" e.g.
structPtr->field = smth;


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1