Solved

C++ program

Posted on 2000-03-30
7
300 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

632 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