Thread: I'm not sure where I went wrong with this - pls help

  1. #1
    Registered User Vireyda's Avatar
    Join Date
    Mar 2004
    Posts
    26

    Question I'm not sure where I went wrong with this - pls help

    Hello,

    I'm writing a simple program that will book seats on a flight but I've been struggling with this section of code for a few days now and decided to seek some help. whe I run this section, I get both pieces of output "That seat has already bee booked" and "Seat %d has been booked". I'm trying to prevent double booking a seat and while the program will not double book the seat, I still get these conflicting messages. Can anyone give me some suggestions to clear this up? I've posted the section of code below.

    Thanks,
    Vireyda

    Code:
    //begin function FirstClass
    void FirstClass(int L[])
    {
    	int seat, booked=0;
    
    	do
    	{
    	printf("Please enter the seat number to be booked => ");
    	fflush(stdin);
    	scanf("%d", &seat);
    	if(seat>5&&seat<1)
    		printf("\nThat is an invalid seat number. Please enter a seat number from 1 to 5.\n");
    		
    	if(L[seat-1]=1);
    		{
    		printf("\nThat seat has already been booked.\n");
    		booked=1;
    		}
    		
    	}while(seat>5&&seat<1);
    
    	while(seat>=1&&seat<=5&&booked==0);
    	{
    	L[seat-1]=1;
    	printf("Seat %d has been booked.\n", seat);
    	}
    }//end FirstClass

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    try changing assignment operator to the equal operator in the following line:

    if(L[seat-1]=1);

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > fflush(stdin);
    This is non-standard

    In addition to elad's point, you want to watch your use of ;
    if ( foo );
    while ( foo );
    These do nothing useful - indeed the second will cause the program to halt if the conditional expression never changes (as it does in your case)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I think Salem means that a semicolon immediately after the conditional of an if, while, or for statement will cause the body of the loop following the conditional to be ignored. For example the semicolon below means that do something will never get done.

    if(foo);
    {
    //do something;
    }

    Same here:

    while(foo);
    {
    //do something;
    }

    However, in do...while loops, the conditional comes after the body of the loop, so the semicolon is needed

    do
    {
    //do something
    }while(foo);

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Code:
    if(foo);
    {
    //do something;
    }
    Actually that something inside of the braces will always be done.


    Code:
    while(foo);
    {
    //do something;
    }
    And this will just throw ya into an infinite loop in most cases, and it will never even get to the opening brace.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    Registered User Vireyda's Avatar
    Join Date
    Mar 2004
    Posts
    26
    Thanks for your help. I got everything working but I was a little confused when "foo" was inserted between brackets. What does this mean?

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Sorry, foo is just a generic term that can stand in for any legal statement in the setting it is used. bar is another common generic term. As I understand it egend has it that foobar used to be a slang phrase that meant something like "who cares?" or "whatever" and it has found roots in programming as a generic fill in.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  2. What's wrong with my coding?
    By EeeK in forum C Programming
    Replies: 28
    Last Post: 10-09-2003, 05:32 AM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM