Like Tree1Likes
  • 1 Post By hk_mp5kpdw

Need help with my program (Struct/Arrays/Stack)

This is a discussion on Need help with my program (Struct/Arrays/Stack) within the C++ Programming forums, part of the General Programming Boards category; Hi guys, i really need help with my program so much that i had to register just to ask you ...

  1. #1
    Registered User
    Join Date
    Jul 2013
    Posts
    4

    Need help with my program (Struct/Arrays/Stack)

    Hi guys, i really need help with my program so much that i had to register just to ask you guys for any advice.

    So basically, my program lets the user input passengers (along with their name and number of baggage) into the plane. the plane can only hold up to 10 passengers. Then, if the user decides to "Take Off" it will continue to "claimBaggage()", The program should be able to display that the passengers have left and claimed their baggage at the counter. The thing is, the last passenger that the user had inputted must be the first one that goes and claim their baggage (Last In First Out).

    My program have problems, when i'm on the part where i display the passengers that goes out in a LIFO basis, the first passenger has no name and has 0 baggage. but after that the program works fine.

    Ex.
    Input:
    Passenger 1 = John, 5 baggage.
    Passenger 2 = Robert, 2 baggage.
    Passenger 3 = Carl, 1 baggage.

    Current Output:
    Passenger _, _ baggage.
    Passenger 3 = Carl, 1 baggage.
    Passenger 2 = Robert, 2 baggage

    Output should be:
    Passenger 3 = Carl, 1 baggage.
    Passenger 2 = Robert, 2 baggage.
    Passenger 1 = John, 5 baggage.


    Here is program code:

    Code:
    #include<iostream>
    #include<iomanip>
    #include<stdio.h>
    #include<conio.h>
    #include<windows.h>
    #define c cout
    
    
    using namespace std;
    
    
    
    
    struct PassengerInfo
    
    
        {
        char name[30];
        int baggageCount;
        };
    
    
    int num;
    PassengerInfo Passenger[10];
    
    void load();
    void takeOff();
    void displaySeats();
    void claimBaggage();
    
    void main()
    {
        int choice;
        do{
        system("cls");
        cout<<"         _                 "<<endl;  
        cout<<"       -=\\`\\             "<<endl;  
        cout<<"   |\\ ____\\_\\__         "<<endl;  
        cout<<" -=\\c`\"\"\"\"\"\"\" \"`) "<<endl;  
        cout<<"    `~~~~~/ /~~`\          "<<endl;  
        cout<<"      -==/ /               "<<endl;  
        cout<<"        '-'                "<<endl; 
        c<<"\n [1] Load Passengers";
        c<<"\n [2] Take Off";
        c<<"\n [3] Display airplane status";
        c<<"\n [4] Exit Program";
        c<<"\n\n Select Option: ";
        cin>>choice;
        c<<endl;
        switch(choice)
            {
                 
                case 1: load(); break;
                
                case 2: takeOff(); break;    
                        
                case 3: displaySeats(); break;
                
            };
        }
        while(choice!=4);
    }
    
    
    void load()
    {
        system("cls");
        c<<"\n You are now loading passengers inside the plane. \n\n";
        c<<"\n Occupied seats: "<<num;
        int bagCtr = 5;
    
    
        if(num != 10)
        {
                
                c<<"\n\n Enter of name of passenger: ";
                cin>>Passenger[num].name;
                
    
    
                do{
                        cout <<" Enter number of baggage: ";
                        cin>> Passenger[num].baggageCount;
    
    
                        //Greater than Baggage Limit
                        if(Passenger[num].baggageCount > bagCtr)
                        {
                            cout <<"\n Sorry, maximum of 5 baggages per person.\n\n";
                            system("pause");
                            
                        }
    
    
                        //Invalid Baggage Limit Input
                        if(Passenger[num].baggageCount < 0)
                        {
                            cout << "\n Invalid Input\n\n";
                            system("pause");
                            
                        }
    
    
                        //Satisfies the baggage limit
                        if(Passenger[num].baggageCount > 0 && Passenger[num].baggageCount < bagCtr)
                        {
                            //goes back to menu
                            break;
                        }
                }while(Passenger[num].baggageCount < 0 || Passenger[num].baggageCount > bagCtr);    
                num++;
                c<<"\n Passenger information successfuly added.\n\n";
                getch();
        }
        
        else
        {
            cout <<"\n\n Sorry, cannot add any more passengers. The airplane is full\n\n";
            system("pause");
        }
    
    
    }
    
    
    void takeOff()
    {
        int option = 0;
    
    
        //Amount of Passengers
        if( num <= 10 && num !=0)
        {
            system("cls");
            c<<"\n Are you sure you want to take off with this current amount of passengers?\n\n";
            c<<" [1]Yes [2]No\n\n";\
            c<<" Enter choice: ";
            cin >>option;
            //Option
            if(option == 1)
            {
                    system ("cls");
                    c << "\n Plane is ready to take off, please go to your respective seats.\n";
                    for (int i=0; i<3; i++)
                    {
                        c<< " ."<<endl;
                        Sleep(790);
                    }
                    system ("cls");
                    c << "\n We are now about to take off, please fasten your seat belts.\n";
                    for (int i=0; i<3; i++)
                    {
                        c<<" ."<<endl;
                            
                        Sleep(790);
                    }
                    system ("cls");
                    c << "\n The plane has landed, passengers may now claim their baggage at the counter.\n\n";
                    system("pause");
                    claimBaggage();
            }
    
    
            else
            {
                main();
            }
        }
    
    
        else
        {
            c <<" We cannot take off, you have no passengers in the plane\n\n";
            system("pause");
            main();
        }
    
    
    }
    
    
    void displaySeats()
    {
        system("cls");
        for (int ctr = 0; ctr < num; ctr++)
        {
            c<<"\n Seat No. "<<10-ctr;
            c<<"\n\n Name of passenger: "<<Passenger[ctr].name;
            c<<"\n Number of baggages: "<<Passenger[ctr].baggageCount;
            c<<endl<<endl;
        }
        system("pause");
    }
    
    
    void claimBaggage()
    {
        system("cls");
        c<<"\n We will now distribute the baggages of the passengers (LIFO Basis). \n\n";
        getch();
    
    
        for (int ctr = num; ctr > 0; ctr--)
        {
            system("cls");
            c<<"\nMr./Ms. "<<Passenger[ctr].name<<" had left with "<<Passenger[ctr].baggageCount<<" baggages. \n\n";
            system("pause");
        }
    
    
        memset(Passenger[num].name, 0, 10);
        
    
    
            
    }

    Here is the code where i display the passengers that left with their baggage:

    Code:
    void claimBaggage()
    {
        system("cls");
        c<<"\n We will now distribute the baggages of the passengers (LIFO Basis). \n\n";
        getch();
    
    
        for (int ctr = num; ctr > 0; ctr--)
        {
            system("cls");
            c<<"\nMr./Ms. "<<Passenger[ctr].name<<" had left with "<<Passenger[ctr].baggageCount<<" baggages. \n\n";
            system("pause");
        }
            
    }

    I also need to reset the array to 0, so when i can input Passengers again.

    Thanks in advance.
    Last edited by Amplirage; 07-09-2013 at 10:12 AM.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    The for loop in claimBaggage is starting one past the end of the passenger list (hence the empty entry) and doesn't continue until (and including) 0 (hence missing the first passenger). It should be like this:
    Code:
        for (int ctr = num - 1; ctr >= 0; ctr--)
    To "zero" the passenger name, you just need to put a zero byte in the first character.
    Code:
        Passenger[num].name[0] = '\0';
    And you should never call main (as you do in takeOff). Instead, you should simply return, possibly with an int value indicating an error.

    And main should return an int.

  3. #3
    Registered User
    Join Date
    Jul 2013
    Posts
    4
    Thank you for the reply oogabooga!

    I've already figured out the display problem, silly me (

    oh and thanks for the tip, so should i just make..

    Code:
    else
            {
                main();
            }
    
    to

    Code:
    else
            {
                return();
            }
    and about the setting the values to 0

    for the baggageCounter, should i use

    Code:
    Passenger[num].baggageCount = 0;
    ?
    because i need both the name and baggage count to reset.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    ooga already posted his response while I was writing/editing my missive but I'll post what I had at that point anyway even though it repeats a bit:

    #1
    Code:
    #define c cout
    
    ...
    
    c<<"\n [1] Load Passengers";
    c<<"\n [2] Take Off";
    c<<"\n [3] Display airplane status";
    c<<"\n [4] Exit Program";
    c<<"\n\n Select Option: ";
    Why???





    #2 Read: FAQ > What's the difference between... > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[])





    #3
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<windows.h>
    If you wanted to use the <stdio.h> header you should instead prefer the <cstdio> version with C++ programs. I suppose you have <conio.h> because of the getch calls? You can get rid of those and replace with cin.get() instead and then you don't need stdio/conio at all.

    The <windows.h> header is not required at all judging from the code provided.





    #4
    Code:
    void takeOff()
    {
        int option = 0;
     
     
        //Amount of Passengers
        if( num <= 10 && num !=0)
        {
    
            ...
    
            if(option == 1)
            {
                ...
            }
     
     
            else
            {
                main();
            }
        }
     
     
        else
        {
            c <<" We cannot take off, you have no passengers in the plane\n\n";
            system("pause");
            main();
        }
     
     
    }
    You should never call main yourself.




    #5
    Code:
    system("cls");
    The system function call requires the <cstdlib> header.




    #6 General:

    Avoid using globals where possible. You should be declaring your Passenger array in main and passing it to the various functions to perform the updates.

    Prefer the std::string class to character arrays.
    Elysia likes this.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Jul 2013
    Posts
    4
    Alright, thanks for all the advice. I have so many mistakes

    I used define c cout, because it shortens 'cout' to just 'c' lol.

    I also need the windows.h for the sleep()

    and i use getch(); because it acts like a system("pause"); for me and the difference between them is that getch(); doesn't display the "Press any key to continue message".

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,845
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Jul 2013
    Posts
    4
    Sorry, i thought it would be better if i post to 2 different sites for more responses.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,020
    That is all well and good if you did it unknowingly. But remember this: cross-posting (what you did) is generally frowned upon. So I would recommend that you simply choose one forum to continue your discussion on. If there isn't a reply in a reasonable amount of time, then you can start looking for another board to continue the discussion.
    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. Are C arrays stored in the stack or the heap?
    By butteflymentor in forum C Programming
    Replies: 17
    Last Post: 03-10-2012, 11:24 PM
  2. Struct Arrays
    By Jdo300 in forum C Programming
    Replies: 17
    Last Post: 03-09-2012, 03:16 PM
  3. arrays and struct !!
    By abood1190 in forum C Programming
    Replies: 9
    Last Post: 10-19-2011, 11:26 AM
  4. Accessing Struct Stack
    By 1999grandamse in forum C++ Programming
    Replies: 6
    Last Post: 10-16-2002, 08:05 PM
  5. Stack functions as arrays instead of node pointers
    By sballew in forum C Programming
    Replies: 8
    Last Post: 12-04-2001, 11:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21