Welcome to Dream.In.Code
Become a C++ Expert!

Join 136,912 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,728 people online right now. Registration is fast and FREE... Join Now!




illegal structure operation

 
Reply to this topicStart new topic

illegal structure operation, what is the correct way to populate array structure in c

lenoy
31 Mar, 2008 - 12:08 AM
Post #1

New D.I.C Head
*

Joined: 4 Mar, 2008
Posts: 11

cpp
#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++;
}
}

User is offlineProfile CardPM
+Quote Post

martin_bg
RE: Illegal Structure Operation
31 Mar, 2008 - 02:37 AM
Post #2

New D.I.C Head
*

Joined: 20 Mar, 2008
Posts: 15

Hello!

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

CODE

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


or allocate memory for them:

CODE

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 Mar, 2008 - 02:54 AM
User is offlineProfile CardPM
+Quote Post

lenoy
RE: Illegal Structure Operation
31 Mar, 2008 - 11:50 AM
Post #3

New D.I.C Head
*

Joined: 4 Mar, 2008
Posts: 11

//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;
}
}
}


}
User is offlineProfile CardPM
+Quote Post

martin_bg
RE: Illegal Structure Operation
1 Apr, 2008 - 12:50 AM
Post #4

New D.I.C Head
*

Joined: 20 Mar, 2008
Posts: 15

When you use pointers to access a struture the dot (".") operatior is no longer used. You sould use the arrow smile.gif "->" e.g.
CODE

structPtr->field = smth;

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/3/08 10:01PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month