Thread: help plzzz...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    39

    help plzzz...

    hello everyone. well here is my program. when i compile and build there are no errors. but when i reach the line enter your name, i enter the name and when i press carriage return, the whole program displays until the end without asking me any of the questions i have placed in printf..

    Code:
    # include <stdio.h>
    # include <string.h>
    
    #define NULL 0
    
    void main()
    {
    	void seat();
    	void new_ticket();
    	void list();
    	void exit();
    	int choice;
    
    
    	printf("\n ````````````` SaHaRa Travles PVT. LTD ```````````\n");
    	printf("\n -------------------------------------------------");
    	printf ("\n 1. Availability of seats \n");
    	printf("\n 2. New ticket issue \n");
    	printf("\n 3. Passenger list for each flight \n");
    	printf("\n 4. Exit \n");
    	printf("\n --------------------------------------------------");
    	printf("\n Enter your choice:");
    	
    	choice = getchar();
    
    	switch (choice)
    	{
    	case '1':
    		seat();
    		break;
    	
    	case '2':
    		new_ticket();
    		break;
    
    	case '3':
    		list();
    		break;
    	case '4':
    		exit();
    		break;
    	default :
    	main();
    	}
    }
    
    void seat()
    {
    	printf("\n seat function incomplete.....");
    }
    
    void new_ticket()
    
    {
    	FILE *fpt;
    	char name,choice,seat,*dest;
    	int s,p_cost;
    	
    
    	dest="Honolulu";
    	fpt = fopen("Reservation.txt", "a+");
    
    	if (fpt==NULL)
    		printf("\n Error accessing file....");
    	else
    		printf("\n ------------------------- \n Proceeding to file creation and appending... \n");
    
    	for (s=0;s<51;++s)
    	{
    
    		printf("\n Destination:&#37;s \n",dest);
    		printf("\n Enter your Name:");
    		scanf("%s",&name);
    		printf("\n Please select the type of seat you wish to book for:  [Enter 'T' for tourist and 'F' for first class \n");
    		scanf("%c",&seat);
    
    		if (seat=='T')
    			p_cost=250;
    		else
    			p_cost=500;
    
    		printf("\n %d USD has been charged as your flight cost \n",p_cost);
    		printf("\n Your flight details has been logged in..... \n ");
    		fprintf(fpt,"%s %s %c %d",name,dest,seat,p_cost);
    		printf("\n Do you want to issue another ticket? Y or N");
    		scanf("%c",&choice);
    
    		if (choice=='Y')
    			continue;
    		else
    			break;
    	}
    
    	return 0;
    }

    a preview of what is displayed:
    http://img85.imageshack.us/my.php?image=errorqt5.jpg
    Last edited by shaheel; 02-22-2008 at 07:41 AM.

  2. #2
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Code:
    	char name,choice,seat,*dest;
    name can store a single one and only character.
    Code:
    		scanf("&#37;s",&name);
    And then if you read more than one character in one character bad things happen.
    Suggestions:
    Declare name as an array of characters with adequate size, like:
    Code:
    char name[256];
    Use scanf with a maximum characters to be read specifier, and no & when the parameter is an array, scanf will know the address to write because arrays are equivalent to the address of their first element, when used in pointer notation like scanf expects.
    Code:
    scanf("%255s", name);
    Also, move all the function prototypes out of main()
    Last edited by xuftugulus; 02-22-2008 at 08:05 AM.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >scanf("&#37;256s", name);
    You get a cookie. Well, half a cookie, because it should be %255s to make room for the null character.
    My best code is written with the delete key.

  4. #4
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Fixed that, thanks for the correction i was in kind of a rather noisy situation here.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    Omaha, Nebraska
    Posts
    116
    why do you have
    Code:
    void main()
    ???

  6. #6
    Registered User
    Join Date
    Aug 2007
    Posts
    39
    thanks for your help.. using array for the string of characters was good. well still then the following line is being omitted when i run the program:
    Code:
    printf("\n Please select the type of seat you wish to book for:");
    		scanf("%c",&seat);
    
    and 
    
    printf("\n Do you want to issue another ticket? Y or N");
    scanf("%c",&choice);
    Any suggestions???

    For the void main(), well as far as i read i have to use void main in C?? is this corrects?
    Last edited by shaheel; 02-22-2008 at 09:18 AM.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >well as far as i read i have to use void main in C?? is this corrects?
    No, the correct definition of main (without arguments) is:
    Code:
    int main ( void )
    {
      /* Your code here */
    
      return 0;
    }
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Aug 2007
    Posts
    39
    is this really for C programming? coz im supose to write a C program.. I know that in C++ i have to use int main() and the return 0,

  9. #9
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    It's a typical problem with scanf that a newline character is left from the Return key you pressed.
    The fastest solution is :
    Do a getchar() call, right after every scanf() you have and it should eat up the newline.
    is this really for C programming? coz im supose to write a C program.. I know that in C++ i have to use int main() and the return 0,
    And yes it is C programming and in fact not only C, that a program has and must return an exit status to the program's caller.
    Last edited by xuftugulus; 02-22-2008 at 09:35 AM.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  10. #10
    Registered User
    Join Date
    Aug 2007
    Posts
    39
    thnaks for ur suggestions...

  11. #11
    Registered User
    Join Date
    Oct 2006
    Location
    Omaha, Nebraska
    Posts
    116
    it's a common question, this might help explain it.
    http://www.cprogramming.com/faq/cgi-...wer=1044841143

    In short, the program (or more precisely main() ) has to return a signal, be it 0 or 1 (finished or not) which is an integer.

    If I'm wrong, some one, let me know.
    Last edited by MikeyIckey; 02-22-2008 at 09:51 AM. Reason: made it a little easier to understand i think

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, main can return any integer, but it must return something (except on some special embedded systems).
    The return value is up to the programmer to define.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need the code of few programs in c++.plzzzzz help...plzzz
    By NAVINKR20 in forum C++ Programming
    Replies: 1
    Last Post: 05-08-2009, 09:13 AM
  2. plzzz!!! answer this c++proggram on how to use do,while code?
    By randyjhon143 in forum C++ Programming
    Replies: 13
    Last Post: 03-12-2009, 04:32 PM
  3. Help With Time Plzzz!!
    By Dagger in forum C++ Programming
    Replies: 7
    Last Post: 02-01-2006, 10:46 PM