Solved

C++ program

Posted on 2000-03-30
7
286 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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…

821 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