Solved

C++ program

Posted on 2000-03-30
7
272 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

932 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

12 Experts available now in Live!

Get 1:1 Help Now