Solved

C++ program

Posted on 2000-03-30
7
267 Views
Last Modified: 2010-04-02
I have written a program that prompts the user for a smoking preference and then issues a boarding pass accordingly.. Seats 1-5 are smoking and seats 6-10 are not.  Everything works except that when one section is full and it asks the user if they would like to be seated in the other section, it issues a boarding pass for each section with the same number.???  Also I can't get the "Plane is full" to work.  It just keeps going.???

Here's my code so far:
#include<iostream.h>
#include<conio.h>

int smoke=0;
int nonsmoke=5;
int Issue_Smoking_Seat();
int Issue_NonSmoking_Seat();

int main()
{

  int Prompt_Customer_For_Preference();

  Prompt_Customer_For_Preference();
 
  if(smoke==5&&nonsmoke==10){
     clrscr();
     cout<<"\n\n\t\tThis flight is full.\n";
  }//end if
}

int Prompt_Customer_For_Preference()
{
  char ans;

  do{
    do{
    clrscr();
    cout<<"\n\n\t\tW E L C O M E  T O  X Y Z  A I R L I N E S \n";
    cout<<"\n\tPlease Enter S for a Smoking seat or N for a Non-Smoking seat\n";
    cin>>ans;
    ans=toupper(ans);
    }while(ans!='S'&&ans!='N');
    //end do while

    if(ans=='S'){
      Issue_Smoking_Seat();
    }//end if

    if(ans=='N'){
      Issue_NonSmoking_Seat();
    }//end if

  }while(smoke<5||nonsmoke<10);
  //end do while

  clrscr();

}

int Issue_Smoking_Seat()
{
  char ans2;
  smoke++;

  if(smoke>5){
    clrscr();
    cout<<"\t\tWe are sorry but the Smoking section is full.\n";
    cout<<"\t\tWould you like a seat in the Non-Smoking section?(Y/N)\n";
    cin>>ans2;
    ans2=toupper(ans2);
 
    if(ans2!='Y'&&ans2!='N'){
      clrscr();
      cout<<"\t\t\Invalid entry. Please try again:\n";
      cin>>ans2;
      ans2=toupper(ans2);
    }//end if

    if(ans2=='Y'){
      Issue_NonSmoking_Seat();

    }//end if

    if(ans2=='N'){
      clrscr();
      cout<<"The next flight leaves in three hours.\n";
     
    }//end if

  }//end if

  clrscr();
  cout<<"\n\n\t\t*****************";
  cout<<"\n\n\t\t  BOARDING PASS"<<endl<<"\n\t\t  FLIGHT XYZ 1234"<<endl;
  cout<<"\n\t\t  SEAT "<<smoke<<endl<<"\n\t\t  SMOKING\n";
  cout<<"\n\t\t****************";
  getch();
}

int Issue_NonSmoking_Seat()
{
  char ans1;
  nonsmoke++;

  if(nonsmoke>10){
    clrscr();
    cout<<"\t\tWe are sorry but the Non-Smoking section is full.\n";
    cout<<"\t\tWould you like a seat in the Smoking section?(Y/N)\n";
    cin>>ans1;
    ans1=toupper(ans1);

    if(ans1!='Y'&&ans1!='N'){
      cout<<"\t\tInvalid entry. Please try again:\n";
      cin>>ans1;
      ans1=toupper(ans1);
    }//end if

    if(ans1=='Y'){
      Issue_Smoking_Seat();
    }//end if

    if(ans1=='N'){
      cout<<"\t\tThe next flight leaves in three hours.\n";
    }//end if

  }//end if

  clrscr();
  cout<<"\n\n\t\t******************";
  cout<<"\n\n\t\t  BOARDING PASS"<<"\n\t\t  FLIGHT XYZ 1234"<<endl;
  cout<<"\n\t\t  SEAT "<<nonsmoke<<endl<<"\n\t\t  NON-SMOKING\n";
  cout<<"\n\t\t******************";
  getch();
}
0
Comment
Question by:licker
7 Comments
 
LVL 3

Expert Comment

by:TEFKASG
Comment Utility
I believe the problem is in the functions Issue_NonSmoking_Seat() and Issue_Smoking_Seat().  The problem is that in each you can increment either the smoke or nonsmoke member variables past the limit you are checking.  The easiest fix would be:  

int function Issue_NonSmoking_Seat(){
  char ans1;
  nonsmoke++

if (nonsmoke > 10){
 nonsmoke--;
 clrscr();
..
..
the rest of your code




int function Issue_Smoking_Seat(){
  char ans2;
  smoke++;

 if (smoke > 5){
  smoke--;
  clrscr();
   .
   .
  The rest of your code
 
I believe this will solve your problem which is why I am posting as an answer.  There are better way of doing this instead of the increment then decriment operation, but this will work.
                                 
0
 

Accepted Solution

by:
mbogaerts earned 120 total points
Comment Utility
You keep incrementing the values that you use to test the plane full condition. You shouldn't. This is how you can solve this. Change your functions:

int Issue_Smoking_Seat()
{
char ans2;
if ( smoke == 5 )
{
// your code for smoke > 5
..
..
}
else
{
smoke++;
clrscr();
cout<<"\n\n\t\t*****************";
cout<<"\n\n\t\t  BOARDING PASS"<<endl<<"\n\t\t  FLIGHT XYZ 1234"<<endl;
cout<<"\n\t\t  SEAT "<<smoke<<endl<<"\n\t\t  SMOKING\n";
cout<<"\n\t\t****************";
}
getch();
}


int Issue_Non_Smoking_Seat()
{
char ans2;
if ( nonsmoke == 10 )
{
// your code for nonsmoke > 10
..
..
}
else
{
nonsmoke++;
clrscr();
cout<<"\n\n\t\t******************";
cout<<"\n\n\t\t  BOARDING PASS"<<"\n\t\t  FLIGHT XYZ 1234"<<endl;
cout<<"\n\t\t  SEAT "<<nonsmoke<<endl<<"\n\t\t  NON-SMOKING\n";
cout<<"\n\t\t******************";
}
getch();
}

This way your counters never get above their limit and your end test will work.
It will also solve the problem of getting two boarding passes.

0
 

Author Comment

by:licker
Comment Utility
This didn't do anything but stop the boarding pass from increasing past its limit.  I still displays both.

Ex:

  Boarding Pass seat 6 nonsmoking- then clrscr-then:
  Boarding Pass seat 5 smoking

And I still can't get the "Plane is full" to work.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 3

Expert Comment

by:TEFKASG
Comment Utility
I see what I missed the first time.  Sorry about that.  I'd post another solution, but mbogaerts beat me to it.
0
 

Author Comment

by:licker
Comment Utility
mbogaerts,

Your soluttion worked great to solve the double boarding pass but I still can't get it to display the "Plane is full" comment

if(smoke==5&&nonsmoke==10){
     clrscr();
     cout<<"\n\n\t\tThis flight is full.\n";
  }//end if

And I noticed that when the user says they do not one a seat in the alternate section, the comment "Next flight ..." doesn't display either???

if(smoke==5&&nonsmoke==10){
     clrscr();
     cout<<"\n\n\t\tThis flight is full.\n";
  }//end if
0
 

Author Comment

by:licker
Comment Utility
OOps, my fault.   I forgot the getch()

thanx a bunch:)
0
 

Expert Comment

by:siewling
Comment Utility
licker,
    I suggest you to return the value of smoke in each functions so the main function will receive directly the incremented value of 'smoke'. I hope this will help you.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now