Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 131,694 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,495 people online right now. Registration is fast and FREE... Join Now!




Assigning and declaring arrays and elements.

 
Reply to this topicStart new topic

Assigning and declaring arrays and elements., Assigning and declaring arrays and eleme

Flipmodemvp
post 23 Nov, 2005 - 01:17 AM
Post #1


New D.I.C Head

*
Joined: 23 Nov, 2005
Posts: 3


My Contributions


I was asked to write a program to assign seats on a plane. Their are ten seats on a plane and I must use an array to fill up each seat. The input is "1 for smoking" and "2 for non-smoking." How am i suppose to assign the seats on the plane? Here is what i got:

#include <iostream.h>
#include <cmath>

int main()
{
int i, plane, seat[10];

cout<<"This program will assign you seats in the airplane."<<endl;


do
{
cout<<"Please type 1 for ''smoking''"<<endl;
cout<<"Please type 2 for ''non-smoking''"<<endl;

cin>>seat[i];
for (i=0;i<10;i++)
if (seat[i]==1)
cin>>seat[i];
else if (seat[i]==2)
{
cin>>seat[i];
cout<<"Your seat assignment is "<<seat[5]<<" (non-smoking section)"<<endl;
}
else
cout<<"Next flight leaves in 3 hours"<<endl;
}while(i<10);



return 0;
}

//now i am getting an error after this so i'm really not sure what to do. Please HELP!
User is offlineProfile CardPM

Go to the top of the page


Dark_Nexus
post 23 Nov, 2005 - 03:38 AM
Post #2


or something bad...real bad.

Group Icon
Joined: 2 May, 2004
Posts: 1,309



Thanked 1 times

Dream Kudos: 625
My Contributions


CODE
cout<<"Please type 1 for ''smoking''"<<endl;


this line will generate an error as you cannot have "'s in a string like that, you have to use the '\"' character to denote a double quote insinde of a string

CODE
cout<<"Please type 1 for \''smoking\''"<<endl;


as for filling the seats...do you have to fill each seat, one at a time, or keep requesting "smoking" or "non-smoking" from the user until all the seats are filled up. is part of the array supposed to be designated for smoking and the other part to be non-smoking only?
User is offlineProfile CardPM

Go to the top of the page

Flipmodemvp
post 23 Nov, 2005 - 10:40 AM
Post #3


New D.I.C Head

*
Joined: 23 Nov, 2005
Posts: 3


My Contributions


Yes, I suppose to keep asking the user to input smoking or nonsmoking until all seats are full. I was thinking of using a do-while loop to keep asking the same question. But what I don't understand is how can I keep looping that question and fill up each array element with a passenger.
User is offlineProfile CardPM

Go to the top of the page

Flipmodemvp
post 23 Nov, 2005 - 10:44 AM
Post #4


New D.I.C Head

*
Joined: 23 Nov, 2005
Posts: 3


My Contributions


and also, yes, only arrays [0]-[4] are non-smoking and arrays [5]-[9] should be for smoking. Once all of smoking or non-smoking is filled up, it should output something like, "The smoking section is full, would you like to sit in the smoking section(y or n). And for this I would use an "if" statement I believe. And if they choose "n" it will output "next flight leaves in 3 hours."
User is offlineProfile CardPM

Go to the top of the page

microchip
post 23 Nov, 2005 - 01:10 PM
Post #5


New D.I.C Head

*
Joined: 14 Aug, 2005
Posts: 37



Thanked 3 times
My Contributions


I'd do something like this then:

CODE

#define TOTAL_SEATS 10 //total number of seats
#define START_SMOKING_SEATS 5 //what seat number and up are smoking

unsigned char smoking = 0;
unsigned char nonsmoking = 0;

unsigned char seats[TOTAL_SEATS];

do {
   // cout code
   if (input_smoking) {
       if (smoking > (TOTAL_SEATS-START_SMOKING_SEATS)) {
           // no more smoking seats
       } else {
           seats[smoking+START_SMOKING_SEATS] = 2; // 2 = smoking
           smoking++;
       }
   } else if (input_nonsmoking) {
       if (nonsmoking == START_SMOKING_SEATS) {
           // no more non-smoking seats
       } else {
           seats[nonsmoking] = 1; // 1 = smoking
           nonsmoking++;
       }
   }
} while ((smoking+nonsmoking) != TOTAL_SEATS);


Something like that.

There is some psuedocode in, so you'll still have to do some thinking...

This post has been edited by microchip: 23 Nov, 2005 - 01:11 PM
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/20/08 08:32AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month