Thread: Fixed my bugs... but have still problems with arrays

  1. #1
    Registered User client's Avatar
    Join Date
    May 2002
    Posts
    12

    Angry Fixed my bugs... but have still problems with arrays

    O.K. ! Thanks to all who explained me how to use fgets... Now I made some bugfixes in my code... this is the result !!!

    Code:
    void firstclass_select()
    { //void firstclass_select() begin
    
            int v = 0;
    	int w = 0;
    	int x = 0;
    	int y = 0;
    	int z = 0;
            char select;
    
    	printf("First Class Selected\n");
                       
    	plane_graph_first();
    
    	while(w == 0)
    	     { //while for w 0 begin
    		for(seat = 1; seat <= 16; seat++)
    		   {
    			for(a = 1; a <= 1; a++)
    			   {
    				if(*first[seat][a] == NULL)
    				   empty++;
    
    				else
    				   taken++;
    			   }
    		   }
    
    
    		if(taken == 16)
    		  {
    			printf("All seats taken in First Class, press any key to continue...");
    			getch();
    			w++;
    		  }
    
    		else
    		  printf("\n\nThere are %d empty seats in First Class\n", empty);
    
    		  printf("Enter seat Number: ");
    		  scanf("%d", &seat);                  
    
    		  if((seat <= 0)||(seat > 16))
    		    printf("Out of range ! (1-16)");
    
    		  else
    		    while(y == 0)
    		         { //while for y begin
    				if(*first[seat][a] == NULL)
    			  	  { //if array is NULL begin
    					while(x == 0)
    					     { //while x for 0 begin
    						printf("Please Enter Gender of Passenger (M) or (F): ");
                                                    a = 1;
    						gets(first[seat][a]);
                                                    gets(first[seat][a]);
    
                                                    strupr(first[seat][a]);
    
    						if((first[seat][a] == "M")||(first[seat][a] == "F"));
    						  { //if array is M or F begin
    							printf("Please Enter Name of Passenger: ");
    
                                                            a++;
    
    							gets(first[seat][a]);
    
    							while(v == 0)
    							     { //while v for 0 begin
    								printf("Is this a (R)eservation or is this ticket (S)old ?: ");
    
                                                                    a++;
    
    								gets(first[seat][a]);
    
                                                                    strupr(first[seat][a]);
    
    								if((first[seat][a] == "R")||(first[seat][a] == "S"))
    								  { //if array is R or S begin
    									printf("Name of Passenger: %s\n", *first[seat][2]);
    									printf("Gender: %s  |  Ticket Type: %s\n", *first[seat][1], *first[seat][3]);
    									printf("Passenger Succesful Saved !!");
                                                                            getch();
    									v++;
    									x++;
    									y++;
    									z++;
    								  } //while array is R or S end
    
    								else
    								  printf("R or S !!!");
    
    							     } //while v for 0 end
                                                      } //if array is M or F end
    
    						else //Why misplaced else ???
    						  printf("M or F !!!");
                                                } //while x for 0 end
    			          } //if array is NULL end
    
    				else
                              	  { //else begin
    			  		printf("Seat no. %d is taken", seat);
    			  		getch();
    			  		w++;
    					y++;
    			  	  } //else end
                           	 }// while for y end
    	     } //while for w 0 end
    
    	getch();
    
    	clrscr();
    
    } //void firstclass_select() end
    So to my problems...

    1. Misplaced else ??? in the code... But I still can't figure out why it is a misplaced else (look in the code //misplaced else ??)

    2. Actually I tried to check if the user inputs 'M' or 'F' & 'R' & 'S', but it did not work out as good as I thought.

    3. When I enter the Gender, Name and R/S thing there, I'll get as output ".l" what does that mean ???

    4. Any comments regarding this code are welcome in case of improvement etc... bugifxes...

    Thanks in advance
    client

  2. #2
    Registered User client's Avatar
    Join Date
    May 2002
    Posts
    12

    Unhappy ... in addition to the text above...

    I removed the * (asteriks) from the strings in the output but this time not ".l" was displayed "(null)" was displayed ???

    Thanks in advance
    client

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >for (a = 1; a <= 1; a++)
    This is a pointless loop, you'll only ever go round once. You don't need the loop, just execute the statements within it.

    >scanf("%d", &seat);
    You should check the return code of scanf() to ensure you read an int correctly.

    >gets(first[seat][a]);
    Don't use gets, use fgets() or similar. I think you mentioned you changed this?

    >misplaced else
    This is so because you have an extra semi-colon on the if statement that is supposed to match it:
    >if ((first[seat][a] == "M") || (first[seat][a] == "F"));

    >first[seat][a] == "M"
    You cannot test with "M", use 'M'.

    >General style comment: there are too many indentation points, it makes the code hard to follow. For example:
    Code:
    if ((seat <= 0) || (seat > 16))
    	printf("Out of range ! (1-16)");
    else
    	.....
    You could change this slightly to be something like this
    Code:
    if ((seat <= 0) || (seat > 16))
    {
    	printf("Out of range ! (1-16)");
    	return;
    }
    ......
    this way the rest of the function hasn't indented another level.

    There may be some other bits, but it's hard to tell as I can't compile it without creating all you variables which are missing.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User client's Avatar
    Join Date
    May 2002
    Posts
    12

    Question Thanks Hammer, but...

    Thanks for your help, but I have the following problems...

    When I change
    Code:
    if((first[seat][a] == "M")||(first[seat][a] == "F"))
    to
    Code:
    if((first[seat][a] == 'M')||(first[seat][a] == 'F'))
    Following error message appears:
    Compiling ..\CPP\ATS.CPP:
    Error ..\CPP\ATS.CPP 179: Cannot convert 'char' to 'char *'
    Error ..\CPP\ATS.CPP 179: Cannot convert 'char' to 'char *'



    I tried to use fgets, I tried it like this
    Code:
    fgets(first[seat][a], 1);
    The following Error message appeared:
    Compiling ..\CPP\ATS.CPP:
    Error ..\CPP\ATS.CPP 175: Too few parameters in call to 'fgets(char *,int,FILE *)'


    So I attached my code (in CPP Format) so that you can see what it is all about... (Excuse me that I saved it with the C++ extension and not as *.C !)

    Thanks, lots of thanks...
    client
    Last edited by client; 05-26-2002 at 06:23 AM.

  5. #5
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    So I attached my code (in CPP Format) so that you can see what it is all about...
    It should be all about C.
    C Board > C Programming > Fixed my bugs... but have still problems with arrays
    The world is waiting. I must leave you now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with arrays
    By steffi in forum C Programming
    Replies: 1
    Last Post: 11-16-2007, 04:37 AM
  2. Im a newbie having problems with dynamic arrays, can anyone help?
    By Dr.Spankenstein in forum C Programming
    Replies: 4
    Last Post: 04-16-2007, 04:54 AM
  3. Help with arrays and pointers please...
    By crazyeyesz28 in forum C++ Programming
    Replies: 8
    Last Post: 03-17-2005, 01:48 PM
  4. Problems with Arrays
    By client in forum C Programming
    Replies: 7
    Last Post: 05-27-2002, 11:11 AM
  5. Help!!! Problems with arrays.
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 05-08-2002, 08:21 PM