Thread: Something's not working..?

  1. #1
    Guest00
    Guest

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

  2. #2
    Guest00
    Guest
    missed out my header file:

    Code:
    #ifndef _custom_h_
    #define _custom_h_
    
    /* Function Prototypes */
    void Option1_Reserve(void);
    void Option2_Cancel(void);
    void Option3_Display(void);
    //void Option4_ViewList(void);
    int Option5_Quit(void);
    void ReserveSeat(int,char[25]);
    
    struct SmokingPassenger
    {
    	char name[25];
    };
    
    struct NonSmokingPassenger
    {
    	char name[25];
    };
    
    struct waitinglist
    {
    	char name[25];
    	char smoking[25];
    };
    
    #endif

  3. #3
    Guest00
    Guest
    bump... i still can't figure out whats wrong...

  4. #4
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Originally posted by Guest00
    bump... i still can't figure out whats wrong...
    A Lot...

  5. #5
    Guest00
    Guest
    if i don't protoype main, i get "error C2065: 'main' : undeclared identifier"
    (i'm doing multi-file btw)

    i need fflush(stdin) so the keyboard buffer doesn't get clogged up...

    changed all main(); to return; ... however doing this makes the program exit instead of returning to the main menu..

  6. #6
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    fflush(stdin) is wrong

    Use: while ( getchar() != '\n' );
    in stead of fflush(stdin);

    Even better: don't use the scanf function but use the fgets function to read from stdin.

  7. #7
    Guest00
    Guest
    oh ok.. didn't know that.. i'm still learning C.. my lecturer told us to use fflush(stdin); =\

  8. #8
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Code:
    for (ArrayNo=0;ArrayNo++;repeat=0)
    I think you mean:
    Code:
    for (ArrayNo=0; ArrayNo < 12 && repeat != 0; ArrayNo++)
    {
    ...
    }
    
    if(ArrayNo == 10)
    {
       /* all seats full */
    }
    And...
    Code:
    result=strcmp(Spassenger[ArrayNo].name,empty);
    Change to:
    Code:
    result=strcmp(Spassenger[ArrayNo].name, "");
    or
    Code:
    if(Spassenger[ArrayNo].name[0] == 0)

  9. #9
    Guest00
    Guest
    Wow... never would have realised those mistakes until they were too late... (actually i'm already very late.. i have to hand this project up in slightly under 14 hours !!! ...)

    thanks for taking the time to look through everything and helping me out.. really really appreciate it!!

  10. #10
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    No problem...

    b.t.w. you should make the ReserveSeat function return something.
    For example, return 0 if seat is successfully reserved and return -1 if all seats are taken.

  11. #11
    Guest00
    Guest
    why's that?
    i can do everything within the ReserveSeat function can't i ? or am i not seeing something here..

  12. #12
    Guest00
    Guest
    oh btw you haven't answered this..
    "changed all main(); to return; ... however doing this makes the program exit instead of returning to the main menu.."

  13. #13
    Guest00
    Guest

    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();
    }

  14. #14
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Do you mean, put the Option1_Reserve and ReserveSeat into 1 function? Yes you can do that. No, I don't think it's a good idea to do that. You can put it all in main if you want but it's better to divide your code in logical steps (parts) and inplement each step in a function.

    About the return... I'm not sure about that. Normally it should return to the calling function.

    What OS/compiler do you use?

  15. #15
    Guest00
    Guest
    fixed for() errors in option2_cancel

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Not working Right
    By raven420smoke in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2005, 03:21 AM
  2. Working on simple Engine
    By bladerunner627 in forum Game Programming
    Replies: 16
    Last Post: 05-25-2005, 11:27 AM
  3. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  4. x on upper right corner not working
    By caduardo21 in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2005, 08:35 PM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM