Thread: Desperate for help - ugly nested if

  1. #1
    baseballkitten
    Guest

    Unhappy Desperate for help - ugly nested if

    I could use some help here... Yes, unfortunately it is homework, my C++ teacher thought it would be a treat to make us write a C lab for fun. Well C was years ago and I'm just stuck. I've tried this program about 5 different ways, have been working on it since last week and hate to bother anyone, but I'm desperate for some help. My extremely messy nested if statement seems to be causing the problem. It seems necessary to me though. The program compiles with no warnings and no errors, but doesn't run. Any assistance would be greatly appreciated. Thank you so much.

    Heather.


    #include <stdio.h>
    #include <conio.h>

    int main (void)

    {

    /* local definitions */

    int seatNum1 = 0;
    int seatNum2 = 0;
    int seatNum3 = 0;
    int seatNum4 = 0;
    int seatNum5 = 0;
    int seatNum6 = 0;

    int answer = 0;
    char answer2 = 'n';

    char lastName [10];
    char firstName [10];


    /* list seat numbers */

    printf("Welcome to Wheezer Airless... where smoking is encouraged!\n");
    printf("Seats on aircraft are 1, 2, 3, 4, 5, and 6.\n");
    printf("First class seats are seats 1 and 2.\n\n");

    /* add passenger */

    printf ("Would you like coach or first class? (1 for first 2 for coach)\n");

    fflush(stdin);
    scanf("%d", &answer);

    if (answer = 'c')
    {
    if (seatNum3 == 1, seatNum4 == 1, seatNum5 == 1, seatNum6 == 1)
    printf("No seats are available!");
    else if (seatNum3 = 1, seatNum4 = 1, seatNum5 = 1, seatNum6 = 0)
    {
    printf("Please enter last name:\n");
    scanf("%c", lastName);
    printf("Please enter first name:\n");
    scanf("%c", firstName);
    printf("%c %c your seat number is 6. \n", firstName, lastName);
    seatNum6 = 1;
    }
    else if (seatNum3 = 1, seatNum4 = 1, seatNum5 = 0)
    {
    printf("Please enter last name:\n");
    scanf("%c", lastName);
    printf("Please enter first name:\n");
    scanf("%c", firstName);
    printf("%c %c your seat number is 6. \n", firstName, lastName);
    seatNum5 = 1;
    }
    else if (seatNum3 = 1, seatNum4 = 0)
    {
    printf("Please enter last name:\n");
    scanf("%c", lastName);
    printf("Please enter first name:\n");
    scanf("%c", firstName);
    printf("%c %c your seat number is 6. \n", firstName, lastName);
    seatNum4 = 1;
    }
    else if (seatNum3 = 0)
    {
    printf("Please enter last name:\n");
    scanf("%c", lastName);
    printf("Please enter first name:\n");
    scanf("%c", firstName);
    printf("%c %c your seat number is 6. \n", firstName, lastName);
    seatNum3 = 1;
    }
    }
    else if (answer = 'f')
    {
    if (seatNum1 = 1, seatNum2 = 1)
    {
    printf("No first class available, would you like coach? (y or n)\n");
    scanf("%c", answer2);
    }
    if (answer2 = 'y')
    if (seatNum3 = 1, seatNum4 = 1, seatNum5 = 1, seatNum6 = 1)
    printf("No seats are available!");
    else if (seatNum3 = 1, seatNum4 = 1, seatNum5 = 1, seatNum6 = 0)
    {
    printf("Please enter last name:\n");
    scanf("%c", lastName);
    printf("Please enter first name:\n");
    scanf("%c", firstName);
    printf("%c %c your seat number is 6. \n", firstName, lastName);
    seatNum6 = 1;
    }
    else if (seatNum3 = 1, seatNum4 = 1, seatNum5 = 0)
    {
    printf("Please enter last name:\n");
    scanf("%c", lastName);
    printf("Please enter first name:\n");
    scanf("%c", firstName);
    printf("%c %c your seat number is 6. \n", firstName, lastName);
    seatNum5 = 1;
    }
    else if (seatNum3 = 1, seatNum4 = 0)
    {
    printf("Please enter last name:\n");
    scanf("%c", lastName);
    printf("Please enter first name:\n");
    scanf("%c", firstName);
    printf("%c %c your seat number is 6. \n", firstName, lastName);
    seatNum4 = 1;
    }
    else if (seatNum3 = 0)
    {
    printf("Please enter last name:\n");
    scanf("%c", lastName);
    printf("Please enter first name:\n");
    scanf("%c", firstName);
    printf("%c %c your seat number is 6. \n", firstName, lastName);
    seatNum3 = 1;
    }
    else if (answer2 = 'n')
    printf("Please try another airline, thank you.\n");
    }
    else if (seatNum1 = 1, seatNum2 = 0)
    {
    printf("Please enter last name:\n");
    scanf("%c", lastName);
    printf("Please enter first name:\n");
    scanf("%c", firstName);
    printf("%c %c your seat number is 6. \n", firstName, lastName);
    seatNum2 = 1;
    }
    else if (seatNum1 = 1, seatNum2 = 0)
    {
    printf("Please enter last name:\n");
    scanf("%c", lastName);
    printf("Please enter first name:\n");
    scanf("%c", firstName);
    printf("%c %c your seat number is 6. \n", firstName, lastName);
    seatNum1 = 1;
    }
    else
    return 0;

    printf("Would you like to exit?");


    return 0;
    }

    Program Specs: (if curious) =)

    Our airplane holds 6 passengers... 2 in First Class... 4 in Coach class.. As Passengers arrive at the gate the system should take and record the last name and assign a seat number (1 thru 6 ) 1 and 2 being First Class.

    People requesting Coach get a Coach seat. If Coach is full, then they are out of luck and must go elsewhere.

    People requesting First Class get a First Class seat unless First Class is full in which case they are offered Coach unless Coach is also full in which case they too, must go elsewhere.

    The program should start by offering a menu of choices to select.


    WHEEZERS AIRLESS

    1. List all seat assignments

    2. Add a passenger to Coach

    3. Add a passenger to First Class

    4. Exit this program

    SELECTION->




    After performing each function the program returns to the menu until Exit option is selected.



    DELIVERABLES: Including e-mailed labs

    Fully documented listing of this and all future programs by doing the following:

    1. Place a program abstract at the top of the program (class, or function not belonging to any class). This should tersely describe the item's purpose, input, output, and method/algorithm(s) used.

    2. Put a comment to the right of each variable (or related group of variables) tersely describing its purpose.

    3. Use meaningful identifiers (names for variables, constants, classes, functions, etc.).

    4. Put a comment above each group of logically-related statements that serve a well-defined purpose so as to make that purpose clear.

    5. Place a comment at the top of your listing giving your name, course, and section.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well you didn't seem to learn much it must be said, because there are a whole raft of errors

    int seatNum1 = 0;
    int seatNum2 = 0;
    int seatNum3 = 0;
    int seatNum4 = 0;
    int seatNum5 = 0;
    int seatNum6 = 0;
    Like, why not use an array - or better yet, two arrays
    int first_class[2];
    int num_in_first = 0;
    int coach_class[4];
    int num_in_coach = 0;

    > fflush(stdin);
    This doesn't work in all environments -
    Use this
    Code:
    while ( getchar() != '\n' ) continue;
    > scanf("%d", &answer);
    From your later use of answer, this should be %c

    > if (answer = 'c')
    Huh - you must know the difference between = and == in comparisons, surely
    This is no different in C++

    > if (seatNum3 == 1, seatNum4 == 1, seatNum5 == 1, seatNum6 == 1)
    Oh man, come on - you know what && and || do, don't you?
    Again, this is no different in C++
    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.

  3. #3
    baseballkitten
    Guest
    I compiled it and I had no errors, it just wouldn't run correctly.

    As far the error in the char vs int, I know there are some things that need worked out, but my nested if is just ugly and I believe that is the root of my problem.

    while ( getchar() != '\n' ) continue;
    -this statement confuses me... should I use this instead of the fflush statement.

    Thanks for pointing out that they may not have taught me all that well =)

    I'm not saying I'm an expert, but I really am trying here... I would like to have enough expereince to help someone out someday also, but it's going to take some time and trial and error before that happens...but I am trying...

  4. #4
    mr.messy
    Guest

    Lightbulb

    I made this, it does the job. You can add validation yourself
    ---------------------------------------------------------------------------

    #include <stdio.h>

    void disp_menu(void); //display the menu
    void list_seats(void); //list the names in seat spaces
    void add_coach(void); //add a 1st class passenger
    void add_first(void); //add a coach class passenger

    char passngr_name[6][13]={"empty", "empty", "empty", "empty", "empty", "empty"};
    int passngr_info[6]={0,0,0,0,0,0};
    int main(void)
    {
    int user_input;

    disp_menu();
    scanf("%d",&user_input);
    while (user_input != 4)
    {
    switch(user_input)
    {
    case 1:list_seats();
    break;
    case 2:add_coach();
    break;
    case 3:add_first();
    break;
    }
    disp_menu();
    scanf("%d", &user_input);
    }

    printf("Done.\n");
    return 0;
    }

    //=======================================

    void disp_menu(void)
    {
    printf("\n\n");
    printf(" -------------------\n");
    printf(" | Wheezer Airless |\n");
    printf(" -------------------\n\n");
    printf("1__List seating arrangement\n");
    printf("2__Add coach class passenger\n");
    printf("3__Add first class passenger\n");
    printf("4__Exit program\n\n");
    printf("Selection ->");
    }

    //=======================================

    void list_seats(void)
    {
    int i;

    for (i=0; i<6; i++)
    printf("seat..%d..%s\n", i+1, passngr_name[i]);
    }

    //=======================================

    void add_coach(void)
    {
    int i, free=0;

    for (i=2; i<6; i++)
    {
    if (passngr_info[i]==0)
    {
    free=1;
    break;
    }
    }
    if (free==1)
    {
    printf("Enter surname>");
    scanf("%s", passngr_name[i]);
    passngr_info[i] = 1;
    }
    else
    printf("Sorry all coach class seats are full\n");
    }

    //=======================================

    void add_first(void)
    {
    int i, free=0;
    char alt_seat;

    for (i=0; i<2; i++)
    {
    if (passngr_info[i]==0)
    {
    free=1;
    break;
    }
    }
    if (free==1)
    {
    printf("Enter surname>");
    scanf("%s", passngr_name[i]);
    passngr_info[i] = 1;
    }
    else
    {
    printf("Sorry all first class seats are full\n");
    for (i=2; i<6; i++)
    if (passngr_info[i]==0)
    free=1;
    if (free==1)
    {
    printf("Would you like to book a coach class seat?[y/n]>");
    scanf(" %c", &alt_seat);
    if (alt_seat == 'y')
    add_coach();
    }
    else
    printf("All coach class seats are full also\n");
    }
    }
    //=======================================

  5. #5
    baseballkitten
    Guest
    Thank you so much Mr. Messy! You were ever so helpful!!! I really appreciate it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Nested array vs. tree
    By KONI in forum Tech Board
    Replies: 1
    Last Post: 06-07-2007, 04:43 AM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  3. nested switch issue
    By fsu_altek in forum C Programming
    Replies: 3
    Last Post: 02-15-2006, 10:29 PM
  4. Displaying Data from a Nested Structure
    By shazg2000 in forum C++ Programming
    Replies: 1
    Last Post: 01-09-2005, 10:16 AM
  5. Nested Classes
    By manofsteel972 in forum C++ Programming
    Replies: 4
    Last Post: 11-21-2004, 11:57 AM