I am new in C++ and trying to do some exercise to be familiar with the language, here I try to implement a program that accepts seat reservation and cancellations for single performance. There are 50 seats (0 to 49)
The program should loop, each loop performing the following steps:
- Prompt the user to type either 'a' or 'c' to add or cancel a reservation.
- Read keyboard input. If neither 'a' nor 'c' is entered, prompt again.
- If 'a' is entered, request the number of seats required. If enough seats are still available, identify the seat(s) to be reserved. Always use the lowest numbered seats available. Print the numbers of the seats allocated along with a reservation number, so that the reservation can be identified later for the purposes of cancellation. If there are not enough seats, print an error message.
- If 'c' is entered, request the reservation number and locate the seats reserved under that reservation number. Mark the seats as available again. If the reservation is not found, print an error message.
Here is what I have done so far but - even my print e.g. cout is marking an error? I am not sure if I should use array or const here, which is more appropriate or can I use #define? - Can anyone assist me please.
Thanks.
#include "stdafx.h"
//The seats has 50 seats numbered from 0 to 49
#define MAX_SIZE 49
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
char ac;
int seatReq;
int seatRes;
int resNum;
do
{
do {
//This prompt the user to type either a (add) c (cancle) the reservation.
cout <<"Please type either 'a' for add or 'c' for cancel"<
cin>> ac;
}
//If neither 'a' or 'c' is entered prompt again
while (ac !='A' && ac != 'a' && ac !='C' && ac != 'c'); //Keep asking till get valid answer
//If 'a' entered request the number of seats required
if (ac == 'a' || ac == 'A') {
cout << "How many seats are required?" <
cout <<"You are requried "<
//Use the lowest numbered seated avaiable
//Print the numbers of the seats allocated along with a reservation number so that the reservation can be indentified later for the
//purposes of cancellatoin.
cout <<"Your seats are available and your seats are reseved at "<
else
//If there are not enough seat, print an error message
cout<<"Sorry your seats are not available"<
//If 'c' is entered, request the reservation number and locate the seats reserved under that reservation number.
//Mark the seats as available again.
//If the reservation if not found, print an error message
if (ac == 'C' ||ac == 'c') {
cout <<"Please entered the reservation number"<
cout <<"Your cancellation reservation number is "<
else
cout<<"The reservation number"<
//If valid asnwer repeat again
while (ac =='a' || ac == 'A' || ac == 'c' || ac == 'C');
//When a reservation added, first check whether the seats can be reserved as a single contiguous block.
return 0;
}
Sam HobbsPosted Dec 9, 2011, 2:11 PM
Please start a new thread for this. This is a new question.
Please look at each place that Visual Studio indicates that there is an error. Look to see if there is a problem with the "case" used. Sometimes you use lowercase (such as "scales") when you also use uppercase (such as "Scales").
Also look at the documentation of how to write an overload for operators. The following shows the details.
If you have any questions about that, then please start a new thread. I will not reply to this thread about this program. I do have more things to say to fix other problems but I want you to create a new thread for this.
siabanie baniePosted Dec 9, 2011, 10:50 AM
I wonder if you can clear and help me out on this matters:
Basically I need to create class scale and translate where these two classes are inheritance from transition class.
In the scale class contain e.g
Public:
scale();
~scale draw();
operator >>;
same as translate.
And the transition class contains:
protected:
vector3D vec;
Here is what I have done but does not seem right:
How can I create these classes ? Can you please assist me on this? Thanks.
Karthik AgarwalPosted Nov 18, 2011, 2:12 AM
But it's better write your code using functions as the length of the code gets bigger bcoz it will be too typical to understand everything in main function. Anyways siabanie banie don't worry you let get to know things as you keep on practicing.
----------------------------------------------------------------------------------------------------------------------------------------
Mark the post as "Accepted Answer" if it helps you.
Sam HobbsPosted Nov 17, 2011, 10:45 PM
I think it is good to use functions like that to organize your programs. You must do that for larger programs so it helps to become accustomed to doing it now.
Note that the switch statement does not have much value here but it is commonly used in situations such as this and if the program were to use more than two things (add and cancel) then it is easier to add more when the program uses a switch like that.
siabanie baniePosted Nov 17, 2011, 7:45 PM
Sam HobbsPosted Nov 17, 2011, 11:51 AM
Karthik AgarwalPosted Nov 17, 2011, 4:55 AM
Karthik AgarwalPosted Nov 17, 2011, 4:51 AM
include "stdafx.h"
#include
using namespace std;
#define MAX_SEATS 50
static void add_reservation(int no_of_seats_res);
static void cancel_reservation(int no_of_seats_res);
int _tmain(int argc, _TCHAR* argv[])
{
char enter_option;
int seatReq = 0;
int no_of_seats_res = 0;
while(1)
{
cout<< "Please enter 'a' for adding, 'c' for cancelling or 'z' for availability : " << endl;
cin>> enter_option;
if(enter_option == 'a' || enter_option == 'A')
{
cout<<"How many seats are required : " << endl;
cin>> seatReq;
if((seatReq <= (MAX_SEATS - no_of_seats_res)) && (no_of_seats_res <= MAX_SEATS))
{
no_of_seats_res = no_of_seats_res + seatReq;
cout<<"You required : "<< seatReq <<"seat/s\nThe seat/s has been reserved" << endl;
}
else
{
cout<<"Sorry, there is not enough seats, try again? " << endl;
}
}
else if(enter_option == 'c' || enter_option == 'C')
{
cout<<"What is the reservation number?" << endl;
cin>> seatReq;
no_of_seats_res = no_of_seats_res - seatReq;
if(seatReq <= no_of_seats_res)
{
cout<<"Seat/s are cancelled" <
else
{
cout<<"Sorry, the reservation number is not valid, try again?" << endl;
}
}
else if(enter_option == 'z' || enter_option == 'Z')
{
cout<<"Available seats are : " << (MAX_SEATS - no_of_seats_res) << endl;
}
else
{
cout<< "Please enter coorect choice: " << endl;
}
}
return 0;
}
Sam HobbsPosted Nov 17, 2011, 4:30 AM
Sam HobbsPosted Nov 17, 2011, 3:57 AM
using namespace std;
#define MAX_SIZE 49
char Prompt() {
char c;
//This prompts the user to type either a (add) or c (cancel) the reservation.
cout << "Please type either 'a' for add or 'c' for cancel" << endl;
cin >> c;
return tolower(c);
}
void add_reservation() {
int seatReq;
cout << "How many seats are required?" <
cout << "You are requried " << seatReq << " of seat" << endl;
//If enough seats are still available, identify the seat(s) to be reserved
//Use the lowest numbered seated avaiable
//Print the numbers of the seats allocated along with a reservation number so that the reservation can be indentified later for the
//purposes of cancellatoin.
cout <<"Your seats are available and your seats are reseved at " << seatReq << endl;
}
void cancel_reservation() {
int resNum;
cout <<"Please entered the reservation number"<
cout <<"Your cancellation reservation number is "<
int _tmain(int argc, _TCHAR* argv[])
{
char ac;
bool More = true;
while (More) {
ac = Prompt();
switch (ac) {
case 'a':
add_reservation();
break;
case 'c':
cancel_reservation();
break;
default:
cout << ac << " is invalid\n";
More = false;
}
}
return 0;
}
Karthik AgarwalPosted Nov 17, 2011, 3:39 AM
//
#include "stdafx.h"
#include
using namespace std;
static void add_reservation(char enter_option);
static void cancel_reservation(char enter_option);
int _tmain(int argc, _TCHAR* argv[])
{
//declare variable
char enter_option;
cout<< "Please enter your option 'a'for adding 'c' for cancelling : " << endl;
cin>> enter_option;
//What would happens if user enter different than a and c?
//Prompt again till valid input entered.
while (enter_option !='A' && enter_option!= 'a' && enter_option!='C' && enter_option!= 'c')
{
//call add reservation
add_reservation(enter_option);
//call cancellation function
cancel_reservation(enter_option);
}
return 0;
}
static void add_reservation(char enter_option)
{
int seatReq;
//if 'a' is entered request the num of seats required
if(enter_option == 'a')
cout<<"How many seats are required : " << endl;
cin>> seatReq;
//if enough seats still available , idenfity the seat to be reserved
//Alywas do the lowest numbered seats available
if (seatReq <= 49)
//Print the numbers of the seats allocated along with
//a reservation number so the reservation can be identified
//later for purposes of cancellation.
cout<<"You required"<< seatReq <<"The seat has been reserved : " << endl;
else
{
//If there are not enough seats, print an error message
cout<<"Sorry, there is not enough seats, try again? " << endl;
}
}
static void cancel_reservation(char enter_option)
{
int seatReq;
//If c is entered, request the reservation number and locate the seats reserved under that reservation number
if(enter_option == 'c')
cout<<"What is the reservation number?" << endl;
cin>> seatReq;
//Mark the seats as available again
//If the reservation is not found, print error message
if (seatReq != seatReq)
cout<<"Sorry, the reservation number is not valid, try again?" << endl;
}
This one should get compiled, i tried it in my PC it got compiled. For any other issues get back to me.
Karthik AgarwalPosted Nov 17, 2011, 3:19 AM
Sam HobbsPosted Nov 17, 2011, 2:50 AM
If you do not understand the program when you make separate functions for adding and canceling reservations then don't do it that way. If you don't do that, however, then you still need to provide the clarification I requested.
Regardless of which version you use, you need to tell us more about what the problem is that you are encountering.
siabanie baniePosted Nov 16, 2011, 7:26 PM
Kathrik,
I have put the #include
I have tried to use the add and cancel function but I am not exactly sure if I did is correct. The code seems getting bit confusing than before..Can you please assist me.
#include "stdafx.h"
#include
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
//declare variable
char enter_option;
cout<< "Please enter your option 'a'for adding 'c' for cancelling"<
//What would happens if user enter different than a and c?
//Prompt again till valid input entered.
while (enter_option !='A' && enter_option!= 'a' && enter_option!='C' && enter_option!= 'c');
{
//call add reservation
add_reservation(char enter_option);
//call cancellation function
cancel_reservation(char enter_option);
}
}
add_reservation(char enter_optoin)
{
//if 'a' is entered request the num of seats required
if(enter_option == 'a')
cout<<"How many seats are required"<
cin>> seatReq;
//if enough seats still available , idenfity the seat to be reserved
//Alywas do the lowest numbered seats available
if (seatReq <= 49)
//Print the numbers of the seats allocated along with
//a reservation number so the reservation can be identified
//later for purposes of cancellation.
cout<<"You required"<< seatReq <<"The seat has been reserved"<
{
//If there are not enough seats, print an error message
cout<<"Sorry, there is not enough seats, try again?"<
}
cancel_reservation(char enter_option)
{
//If c is entered, request the reservation number and locate the seats reserved under that reservation number
if(enter_option == 'c')
cout<<"What is the reservation number?"<
//Mark the seats as available again
//If the reservation is not found, print error message
if (seatReq != seatReq)
cout<<"Sorry, the reservation number is not valid, try again?"<
}
//If neither a or c is entered prompt again
//When a reservation added, first check whether the seats can be reserved as a single contiguous block.
return 0;
Sam HobbsPosted Nov 16, 2011, 1:19 PM
If siabanie banie were to cooperate, then I could and would be more specific about the problems. I did create a project of my own with the code; that is how I was able to see that "seatRes" is not needed.
Also note that it is a mistake to use iostream.h. That file is archaic and does not even exist for VC 2010. It is iostream without the ".h". The iostream.h file in older versions of VC had non-standard classes. The C++ standard requires that the file be called iostream without the ".h".
Karthik, you still do not believe me that I know C++. I wish you would believe me that I do.
Karthik AgarwalPosted Nov 16, 2011, 5:09 AM
Sam HobbsPosted Nov 16, 2011, 5:03 AM
Karthik AgarwalPosted Nov 16, 2011, 4:42 AM
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
//declare your variables here
char enter_option;
cout << "please enter your option a for adding and c for cancelling" << endl;
cin >> enter_option;
while(1)
{
//call add reservation function here like below
add_reservation(char enter_option);
//call cancellation function here like below
cancel_reservation(char enter_option);
}
}
add_reservation(char enter_option)
{
//also declare if any variables required
//add your reservation code here as it is in the way you wrote
}
cancel_reservation(char enter_option)
{
//also declare if any variables required
//add your cancellation code here as it is in the way you wrote
}
When it comes to the code which you wrote it will never be compiled because you didn't add the header file below which is required for IO operations. Hence try this way and let me know if you have any concerns.
Sam HobbsPosted Nov 15, 2011, 10:23 PM
What are the compile errors?
siabanie baniePosted Nov 15, 2011, 6:12 PM
Sam HobbsPosted Nov 13, 2011, 5:07 AM
I do not understand "use array or const here"; where is here?
In your code, change "seatRes" to "seatReq"; you made a mistake and I think you don't need "seatRes".
Also note that if "a" is entered for the action to do, then when the program checks for "c" or "C" then it will not be one of those but then the "else" will execute but it should not execute the "else" when the action is "a". Beginners usually put everything into the main function and that makes it difficult to understand. An experienced programmer is more likely to put parts in separate functions. I suggest that you make two more functions; one for adding reservations and another for removing them. Then call the appropriate function from you main.