Solved

C++ program

Posted on 2000-03-30
7
278 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
ID: 2670468
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
ID: 2670563
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
ID: 2670581
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 3

Expert Comment

by:TEFKASG
ID: 2670700
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
ID: 2671375
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
ID: 2671440
OOps, my fault.   I forgot the getch()

thanx a bunch:)
0
 

Expert Comment

by:siewling
ID: 2671457
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
TTimer no longer functioning 7 90
how to understand recursion 12 226
c++ getting the first 10 characters of a char* string 11 93
White board coding practice 3 88
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
  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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

770 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