looping help

This is a discussion on looping help within the C++ Programming forums, part of the General Programming Boards category; I'm having a little trouble with this code. I've been assigned to make an Airline Reservations System script. Here's what ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    4

    looping help

    I'm having a little trouble with this code. I've been assigned to make an Airline Reservations System script. Here's what I have to do:

    There's a 10 element array, and that represents the seating chart on the plane. All elements are set to 0. When a person takes a seat, that element is substituted with a 1 to declare that the seat has been filled. There are two seating sections: First Class and Economy. A person can decide what section they want their seat in. If First Class has been completely filled, then the program asks if it should assign a seat to the Economy. If yes, then a seat in Economy gets filled. If no, a message is displayed.

    Here's what I have so far:

    Code:
    #include <iostream.h>
    #include <windows.h>
    
    int main (int argc, char *argv[])
    {
    	// Define the input values
    	string economy_input;
    	int input;
    	int counter = 0;
    	
    
    	// Define the arrays
    	int firstclass[5] = {0, 0, 0, 0, 0};
    	int economy[5]	 = {0, 0, 0, 0, 0};
    
    	while (counter != -1)
    	{
    		for (int i = 0; i < sizeof(firstclass); i++)
    		{
    		cout << "Please type in 1 for \"First Class\" or 2 for \"Economy\"" << endl;
    		cin >> input;
    
    		if (input == 1) // "First Class" has been entered
    		{
    			if (firstclass[i] == 0 && firstclass[i] <= sizeof(firstclass)) // Checks to make sure that there is a spot left in the array
    			{
    				firstclass[i] = 1; // Fill in the element
    				cout << "Assigning the person's seat to: " << i << " in the first class section." << endl;
    			}
    			else
    			{
    				cout << "All of the first class spaces have been filled. Would you like to go to economy? Yes or No" << endl;
    				getline(cin,economy_input);
    
    				if (economy_input == "No")
    				{
    					cout << "Next flight leaves in 3 hours." << endl;
    				}
    				if (economy_input == "Yes")
    				{
    					if (economy[i] == 0 && economy[i] <= sizeof(economy))
    					{
    						economy[i] = 1;
    						cout << "Assigning the person's seat to: " << i << endl;
    					}
    				}
    			}
    		}
    
    		
    		if (input == 2) // "Economy" has been entered
    		{
    			for (int i = 0; i < sizeof(economy); i++)
    			{
    			if (economy[i] == 0 && economy[i] <= sizeof(economy))
    			{
    				economy[i] = 1;
    				cout << "Assigning the person's seat to: " << i << " in the economy section." << endl;
    			}
    			}
    		}
    	}
    
    	return 0;
    }
    I'm not sure how the for loops and while loop would work for this script. I don't even know if there are suppose to be for loops/while loops. If someone could help me fix this up, it would be much appreciated.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,449
    #include <iostream.h>
    this header file is outdated - you should use more recent compiler

    firstclass[i] <= sizeof(firstclass)
    obviosly wrong

    i < sizeof(firstclass)
    wrong as well - on another reason
    sizeof(firstclass) = 5* sizeof (int) which is 20 on most compilers

    and yes - your for loops are messed up

    just think what wil happen if the user selects 5 economy class sits first... He will not be able to buy firstclass at all
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Also, what do you need <windows.h> for? I don't see any Windows API calls in there.

    Since you're using getline() you should #include <string>

    And since you don't explicitly qualify things like std::cout std::string or std::getline you need to add using namespace std; after your #include directives.

    Lastly, you also have some parts of the code that aren't indented properly, which could make it harder to read.

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    4
    Thanks for the suggestions. I've modified the script a bit.

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main (int argc, char *argv[])
    {
    	string economy_input;
    	int input;
    	
    	int seats[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    	int size = sizeof(seats) / sizeof(int);
    
    	for (int i = 0; i < size; i++)
    	{
    		cout << "Please type in 1 for \"First Class\" or 2 for \"Economy\"" << endl;
    		cin >> input;
    
    		if (input == 1)
    		{
    			if (seats[i] == 0)
    			{
    				seats[i] = 1;
    				cout << "Assigning the person's seat to: " << i << " in the first class section." << endl;
    			}
    			else
    			{
    				cout << "All of the first class spaces have been filled. Would you like to go to economy? Yes or No" << endl;
    				getline(cin,economy_input);
    
    				if (economy_input == "No")
    				{
    					cout << "Next flight leaves in 3 hours." << endl;
    				}
    			}
    		}
    		
    		if (input == 2)
    		{
    			seats[i] = 1;
    			cout << "Assigning the person's seat to: " << i << " in the economy section." << endl;
    		}
    	}
    
    	return 0;
    }
    I changed it to a single array since the problem is asking for a single-subscripted array.

    My problem is that I don't know how to mark the elements to 1. First Class is seats 1-5 and Economy is 6-10. So if a person types in 2, then element 6 in the array should be set to 1. That's the part that I don't know how to do. The same would go with First Class.

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,449
    I suppose you better go with 2 counters

    Code:
    int economyIndex = 5;
    int firstClassIndex = 0;
    and some while loop like

    Code:
    while(firstClassIndex <5 || economyIndex <10)
    {
       // there are some places left
       //ask sit type
       if(Firstclass)
       {
           if(firstClassIndex == 5)
           {
              //no sits left - suggest economy
           }
           else
           {
               //take place firstClassIndex and increase the index
           }
        }
        else
        {
              //economy class
              if(economyIndex <10)
              {
                  //take place economyIndex and increase the index
              }
              else
              {
                    //no economy class sits left - warn and continue
              }
        }
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. wierd looping effect after exporting 3ds to .x annimation
    By Anddos in forum Game Programming
    Replies: 3
    Last Post: 01-06-2009, 12:43 PM
  2. problems with prototype function looping
    By dezz101 in forum C Programming
    Replies: 5
    Last Post: 04-29-2008, 06:03 AM
  3. looping went berserk
    By miryellis in forum C Programming
    Replies: 7
    Last Post: 09-21-2004, 01:59 PM
  4. Looping questions
    By Peyote in forum C++ Programming
    Replies: 3
    Last Post: 09-15-2003, 11:01 PM
  5. looping and input
    By Kinasz in forum C Programming
    Replies: 2
    Last Post: 03-17-2003, 06:12 AM

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