View Poll Results: What is the best way to get a character ???

Voters
7. You may not vote on this poll
  • ch = getche()

    0 0%
  • ch = getch()

    4 57.14%
  • scanf("%c", &char)

    2 28.57%
  • fscanf("%c", &char)

    1 14.29%

Thread: Problems with Arrays

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

    Angry Problems with Arrays

    Hi again ,

    So I have written the following module, but it locks the computer or shows error messages... I still can't understand what's the problem with this code...

    Here it is... (Should work under Borland C++ 3.1 for DOS/Win)
    And I can't use struct in this project !!!

    Code:
    void firstclass_select()
    { //void firstclass_select() begin
    
    	int w = 0;
            int x = 0;
    
    	printf("First Class Selected\n");
                       
    	plane_graph_first();
    
    	while(w == 0)
    	     { //while w for 0 begin
    		printf("Enter seat Number: ");
    		scanf("%d", &seat);
    
    		if((seat <= 0)||(seat > 16))
    		  printf("Out of range ! (1-16)");
    		else
    		  while(x == 0)
    		       { //while x for 0 begin
                             y = 1;
    			 if(*first[seat][y] == NULL)
    			   { //001 if condition begin
    			    printf("Enter Gender (M) or (F): ");
    
    			    fgets(*first[seat][y]);
    
                                strupr(*first[seat][y]);
    
    			    if((*first[seat][y] == 'M')||(*first[seat][y] == 'F'))
    			      { //002 if condition begin
                                    y++;
    
    				printf("Enter Name & Surname: ");
    
    				fgets(*first[seat][y]);
    
    				printf("Is this a (R)eservation or is this ticket (S)old ?");
    
                                    y++;
    
    				fgets(*first[seat][y];
    
                                    strupr(*first[seat][y];
    
    				if((*first[seat][y] == 'R')||(*first[seat][y] == 'S'))
    				  printf("\n\nPassenger saved in Array !!");
    				  w++;
                                      x++;
    
    				else
    				  printf("%s unknown !!", *first[seat][y];
    			      } //002 if condition end
    
    			    else
                                  printf("%s unknwon !!", *first[seat][y];
    
    			    } //001 if condition end
    
    			 else
    			   printf("Seats already taken !!");
                           } //while x for 0 end			        	
                           	
    	     } //while w for 0 end
    
    	getch();
    
    	clrscr();
    
    } //void firstclass_select() end
    Thanks in advance
    client

    Also have a look at http://www.cprogramming.com/cboard/s...threadid=18437
    Last edited by client; 05-27-2002 at 07:14 AM.

  2. #2
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    > what's the problem with this code...
    Well, there could be, or are, alot of things.
    Do you have anything in particular you would like us to look for or fix? Otherwise, there, I answered your question.

    > Thanks in advance
    Hey....anytime.
    The world is waiting. I must leave you now.

  3. #3
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    You are using scanf and fgets at the same time. This is wrong because scanf leaves the \n character in your input buffer.
    See this thread for more information.

    fgets(*first[seat][y]);
    That's strange, I always thought that fgets had 2 arguments!

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Monster

    fgets(*first[seat][y]);
    That's strange, I always thought that fgets had 2 arguments! [/B]
    I'll up the ante, and says it's got 3.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    The standard tells this about the function fgets(). I think this is correct.

    7.19.7.2 The fgets function

    Synopsis
    1 #include <stdio.h>
    char *fgets(char * restrict s, int n, FILE * restrict stream);

    Description
    2 The fgets function reads at most one less than the number of characters specified by n
    from the stream pointed to by stream into the array pointed to by s. No additional
    characters are read after a new-line character (which is retained) or after end-of-file. A
    null character is written immediately after the last character read into the array.

    Returns
    3 The fgets function returns s if successful. If end-of-file is encountered and no
    characters have been read into the array, the contents of the array remain unchanged and a
    null pointer is returned. If a read error occurs during the operation, the array contents are
    indeterminate and a null pointer is returned.
    Last edited by Shiro; 05-24-2002 at 01:53 PM.

  6. #6
    Registered User alpha561's Avatar
    Join Date
    May 2002
    Posts
    18
    Shiro: whereabouts can you get a copy of the ANSI C standard?
    alpha561

    "You don't want to sell me death sticks"

  7. #7
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    You can get it from the ANSI organisation at www.ansi.org. But note that the standard is not free. Thought it costs about $20. But the draft version is available on the net. Search for "C99 draft", "ansi c standard draft" or something like that.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    The last draft of C99 is free here
    http://std.dkuug.dk/jtc1/sc22/wg14/www/docs/n869/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problems with arrays
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 03-31-2009, 07:21 AM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. My Arrays!!!, My Arrays!!! Help!?
    By llcool_d2006 in forum C++ Programming
    Replies: 1
    Last Post: 12-10-2006, 12:07 PM
  4. Problems with Strings and Arrays.
    By SlyMaelstrom in forum C++ Programming
    Replies: 13
    Last Post: 04-15-2005, 02:13 PM
  5. Help!!! Problems with arrays.
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 05-08-2002, 08:21 PM