Something's not working..?
i've narrowed the problem down to the strcpy part.. when trying this seperately, changing if(result==0) to if(result<0) fixes the problem, but why so? and even if i change it ti if(result<0) it doesn't work in my program..
here's the function that calls the problem function:
Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include "custom.h"
extern struct SmokingPassenger Spassenger[12];
extern struct NonSmokingPassenger NSpassenger[6];
extern struct waitinglist waiting_list[9];
void main(void);
// Start of Option1_Reserve
void Option1_Reserve(void)
{
extern int ArrayNo;
extern int WLArrayNo;
extern int smoking_space;
extern int nonsmoking_space;
char name[25];
char choice_smoking;
char smoking_alt;
char temp[30];
int quit=0;
int loop=0;
// Enter name
system("cls");
printf("Please enter your name: ");
fflush(stdin);
scanf("%s", &name);
strupr(name);
// Smoking or non-smoking?
do {
printf("\nWould you prefer the (s)moking or the (n)on-smoking section?: ");
fflush(stdin);
scanf("%c", &choice_smoking);
choice_smoking=toupper(choice_smoking);
if (choice_smoking=='S')
{
if (smoking_space>0)
{
system("cls");
printf("An available seat in the smoking section has been allocated to you.\n");
printf("Your ticket has been printed out. Please take it.");
smoking_space--;
ReserveSeat(1,name);
printf("%s", &name);
getch();
main();
}
else if (nonsmoking_space>0)
{
system("cls");
printf("Sorry, there is no space in the smoking section.\n");
printf("However, there are seats in the non-smoking section, would you like to reserve a seat there? Y/N: ");
fflush(stdin);
scanf("%s", &smoking_alt);
smoking_alt=toupper(smoking_alt);
do {
loop=0;
if (smoking_alt=='Y')
{
system("cls");
printf("An available seat in the non-smoking section has been allocated to you.\n");
printf("Your ticket has been printed out. Please take it.");
nonsmoking_space--;
ReserveSeat(2,name);
getch();
main();
}
else if (smoking_alt=='N')
{
system("cls");
printf("Sorry, the flight is full. You will be put in the waiting list.");
strcpy(waiting_list[WLArrayNo].name,name);
strcpy(temp,"smoking");
strcpy(waiting_list[WLArrayNo].smoking,temp);
WLArrayNo++;
getch();
main();
}
else
{
printf("Please enter Y or N.");
}
} while (loop==0);
main();
}
else
{
printf("Sorry, the flight is full. You will be put in the waiting list.");
strcpy(waiting_list[WLArrayNo].name,name);
strcpy(temp,"smoking");
strcpy(waiting_list[WLArrayNo].smoking,temp);
WLArrayNo++;
getch();
}
quit=1;
}
else if (choice_smoking=='N')
{
if (nonsmoking_space>0)
{
system("cls");
printf("An available seat in the non-smoking section has been allocated to you.\n");
printf("Your ticket has been printed out. Please take it.");
nonsmoking_space--;
ReserveSeat(2,name);
getch();
main();
}
else if (nonsmoking_space>0)
{
system("cls");
printf("Sorry, there is no space in the non-smoking section.\n");
printf("However, there are seats in the smoking section, would you like to reserve a seat there? Y/N: ");
fflush(stdin);
scanf("%s", &smoking_alt);
smoking_alt=toupper(smoking_alt);
do {
loop=0;
if (smoking_alt=='Y')
{
system("cls");
printf("An available seat in the smoking section has been allocated to you.\n");
printf("Your ticket has been printed out. Please take it.");
smoking_space--;
ReserveSeat(1,name);
getch();
main();
}
else if (smoking_alt=='N')
{
system("cls");
printf("Sorry, the flight is full. You will be put in the waiting list.");
strcpy(waiting_list[WLArrayNo].name,name);
strcpy(temp,"smoking");
strcpy(waiting_list[WLArrayNo].smoking,temp);
WLArrayNo++;
getch();
main();
}
else
{
printf("Please enter Y or N.");
}
} while (loop==0);
main();
}
else
{
printf("Sorry, the flight is full. You will be put in the waiting list.");
printf("\nWould you prefer the (s)moking or the (n)on-smoking section?: ");
fflush(stdin);
scanf("%c", &temp);
strcpy(waiting_list[WLArrayNo].name,name);
strcpy(waiting_list[WLArrayNo].smoking,temp);
WLArrayNo++;
getch();
main();
}
quit=1;
}
else
{
printf("Please enter S for smoking or N for non-smoking.");
quit=0;
}
} while (quit==0);
}
and here's the problem function:
Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include "custom.h"
extern struct SmokingPassenger Spassenger[12];
extern struct NonSmokingPassenger NSpassenger[6];
void ReserveSeat(int temp, char name[25])
{
int ArrayNo=0;
int repeat=1;
char empty[25];
int result;
if (temp=1)
{
for (ArrayNo=0;ArrayNo++;repeat=0)
{
result=strcmp(Spassenger[ArrayNo].name,empty);
if (result==0)
{
strcpy(Spassenger[ArrayNo].name,name);
repeat=0;
}
}
}
else
{
for (ArrayNo=0;ArrayNo++;repeat=0)
{
result=strcmp(NSpassenger[ArrayNo].name,empty);
if (result==0)
{
strcpy(NSpassenger[ArrayNo].name,name);
repeat=0;
}
}
}
}
the other parts of my project..
here are the other parts of my project.. if anyone would be kind enough to look through them..
these aren't really complete yet..
Option2_Cancel
Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include "custom.h"
extern struct SmokingPassenger Spassenger[12];
extern struct NonSmokingPassenger NSpassenger[6];
extern struct waitinglist waiting_list[9];
void main(void);
// Start of Option2_Cancel
void Option2_Cancel(void)
{
int Option;
int ArrayNo=0;
int ArrayNo2=0;
int repeat=1;
int repeat2=1;
int result;
char name[25];
char smoking[25]="smoking";
char nonsmoking[25]="non-smoking";
printf("Cancel Reservation\n");
printf("(1) Cancel Reserved Seat\n");
printf("(2) Cancel Queue in Waiting List\n");
printf("(3) Return to previous screen\n");
printf("Choose an option: ");
fflush(stdin);
scanf("%d", &Option);
system("cls");
if (Option==1)
{
printf("Please enter your name: ");
scanf("%s", &name);
strupr(name);
for (ArrayNo=0;ArrayNo++;repeat=0)
{
result=strcmp(Spassenger[ArrayNo].name,name);
if (result==0)
{
strcpy(Spassenger[ArrayNo].name,"");
for (ArrayNo2=0;ArrayNo2++;repeat2=0)
{
result=strcmp(waiting_list[ArrayNo2].smoking,smoking);
if (result==0)
{
strcpy(Spassenger[ArrayNo].name,waiting_list[ArrayNo2].name);
printf("\n\nThe free seat will be given to %s. The ticket has been printed out.", waiting_list[ArrayNo2].name);
repeat2=0;
}
else repeat2=0;
}
repeat=0;
}
}
for (ArrayNo=0;ArrayNo++;repeat=0)
{
result=strcmp(NSpassenger[ArrayNo].name,name);
if (result==0)
{
strcpy(NSpassenger[ArrayNo].name,"");
for (ArrayNo2=0;ArrayNo2++;repeat2=0)
{
result=strcmp(waiting_list[ArrayNo2].smoking,nonsmoking);
if (result==0)
{
strcpy(NSpassenger[ArrayNo].name,waiting_list[ArrayNo2].name);
printf("\n\nThe free seat will be given to %s. The ticket has been printed out.", waiting_list[ArrayNo2].name);
repeat2=0;
}
else repeat2=0;
}
repeat=0;
}
}
printf("Reservation Deleted.");
}
if (Option==2)
{
printf("Please enter your name: ");
scanf("%s", &name);
strupr(name);
for (ArrayNo=0;ArrayNo++;repeat=0)
{
result=strcmp(Spassenger[ArrayNo].name,name);
if (result==0)
{
strcpy(Spassenger[ArrayNo].name,"");
repeat=0;
}
}
for (ArrayNo=0;ArrayNo++;repeat=0)
{
result=strcmp(NSpassenger[ArrayNo].name,name);
if (result==0)
{
strcpy(NSpassenger[ArrayNo].name,"");
repeat=0;
}
}
printf("Reservation Deleted.");
}
}
Option3_Display
Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include "custom.h"
extern struct SmokingPassenger Spassenger[12];
extern struct NonSmokingPassenger NSpassenger[6];
extern struct waitinglist waiting_list[9];
void main(void);
// Start of Option3_Display
void Option3_Display(void)
{
system("cls");
printf("Non Smoking Area:\n");
printf("\t\tSeat1\t\tSeat2\t\tSeat3\n\n");
printf("Row 1:\t\t( %s )\t\t( %s )\t\t( %s )\n\n", NSpassenger[0].name, NSpassenger[1].name, NSpassenger[2].name);
printf("Row 2:\t\t( %s )\t\t( %s )\t\t( %s )\n\n", NSpassenger[3].name, NSpassenger[4].name, NSpassenger[5].name);
printf("Row 3:\t\t( %s )\t\t( %s )\t\t( %s )\n\n", NSpassenger[6].name, NSpassenger[7].name, NSpassenger[8].name);
printf("Row 4:\t\t( %s )\t\t( %s )\t\t( %s )\n\n", NSpassenger[9].name, NSpassenger[10].name, NSpassenger[11].name);
printf("Smoking Area:\n");
printf("\t\tSeat1\t\tSeat2\t\tSeat3\n\n");
printf("Row 1:\t\t( %s )\t\t( %s )\t\t( %s )\n\n", Spassenger[0].name, Spassenger[1].name, Spassenger[2].name);
printf("Row 2:\t\t( %s )\t\t( %s )\t\t( %s )\n\n", Spassenger[3].name, Spassenger[4].name, Spassenger[5].name);
getch();
main();
}