Stuck!! Not Far More Need Help!!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • 1051109210
    New Member
    • Mar 2007
    • 29

    Stuck!! Not Far More Need Help!!

    Code:
    #include<iostream>
    
    using namespace std;
    
    int main(){
     
        string seat[7][4];
        int row,i,j;
        char col,response,ok;
        
      for(int p=1;p<=7;p++){
    cout<<p;
    
    seat[p][0];
    cout<<"\tA";
    seat[p][1];
    cout<<"\tB";
    seat[p][2];
    cout<<"\tC";
    seat[p][3];
    cout<<"\tD\n";
    
    }
    
        do
        {
        cout << "\nPlease select seating: ";
        cin >> row>>col;
    
        i = row-1;
    
        if (col=='A'||col=='a'){
        j = 0;
        }
        if (col=='B'||col=='b'){
        j = 1;
        }
        if (col=='C'||col=='c'){
        j = 2;
        }
        if (col=='D'||col=='d'){
        j = 3;
        }
        
        if (seat[i][j]=="X"){
        cout << "This seat has been taken. Please choose a new one." << endl << endl;
        
    
          for(int p=1;p<=7;p++){
    cout<<p;
    seat[p][0];
    cout<<"\tA";
    seat[p][1];
    cout<<"\tB";
    seat[p][2];
    cout<<"\tC";
    seat[p][3];
    cout<<"\tD\n";
        }
    }    else{
     
        switch(row){
        case 1: if (col=='A'||col=='a'){
                seat[0][0]="X";
                }
                else if (col=='B'||col=='b'){
                seat[0][1]="X";
                }
                else if (col=='C'||col=='c'){
                seat[0][2]="X";
                }
                else if (col=='D'||col=='d'){
                seat[0][3]="X";
                }
        break;
        
        case 2: if (col=='A'||col=='a'){
                seat[1][0]="X";
                }
                else if (col=='B'||col=='b'){
                seat[1][1]="X";
                }
                else if (col=='C'||col=='c'){
                seat[1][2]="X";
                }
                else if (col=='D'||col=='d'){
                seat[1][3]="X";
                }
        break;
        
        case 3: if (col=='A'||col=='a'){
                seat[2][0]="X";
                }
                else if (col=='B'||col=='b'){
                seat[2][1]="X";
                }
                else if (col=='C'||col=='c'){
                seat[2][2]="X";
                }
                else if (col=='D'||col=='d'){
                seat[2][3]="X";
                }
        break;
        
        case 4: if (col=='A'||col=='a'){
                seat[3][0]="X";
                }
                else if (col=='B'||col=='b'){
                seat[3][1]="X";
                }
                else if (col=='C'||col=='c'){
                seat[3][2]="X";
                }
                else if (col=='D'||col=='d'){
                seat[3][3]="X";
                }
        break;
        
        case 5: if (col=='A'||col=='a'){
                seat[4][0]="X";
                }
                else if (col=='B'||col=='b'){
                seat[4][1]="X";
                }
                else if (col=='C'||col=='c'){
                seat[4][2]="X";
                }
                else if (col=='D'||col=='d'){
                seat[4][3]="X";
                }
        break;
        
        case 6: if (col=='A'||col=='a'){
                seat[5][0]="X";
                }
                else if (col=='B'||col=='b'){
                seat[5][1]="X";
                }
                else if (col=='C'||col=='c'){
                seat[5][2]="X";
                }
                else if (col=='D'||col=='d'){
                seat[5][3]="X";
                }
        break;
        
        case 7: if (col=='A'||col=='a'){
                seat[6][0]="X";
                }
                else if (col=='B'||col=='b'){
                seat[6][1]="X";
                }
                else if (col=='C'||col=='c'){
                seat[6][2]="X";
                }
                else if (col=='D'||col=='d'){
                seat[6][3]="X";
                }
        break;   
    }
      
          for(int p=1;p<=7;p++){
    cout<<p;
    seat[p][0];
    cout<<"\tA";
    seat[p][1];
    cout<<"\tB";
    seat[p][2];
    cout<<"\tC";
    seat[p][3];
    cout<<"\tD\n";}
        
        if (seat[0][0]=="X" && 
        seat[1][0]=="X" &&
        seat[2][0]=="X" &&
        seat[3][0]=="X" &&
        seat[4][0]=="X" &&
        seat[5][0]=="X" && 
        seat[6][0]=="X" &&
        seat[0][1]=="X" &&
        seat[1][1]=="X" &&
        seat[2][1]=="X" &&
        seat[3][1]=="X" &&
        seat[4][1]=="X" &&
        seat[5][1]=="X" &&
        seat[6][1]=="X" &&
        seat[0][2]=="X" &&
        seat[1][2]=="X" &&
        seat[2][2]=="X" &&
        seat[3][2]=="X" &&
        seat[4][2]=="X" &&
        seat[5][2]=="X" &&
        seat[6][2]=="X" &&
        seat[0][3]=="X" &&
        seat[1][3]=="X" &&
        seat[2][3]=="X" &&
        seat[3][3]=="X" &&
        seat[4][3]=="X" &&
        seat[5][3]=="X" &&
        seat[6][3]=="X"){
                         
                         
        system("pause");
        return 0; 
        
        }else{
    
        do {
            
        cout << "Do you want to continue? [y/n]";
        cin >> response;
        
        if(response=='y' || response=='n' || response=='N' || response=='Y'){
        ok='e';
        }
        else{
             ok='p';
        }
        
    }while(ok=='p');
    }
    }
        }while(response=='y' || response =='Y');
        
    system("pause");
    return 0;   
    }

    ok so where went wrong?its suppose to show "x" after i choose d seat
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    Please read the Posting Guidelines, especially concerning clear thread titles and posting full code.

    I would recommend learning functions and code abstraction - if you cut everything out of your first do-while, you have almost nothing else in there. If you could abstract those calls, your main (and your overall program) would be much smaller.

    In the first part of your program you make several calls like this:

    seat[p][0];

    but don't do anything with them. These can be removed.

    Other than that, you need to ask a more specific question to get more help, there is quite a bit that can be changed in this code...

    Comment

    • 1051109210
      New Member
      • Mar 2007
      • 29

      #3
      ohoh...ok sorry abt dat....
      here's d q...

      Questiom :-

      write a program to assign passengers seats in an airplane. Assume a
      small airplane with seat numbering as following:

      1 A B C D
      2 A B C D
      3 A B C D
      4 A B C D
      5 A B C D
      6 A B C D
      7 A B C D


      The program should display the seat pattern, with an 'X' marking the seats
      already assigned. For example, after seats 1A, 2B and 4C are taken, the display
      should look like this:

      1 x B C D
      2 A X C D
      3 A B C D
      4 A B X D
      5 A B C D
      6 A B C D
      7 A B C D


      after displaying the seats available, the program prompts for the seat
      desired, the user type in a seat, and then the display of availble seats
      is updated. This continues until all the seats are filled or until the used
      signals that the program should end.

      If the user types in a seat that is already assigned, the program should
      say that the seat is occupied ans ask for another choice.


      my program will run but it wont display "x" but it does recognizes if the seat taken already

      Comment

      • dubZ
        New Member
        • Mar 2007
        • 7

        #4
        Originally posted by 1051109210
        ohoh...ok sorry abt dat....
        here's d q...

        Questiom :-

        write a program to assign passengers seats in an airplane. Assume a
        small airplane with seat numbering as following:

        1 A B C D
        2 A B C D
        3 A B C D
        4 A B C D
        5 A B C D
        6 A B C D
        7 A B C D


        The program should display the seat pattern, with an 'X' marking the seats
        already assigned. For example, after seats 1A, 2B and 4C are taken, the display
        should look like this:

        1 x B C D
        2 A X C D
        3 A B C D
        4 A B X D
        5 A B C D
        6 A B C D
        7 A B C D


        after displaying the seats available, the program prompts for the seat
        desired, the user type in a seat, and then the display of availble seats
        is updated. This continues until all the seats are filled or until the used
        signals that the program should end.

        If the user types in a seat that is already assigned, the program should
        say that the seat is occupied ans ask for another choice.


        my program will run but it wont display "x" but it does recognizes if the seat taken already
        Your seat array could be of type char rather than type string. I believe the second dimension (Y dimension) should be 5 to hold the row number in the first column. Then you know that column 1 will be "A's", column 2 will be "B's", etc. You should initialize the array to its default state (with no seats taken), then you can mark seats as being taken. When you are marking seats,I assume that you will never have to worry about the first column since it is just listing the row numbers.

        Also, I agree with the previous post that suggests you should break this code down into seperate functions to do the job. It would make it much easier for you to follow and figure out what is going on or going wrong.

        Comment

        • Demoris
          New Member
          • Mar 2007
          • 5

          #5
          Adding a few functions to clean up your program would be very helpful. Some possible functions could include:
          • changing seat to display an 'X'
          • checking to see if a seat is occupied
          • checking to see if all seats are occupied
          • displaying the seat arrangement after each seat choice by the user



          To essentially eliminate the need for the switch statement:
          (the reason for 65 is thats the ascii code for 'A' - as such, you'd need to use toupper() if a lowercase letter is entered)

          Code:
          if (seat not taken)
              seat[row-1][static_cast<int>(col - 65)] = 'X';

          Also, it would be useful to make a function that checks if a seat is taken:

          Code:
          if (seats[row-1][static_cast<int>(col - 65)] == 'X') return true;
          else return false;
          And a function to check if all seats are occupied: This function checks through easch seat in the array. If all (28) == 'X', then it returns true, implying all seats are taken.

          Code:
          int counter = 0;
          	
          	for (int i = 0; i < 7; i++)
          	{
          		for (int j = 0; j < 4; j++)
          			 if (seat[row-1][static_cast<int>(col - 65)] = 'X') counter++;
          	}
          	
          	if (counter == 28) return true;
          	else return false;

          Comment

          • 1051109210
            New Member
            • Mar 2007
            • 29

            #6
            huh? i dont quite understand dat...heh...oni 4th week of programming!! but thnks!! will really look into it...this q is frm walter savitch ...problem solving with c++...im doin their ques...d programmin projects...is there anywhere i can get d ans to chck wif wht i've done? the ques there are nice and challengin...bu t cant check it wif anywer...:(

            Comment

            Working...