Thread: Help in 1D C++

  1. #1
    "PALINDROME"
    Join Date
    Nov 2010
    Posts
    59

    Help in 1D C++

    Help !!!!
    Last edited by [Student]; 07-16-2011 at 08:59 PM.

  2. #2
    "PALINDROME"
    Join Date
    Nov 2010
    Posts
    59
    Code:
    #include<iostream>
    #include<conio.h>
    using namespace std;
    
    void waitinglist(int choice, int plane[11]){
        
        cout<<"First Class"<<endl;
        cout<<" ---------------------\n";
        cout<<"   ";
        for(int i=1; i<=5; i++){
        if(choice == plane[i]){
                  cout<<"X   ";
                  }  
                  }
                  }
    void waitinglist2(int choice, int plane[11]){
         
          cout<<"\n\nEconomy"<<endl;
        cout<<" ---------------------\n";
        cout<<"   ";
        for(int i=1; i<=5; i++){
                if(plane[i] == choice){
                cout<<"X   ";
                  }  
                  }
                  }
         
    int main(){
        
        int plane[11]={0};
        int i = 0;
        int firstclass=1;
        int economy = 6;
        int choice;
        char response[2];
        
        
        press:
              waitinglist(choice,plane);
              waitinglist2(choice,plane);
        cout<<"\n\n\nPlease type 1 - First class \n";
        cout<<"Please type 2 - Economy \n";
          
        while ( i < 10 ) {
           cin>>choice;
           if(choice == 1){
                     if(!plane[firstclass] && firstclass<=5){
                     cout<<"\nReservation Confirmed - Seat# "<<firstclass<<"\n";;
                     plane[firstclass++]=1;
                     i++;
                     getch();
                     system("cls");
                     goto press;
                     }
                     
                     else if ( firstclass > 5 && economy <= 10 ) {
                     cout<<"\nThe first class section is full.\n";
                     cout<<"Would you like to sit in the economy";
                     cout<<" section ( Y or N )? ";
                     cin>>response;
                     
                     if ( response[ 0 ] == 'Y') {
                          cout<<"\nReservation Confirmed - Seat# "<<economy<<"\n";
                          plane[ economy++ ] = 1;
                          i++;
                          getch();
                          system("cls");
                          return main();
                          }
                          else { 
                          cout<<"\nNext flight leaves in 3 hours.\n";
                          getch();
                          system("cls");
                          goto press;
                          } 
                          } 
                          else { 
                          cout<<"\nNext flight leaves in 3 hours.\n";
                          getch();
                          system("cls");
                          return main();
                          }
    
                          } 
                          else { 
                               if ( !plane[ economy ] && economy <= 10 ) {
                               cout<<"\nReservation Confirmed - Seat# "<<economy<<"\n";
                                plane[ economy++ ] = 1;
                                i++;
                                getch();
                                system("cls");
                                goto press;
                                }
                                else if ( economy > 10 && firstclass <= 5 ) {
                                 cout<<"\nThe first class section is full.\n";
                                 cout<<"Would you like to sit in the economy";
                                 cout<<" section ( Y or N )? ";
                                 cin>>response;    
                                 
                                 if ( toupper( response[ 0 ] ) == 'Y' ) {
                                      cout<<"\nReservation Confirmed - Seat# "<<firstclass<<"\n";
                                      plane[ firstclass++ ] = 1;
                                      i++;
                                      getch();
                                      system("cls");
                                      goto press;
                                      }
                                      else {
                                           cout<<"\nNext flight leaves in 3 hours.\n";
                                           getch();
                                           system("cls");
                                           return main();
                                           }
                                           }
                                           else { 
                                           cout<<"\nNext flight leaves in 3 hours.\n";
                                           getch();
                                           system("cls");
                                           return main();
                                           }
                                           }
                                           }
                                           cout<<"\nReservation Full\n"; 
                                           getch();
                                           system("cls");
                                           return main();               
                                           return 0;
                                           }
    Here is My Code I'm at average of 85%, I'm Stuck in Waiting List....plz need your help!...

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What, exactly, are you stuck on?

  4. #4
    "PALINDROME"
    Join Date
    Nov 2010
    Posts
    59
    I'm Stuck in this code....i got a wrong in waiting list....plz i need your help...

    Code:
    void waitinglist(int choice, int plane[11]){
        
        cout<<"First Class"<<endl;
        cout<<" ---------------------\n";
        cout<<"   ";
        for(int i=1; i<=5; i++){
        if(choice == plane[i]){
                  cout<<"X   ";
                  }  
                  }
                  }
    void waitinglist2(int choice, int plane[11]){
         
          cout<<"\n\nEconomy"<<endl;
        cout<<" ---------------------\n";
        cout<<"   ";
        for(int i=1; i<=5; i++){
                if(plane[i] == choice){
                cout<<"X   ";
                  }  
                  }
                  }

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Your code prints some number of X's. Is it supposed to do something else?

    Also, note that the first time you call this function, the value of choice is indeterminate.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I can help you alright...

    1. Stop calling main() recursively!
    2. Stop using goto!
    3. Fix your code indentation. Writing code is not supposed to be a curly-brace counting exercise!

    Yes I'm totally serious.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed