#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    New york
    Posts
    1
    Rep Power
    0

    Help!!! My C++ assignment has a problem.


    Hi,

    I am doing my assignment but my program is not working well. I think that something wrong is with my for loop. I hope someone can help me fix the problem.
    Thanks!!!

    My program goes like this.


    #include<iostream.h>
    #include<iomanip.h>

    #define NUM_DESTINATIONS 6


    int main()

    {
    int total_tickets,
    num_orders,
    another_customer,// yes =1, no=0
    another_order;// yes =1, no=0



    double ticket_price[6] ={56.79, 105.69, 93.49, 155.99, 87.49, 73.99};
    int city_tickets[NUM_DESTINATIONS];



    int destination;


    double amount,
    total_amount;



    cout<<setprecision(2)
    <<setiosflags(ios::fixed)
    <<setiosflags(ios::showpoint);

    //Initialize counter and accumulator
    num_orders=0;
    amount=0;
    total_amount=0;
    total_tickets=0;



    //Ask for a user
    cout<<"\nDo you want to process a customer";
    cout<<"\nEnter 1 for Yes or 0 for No: ";
    cin>>another_customer;

    while(another_customer)

    {

    //Ask the use if he or she wants to continue

    cout<<endl;
    cout<<"Do you want to process a ticket order? Enter 1 for Yes or 0 for No: ";
    cin>>another_order; // yes =1, no=0


    while(another_order)

    {
    cout<<endl;
    cout<<"Enter the the number of the destination(1-6): ";


    cin>>destination;
    if ((destination<1) || (destination > 6))
    cout<<"\nInvalid destination number entered. Please try again.";

    else

    {


    cout<<"Enter the the number of tickets desired: ";
    cin>>city_tickets[destination];


    amount= ticket_price[destination-1] * city_tickets[destination];

    cout<<"\nThe number of tickets sold to City "<<destination<<": "
    <<city_tickets[destination];
    cout<<"\nThe price of each ticket: "<<ticket_price[destination-1];
    cout<<"\nThe amount of tickets sold: "<<amount;


    }

    total_amount+= amount;
    total_tickets+=city_tickets[destination];

    cout<<endl;
    cout<<"Do you want to process another ticket order? Enter 1 for Yes or 0 for No: ";
    cin>>another_order; // yes =1, no=0

    }



    for (destination=0; destination<NUM_DESTINATIONS;++destination)
    {


    cout<<"\n\nCity"<<setw(3)<<destination+1<<":"
    <<" The total number of tickets sold:"<<setw(5)<< city_tickets[destination+1]
    <<" and the total amount of tickets sold:"<<setw(5)
    << city_tickets[destination+1]*ticket_price[destination];


    }




    //Ask for a user
    cout<<endl;
    cout<<"Do you want to process another customer?"<<endl;
    cout<<"Enter 1 for Yes or 0 for No: ";
    cin>>another_customer;
    }


    cout<<"\nThe grand total of all the ticket price: "<<total_amount;
    cout<<"\nThe grand total of all the ticket sold: "<<total_tickets;


    return 0;

    }
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    India
    Posts
    9
    Rep Power
    0
    hi ,you are defining city_tickets as a int[6] array & yet you r trying to assess city_tickets[6] element...arrays index in C/C++ starts from 0...so city_tickets[destination+1] will not be b valid when
    destination = NUM_DEST - 1...

    similarily this is also going to cause trouble at destination = 6...

    amount= ticket_price[destination-1] * city_tickets[destination];

IMN logo majestic logo threadwatch logo seochat tools logo