View Single Post
Old 10-02-2005, 09:42 AM   #14 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
Since I'm not here to do your homework, then I'm reluctant to provide you with the answer to your request..
But anyway I made it.. Now your challenge is to explain from line 15 to 55 exactly what is going on.
Code:
#include <iostream>
#include <vector>

int main()
{
  unsigned int no_floors=0, temp=0, counter = 0;
  std::vector <int> no_rooms, no_op_rooms;
  while(no_floors < 1)
  {
    std::cout << "Enter number of floors in building: ";
    std::cin >> no_floors;
    if(no_floors < 1)
       std::cout << "Error please enter 1 (ONE) or more floors" << std::endl;
  }
  for(int i=1; i <= no_floors; ++i)
  {
    if(i == 13)
      continue; /* skip 13th floor */
    /* bad programming style to use gotos
     * but if we know what we're doing.... 
     */
    start_rooms:
    std::cout << "Enter number of rooms on " << i << ". floor: ";
    std::cin >> temp;
    if(temp < 10)
    {
      std::cout << "Error you should give 10 or more rooms pr. floor" << std::endl;
      goto start_rooms;
    }
    no_rooms.push_back(temp);
  start_op_rooms:
    std::cout << "Enter number of occupied rooms on " << i << ". floor: ";
    std::cin >> temp;
    if(i > 13)
      counter = i-2;
    else
      counter = i-1;
    if(no_rooms[counter] < temp)
      {
	std::cout << "Error there can not be more occupied rooms" << std::endl;
	std::cout << "than the floor provides, which is " << no_rooms[counter] << std::endl;
	goto start_op_rooms;
      }
    no_op_rooms.push_back(temp);
  }
  std::cout << "For the hotel, the room statistics are:" << std::endl;
  std::cout << "Floor:\tRooms:\tOccupied:\tFree:\tPercentage:" << std::endl;
  for(int i=0; i < no_rooms.size(); ++i)
    {
      std::cout << "  " << i+1 << ")\t  " << no_rooms[i] /* number of rooms */
		<< "\t  " << no_op_rooms[i]           /* occupied rooms */
		<< "\t\t  " << (no_rooms[i] - no_op_rooms[i])/* free rooms */
		<< "\t  " << static_cast<double>(100.0*no_op_rooms[i]/no_rooms[i])
		<< std::endl;
    }
  return 0;
}
If you wan't the output to look nice and shiny, use your own time to fiddle with it.. I didn't waste 8 years on my PhD to learn how to use set_with(), set_fill() and set_precision() to mangle with the output.

If you're observant, you'll notice a small error in the program, if you can locate it and solve it, then i havn't wasted my time trying to teach you the basics in this assignment.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote