just need a little help

This is a discussion on just need a little help within the C++ Programming forums, part of the General Programming Boards category; I amm writing a program where a plane has ten seats 5 r 1st class and the other 5 r ...

  1. #1
    Registered User edshaft's Avatar
    Join Date
    Nov 2001
    Posts
    45

    Cool just need a little help

    I amm writing a program where a plane has ten seats 5 r 1st class and the other 5 r economy and when all the seats in one category is full u ask if it is alright to place them in the other and well im having alittle trouble:

    int first(int seat[], int i){
    int non;
    if(seat[4]!=1){
    seat[i]=1;
    cout<<"You are in seat "<<i+1<<" in first class\n";
    }
    else {
    cout<<"Is it all right to place you in nonsmoking?(y/n)\n";
    cin>> non;
    if(non=='y'||'Y'){
    int j=5;
    int econ(seat,j);}
    else{
    cout<<"Next flight in three hours\n";}}
    return 0;
    }
    int econ(int seat[], int j){
    int non;
    if(seat[9]!=1){
    seat[j]=1;
    cout<<"You are in seat "<<j-4<<" in economy\n";
    }
    else {
    cout<<"Is it all right to place you in smoking?(y/n)";
    cin>> non;
    if(non=='y'||'Y'){
    int i=0;
    int first(seat,i);}
    else{
    cout<<"Next flight leaves in three hours.";}}
    return 0;
    }
    int main(int argc, char* argv[])
    {int seat[10], assign, i=0, j=5;
    while(seat[10]!=1){
    cout<<"Please type 1 for First Class\n";
    cout<<"Please type 2 for Economy\n";
    cin>>assign;
    if (assign==1){
    first(seat,i);
    i++;}
    else{econ(seat,j);
    j++;}
    }
    return 0;
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Assumng that your user will always enter the proper input (will never happen, so this is purely academic).
    Code:
    int main(){
        int firstClass = 1, coach = 6;
    
        puts("Please enter your section (f = first class, c = coach)"); 
        if(getchar() == 'f'){
            if(firstClass != 6){
                printf("You are in seat %d", firstClass++);
            }else{
                puts("First class is full, will you ride coach? (y/n)");
                if(getchar() == 'y') 
                    if(coach == 11) printf("Sorry, you've been bumped");
                    else printf("You are in seat %d", coach++);
                else printf("I'm sorry, you're screwed");
            }
        }else{
            if(coach != 11){
                printf("You are in seat %d", coach++);
            }else{
                puts("Coach is full, will you ride first class? (y/n)");
                if(getchar() == 'y') 
                    if(firstClass == 6) printf("Sorry, you've been bumped");
                    else printf("You are in seat %d", firstClass++);
                else printf("I'm sorry, you're screwed");
            }
        }
        return 0;
    }
    Throw this in a loop and you will be all set.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User edshaft's Avatar
    Join Date
    Nov 2001
    Posts
    45
    thanx but that is not what i want im usind microsofr visual c++ and well i have my code just about where i want it but when it runs out of room in 1st and i say n to being put in econ it puts me there anyway and when i say n in econ i get an infinte loop here's the code:
    int first(int [], int);
    int econ(int [],int);
    int main(int argc, char* argv[])
    {int seat[5], seats[5], assign, i=0, j=0;
    while(seat[11]!=0){
    cout<<"Please type 1 for First Class\n";
    cout<<"Please type 2 for Economy\n";
    cin>>assign;
    if (assign==1){
    first(seat,i);
    i++;}
    else{econ(seats,j);
    j++;}
    }
    return 0;
    }
    int first(int seat[], int i){
    char non;
    if(seat[4]!=1){
    seat[i]=1;
    cout<<"You are in seat "<<i+1<<" in first class\n";
    }
    else {
    cout<<"Is it all right to place you in nonsmoking?(y/n)\n"<<flush;
    cin>> non;
    if(non=='y'||'Y'){
    int i=0;
    int seats[5];
    econ(seats,i);}
    else{
    cout<<"Next flight in three hours\n";}}
    return 0;
    }
    int econ(int seat[], int j){
    int non;
    if(seat[4]!=1){
    seat[j]=1;
    cout<<"You are in seat "<<j+1<<" in economy\n";
    }
    else {
    cout<<"Is it all right to place you in smoking?(y/n)"<<flush;
    cin>> non;
    if(non=='y'||'Y'){
    int i=0;
    first(seat,i);}
    else
    cout<<"Next flight leaves in three hours.";}
    return 0;
    }
    Follow the white rabbit

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    I'm sorry, but your code is very difficult to read. I'd love to help you, but spending most of my time reformatting your code isn't something i can afford to do. Please go here and read the part about using the code tags.

    -Prelude
    My best code is written with the delete key.

  5. #5
    Unregistered
    Guest

    ur Help

    The reason that the program is going into an infinite loop is because you do not want to go into economy class but there still are some seats left. Therefore, ur while loop in the fifth line of code is continually executed. Use a break command to exit this loop.

  6. #6
    Registered User edshaft's Avatar
    Join Date
    Nov 2001
    Posts
    45

    Question

    I tried to fix it but i dont know what i did
    there's an attachment
    Attached Files Attached Files

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21