A simple program using ARRAYS, STRUCTS, and CLASSES

This is a discussion on A simple program using ARRAYS, STRUCTS, and CLASSES within the C++ Programming forums, part of the General Programming Boards category; Hi. I am a C++ Student in college, enrolled in a simple C++ programming course. In class, the lectures have ...

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

    Talking A simple program using ARRAYS, STRUCTS, and CLASSES

    Hi. I am a C++ Student in college, enrolled in a simple C++ programming course. In class, the lectures have been about Arrays, Structs and Classes. My professor assigned a program that (to me) is IMPOSSIBLE. I CANNOT FIGURE IT OUT. Before I describe this project, I DO NOT, repeat, DO NOT want a program written. I would, however, like a guideline that puts me in the right direction. As of now, I have no clue how to even begin. Any kind of input, big or small, (even a little encouragement) is GREATLY appreciated.

    Here is the project:

    Write an object-oriented program that can be used by a small theater to sell tickets for its performances. The auditorium has 15 rows of seats, with 30 seats in each row. (I would probably make a multi-dimensional array here, I'm assuming.. int seats [15][30].. ? ). Design and create class called Theater to encapsulate the functionality specified below.

    When the program begins, read the seat prices for each row from a file called seats.txt (here I'm guessing, I should do:

    Code:
       
            ifstream in_stream;
            in_stream.open(seats.txt);
            if (in_stream.fail()) //if the opening of the input file fails, the program exits
            {
                 cout <<  "Input file opening failed. \n";
                 exit (1);  
             }
             int x;
             in_stream >> x; //I have NO clue what i"m doing here actually. How do I 
                                          make an in_stream read from this text file?
    )

    Prices in this file will be stored as a values of type double, with once price per line and one price for each row. The price for Row 1 will be listed first, the price for Row 2 will be listed second, and so on. The prices can be stored in an array.

    After the prices have been read, the program should dispaly a seating chart similar to the one shown below. For example, the following image shows a chart depicting each seat in the theater. Seats that are available are represented by the # symbol, whereas those that are taken are represented by an * symbol.

    Finally, the program should enter a loop to display a menu system with options that allow the user to:
    1. Purchase tickets
    2. Display the total dollar value of all tickets sold
    3. Display the total number of tickets sold
    4. Display the number of seats available in each row
    5. Display the number of seats available in the entire auditorium.
    6. Display the current seating chart.
    7. Exist the system

    (Here I'm guessing I would use the SWITCH branching mechanism..)

    To purchase tickets, the user must enter the row and seat number of the desired seats. Do not accept row or seat numbers that do not exist. In addition, do not sell seats more than once: when the user requests a particular seat, make sure that seat is availabe before it is sold. The user will continue to entre row and seat numbers until a value of -1 is entered fro the row, indicating that he or she is finished purchasing tickets. (Here, I'm guessing I would search the array using the sequential search algorithm). The program should then display the total dolalr value of all tickets sold, including the tickets just purchased, and return to the main menu.

    Again, I know this project is a tough and long one, but ANY kind of input for a pathetic C++ beginner is GREATLY appreciatd

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,339
    If the seats were input through cin would you be able to read them? Using an ifstream is almost exactly the same.

    >> Prices in this file will be stored as a values of type double.
    You don't want to read into a variable of type int, which is what your example does with the variable x. If x was the proper type, then your read would properly read in a single price.

    >> (Here I'm guessing I would use the SWITCH branching mechanism..)
    Sounds like a good plan.

    I would start in small steps. Think about what kind of classes you will need, what operations they might need to perform and what data they should hold.

    Once you have an idea of your design, get some code working. Try and get code to work that reads in the data in the file and writes it back out again. Then update that code to store the data in an array and get that working, then if you've decided to store that information in one of your classes try and get that working. Then move on to the seating chart. Each step of the way pick one thing to work on and make sure the code compiles and runs and does what you expect before moving on to the next step.

  3. #3
    Registered User t3chn0n3rd's Avatar
    Join Date
    Dec 2007
    Location
    kansas city
    Posts
    25
    Code:
    ifstream in_stream;
            in_stream.open(seats.txt);
            if (in_stream.fail()) //if the opening of the input file fails, the program exits
            {
                 cout <<  "Input file opening failed. \n";
                 exit (1);  
             }
             int x;
             in_stream >> x;

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    4
    Quote Originally Posted by Daved View Post
    If the seats were input through cin would you be able to read them? Using an ifstream is almost exactly the same.
    What do you mean by that? If had changed my code to

    Code:
      double x;
      in_stream >> x;
    would it have the same affect as,

    Code:
          double x;
          cout << "Please enter the seating price" << endl;
         cin >> x;
    ???

    But if that is so, the set prices are read from seats.txt that has one price per line and one price for each row. How would make the compiler read from the text file and stored in an array. Furthermore, how can I make the compiler store the prices in an array? I didn't even know you could store information in an array without intializing the array first or asking the user for input.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by CStudentHelp View Post
    What do you mean by that? If had changed my code to

    Code:
      double x;
      in_stream >> x;
    would it have the same affect as,

    Code:
          double x;
          cout << "Please enter the seating price" << endl;
         cin >> x;
    ???
    Yes.
    I didn't even know you could store information in an array without intializing the array first or asking the user for input.
    Neither of those are even slightly necessary for getting data into an array.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,339
    The difference between in_stream >> x; and cin >> x; is that the first reads from the file and the second reads from the console.

    >> How would make the compiler read from the text file and stored in an array.
    You now have code to read in a single price and store it in a variable. Now you just have to set the appropriate value in your array to the value of that variable. You could also read directly into the array if you wanted to.

    First, create an array, then see if you can figure out how to get the data from the file to an array by one of those two methods. If you can't get it to work, post your best attempt here and explain how it's not working.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    4
    could you tell me what I am doing wrong here?

    Code:
    #include <iostream>
    #include <fstream> 
    
    using namespcae std;
    
    class Theater 
    {
    public:
    	void purchase_tickets();
    	void display_total_value_of_all_tickets_sold();
    
    
    int main ()
    {
    	ifstream ifs;
        ifs.open("seats.txt");
            if (ifs.fail()) //if the opening of the input file fails, the program exits
            {
                 cout <<  "Input file opening failed. \n";
                 exit (1);  
             }    
    	double x;
    	ifs >> x;
    
    	const int nrows = 15;
    	const int ncols = 30;
    	char theater[nrows][ncols];
    
    
    	for (row = 0; row < nrows; ++row)
    	{
    		for (seat = 0; seat < ncols; ++seat)
    	{
    		int one_seat;
    		ifs >> one_seat;
    		theatre[nrows][ncols] = one_seat;
    	}
    	}
    
    	
    	do
    	{
    	int choice;
    	cout << "1. Purchases tickets. " << endl
    		 << "2. Display the total dollar value of all tickets sold." << endl
    		 << "3. Display the total number of tickets sold." << endl
    		 << "4. Display the nubmer of seats available in each row." << endl
    		 << "5. Disply the number of seats available in the entire auditorium" << endl
    		 << "6. Display the current seating chart." << endl
    		 << "7. Exit the system." << endl;
    		 << "Make your selection and press Return:" << endl;
    	cin >> choice;
    	switch (choice)
    	{
    	case 1: 
    		int row, seat;
    		cout << "Entre row" << endl;
    		cin >> row;
    		cout << "Entre seat" << endl;
    		cin >> seat; 
    
    		break;
    	case 2: 
    		//insert code here
    		break;
    	case 3: 
    		//insert code here
    		break;
    	case 4:
    		//insert code here
    		break;
    	case 5:
    		//insert code here
    		break;
    	case 6:
    		//insert code here
    		break;
    	case 7:
    		cout << "End of Program. Good bye :) \n" << endl;
    		break;
    	default: 
    		cout << "Not a valid choice. \n"
    			 << "Choose again \n";
    	}
    	}while (choice != 7);
    
    	
    	return 0;
    }

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    4
    well besides the wrongly declared classes and not defining the void functions, what else is missing??

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You declare theater as an array of characters, but store integers into it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structs or classes?
    By legit in forum C++ Programming
    Replies: 4
    Last Post: 06-28-2009, 10:16 AM
  2. allocatable arrays in CLASSes and STRUCTS
    By simone.marras in forum C++ Programming
    Replies: 4
    Last Post: 03-14-2009, 10:50 AM
  3. classes, structs and unions
    By Luciferek in forum C++ Programming
    Replies: 24
    Last Post: 08-09-2008, 10:26 AM
  4. Why use Classes instead of Structs?
    By yaya in forum C++ Programming
    Replies: 12
    Last Post: 03-16-2008, 12:39 AM
  5. Are structs and classes the same thing?
    By dwks in forum C++ Programming
    Replies: 6
    Last Post: 11-25-2005, 02:21 PM

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