| | |
Array C++ Help
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2008
Posts: 2
Reputation:
Solved Threads: 0
Hello. My first thread here @ daniweb.
Im stuck on some source code.
I am trying to create a small program that allows user to input '1' or '2' for class selection on an airplane. 1 for first 2 for economy. the plane capacity is 10, which is also the [array] size. If First Class is full (1-5) cout << "Can we move you to economy" and vice versa. Im having a tough time implementing an array to carry this out and allows the user to fill the capacity without over booking. Here's what i have so far, its pretty far off because I was stuck awhile ago so I created this ugly alternative, but it does basically what i want it to do ish.
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
#include <iomanip>
using std::setw;
int main()
{
const int capacity = 10;
int plane[capacity] = {0};
int decision;
int firstclass = 5;
int economy = 5;
cout << " Hello and Welcome to UNA (United Nerd Air) " << endl;
cout << " Looks Like You're Looking to Purchase Tickets Today?" << endl;
for (int i = 0; i <= 9; i++)
{
cout << endl << setw(45) << "First Class or Economy?" << endl;
cout << setw(42) << "( 1 ) or ( 2 )" << endl;
cin >> decision;
if (decision == 1)
{
cout << setw(45) << "First Class has been selected" << endl << endl;
cout << setw(20) << --firstclass << setw(15) << " First Class seats remain." << endl;
if (firstclass < 1 && economy >= 1)
cout << setw(45) << "First Class is booked. Economy?" << endl;
}
else
if (decision == 2)
{
cout << setw(45) << "Economy has been selected" << endl << endl;
cout << setw(20) << --economy << setw(15) << " Economy seats remain." << endl;
if (economy < 1 && firstclass >= 1)
cout << setw(45) << "Economy is full. First Class?" << endl;
}
else
cout << "Error.\n1. First Class\n2. Second Class" << endl;
}
return 0;
}
any suggestions? help? greatly appreciated.
Im stuck on some source code.
I am trying to create a small program that allows user to input '1' or '2' for class selection on an airplane. 1 for first 2 for economy. the plane capacity is 10, which is also the [array] size. If First Class is full (1-5) cout << "Can we move you to economy" and vice versa. Im having a tough time implementing an array to carry this out and allows the user to fill the capacity without over booking. Here's what i have so far, its pretty far off because I was stuck awhile ago so I created this ugly alternative, but it does basically what i want it to do ish.
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
#include <iomanip>
using std::setw;
int main()
{
const int capacity = 10;
int plane[capacity] = {0};
int decision;
int firstclass = 5;
int economy = 5;
cout << " Hello and Welcome to UNA (United Nerd Air) " << endl;
cout << " Looks Like You're Looking to Purchase Tickets Today?" << endl;
for (int i = 0; i <= 9; i++)
{
cout << endl << setw(45) << "First Class or Economy?" << endl;
cout << setw(42) << "( 1 ) or ( 2 )" << endl;
cin >> decision;
if (decision == 1)
{
cout << setw(45) << "First Class has been selected" << endl << endl;
cout << setw(20) << --firstclass << setw(15) << " First Class seats remain." << endl;
if (firstclass < 1 && economy >= 1)
cout << setw(45) << "First Class is booked. Economy?" << endl;
}
else
if (decision == 2)
{
cout << setw(45) << "Economy has been selected" << endl << endl;
cout << setw(20) << --economy << setw(15) << " Economy seats remain." << endl;
if (economy < 1 && firstclass >= 1)
cout << setw(45) << "Economy is full. First Class?" << endl;
}
else
cout << "Error.\n1. First Class\n2. Second Class" << endl;
}
return 0;
}
any suggestions? help? greatly appreciated.
•
•
Join Date: Jul 2005
Posts: 1,749
Reputation:
Solved Threads: 283
Use pencil and paper to work out your logic before trying to write the code. It might look something like this:
C++ Syntax (Toggle Plain Text)
assume: a seat equals an element of the array if value of an element in the array is zero, then seat is available logic: if(decision == 1) if no first class seats available display message else display message--the following seats are open loop through first five seats of plane if value of current element of plane is zero display result of current index plus 1 use a loop to allow user selection of available seats loop until valid selection made or user gives up display request for selection if valid selection if selected seat is availble change value of that seat to some value other than zero else display message that seat is already taken else display message that seat is not part of first class
Last edited by Lerner; Nov 25th, 2008 at 11:57 am.
Klatu Barada Nikto
•
•
Join Date: Dec 2008
Posts: 2
Reputation:
Solved Threads: 0
I have been working on a similar project, but am stalled in my attempts to test my program. I have a syntax error inthe main part of my program but have been unable to find it. I have listed the error message below.....
error C2059: syntax error : 'return'
I am at my whits end and would be gratefull for any help or hints in finding my error .
error C2059: syntax error : 'return'
I am at my whits end and would be gratefull for any help or hints in finding my error .
// Program: First Economy Air, Automated Seat Reservation Program
// This is a menu driven program that assigns seats in a 10 seat
//airplane, then prints out a boarding pass then a seating chart
// seats 1-5 are first class and 6-10 are economy
// Amy Higgins
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
//function prototypes
void showMenu (); // Holds menu choices
void printSeatingChart (int [], int); // Prints to screen Array
void firstClassTicket (int [], int); // Used to assign first class seats if availible
void economyTicket (int [], int); // Used to assign economy seats if availible
int main()
{
const int SEATS = 10; // Seating capacity of airplane
int airplane[SEATS] = {0}; // Airplane array, used to keep
// track of open and occupied seats
int firstClass; // holds the seat number reserved (1-5)
int economy; // holds the seat number reserved (6-10)
nt choice; // To hold a menu choice
do
{
// Display the menu and get the user's choice.
showMenu();
cin >> choice;
// Validate the menu selection.
while (choice < 1 || choice > 3)
{
cout << "Please enter 1, 2, or 3: ";
cin >> choice;
}
if (choice >= 1 || choice <= 3)
{
switch (choice)
{
case 1: firstClassTicket(airplane, SEATS);
break;
case 2: economyTicket(airplane, SEATS);
break;
case 3: printSeatingChart(airplane, SEATS);
break;
}
}
}
return 0;
}
//**************************************************
// Definition of function showMenu which displays the menu. *
//*************************************=************
void showMenu()
{
cout << "\n\t\tWelcom to First Economy Air\n\n";
cout<<"======================================\n";
cout<<" Please select an option from the menu \n";
cout<< "below."<<endl<<endl;
cout << "1. First Class Ticket\n";
cout << "2. Economy Ticket\n";
cout << "3. Exit\n\n";
cout << "Enter your choice: ";
}
//*******************************************
// Definition of function printSeatingChart *
//********************************************
void printSeatingChart (int array [], int size)
{
cout<< "print seat chart, exit."<<endl;
for(int i = 0; i < size; i++) // Print contents of array
{
cout<< array[i]<<endl;
}
}
//*****************************************
// Definition of function firstClassTicket *
//******************************************
void firstClassTicket (int array [], int size)
{
// this is just a test to see if this function
//can read and return array values
cout<< "you have purchased a First class ticket."<<endl;
for(int i = 0; i < size; i++)
{
cout<< array[i]<<endl;
}
}
//*******************************************
// Definition of function economyTicket *
//*******************************************
void economyTicket (int array [], int size)
{
// this is just a test to see if this function
//can read and return array values
cout<< "you have purchased a Economy ticket."<<endl;
for(int i = 0; i < size; i++)
{
cout<< array[i]<<endl;
}
} Firstly, please don't hijack others threads...start your own in future. Your syntax error is the fact that you have a do..while loop definded as follows
with no while statement
Chris
C++ Syntax (Toggle Plain Text)
do{ //somecode }
do{
//somecode
}while(somecondition);Chris
Knowledge is power -- But experience is everything
•
•
Join Date: Dec 2008
Posts: 2
Reputation:
Solved Threads: 0
I'm sorry , hijacking the thread was not my intent. In some of the threads I read earlier the repliers complained of people starting multiple threads on the same subject. In an attempt to avoid one blunder I have made another. I'll reread the tutorial on posting threads before I make another post.
Thank you for your quick answer. I know what i need to change now. Thank you for your help once again.
Thank you for your quick answer. I know what i need to change now. Thank you for your help once again.
![]() |
Similar Threads
- Can I ghost a RAID array??? (Windows NT / 2000 / XP)
- Creating dynamic array structures (C++)
- Array limit (C)
- struct dynamic 2d array alloc (C)
- string to integer array transformation (C)
- Array (Visual Basic 4 / 5 / 6)
Other Threads in the C++ Forum
- Previous Thread: Simple MP3 Player DLL
- Next Thread: trouble with for loop
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






