473,748 Members | 2,326 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

c++ array help

i've been tryin to do the following -
-Add a new flight number to the end of the queue (got it done)
-LAnd the plane at the front of the queue - problems wit it-
-display the queue - got it done
-seach for a specific flight number in queue ( didn't get there yet)
-move a flight number one one position in the queue to another ( didn't
get there yet)

this is what i have so far. it runs but something is wrong and i don't
know what it is.

#include <iostream>
#include <iomanip>
using namespace std ;

const int MAXQUEUE = 100 ; // maximum number of flights in the queue

void displayQueue (int[], int) ;
void getChoice ();
void addPlane (int[], int&) ;
int checkPlane (int[], int, int) ;

int main ()
{
int queue[MAXQUEUE] ; // array of incoming flight numbers
int qsize = 0 ; // number of flights in the queue
char choice; // user's choice of next operation

do
{ // start menu loop
displayQueue (queue, qsize) ;
getChoice();
switch (choice)
{ // start switch
case 'S' : cout << "under construction" << endl; break ;
case 'L' : cout << "under construction" << endl; break ;
case 'A' : addPlane (queue, qsize) ; break ;
case 'M' : cout << "under construction" << endl; break ;
case 'Q' : cout << "program ended" << endl ;
} // end switch
} while (choice != 'Q'); // end menu loop

return 1 ;
}

//============dis playQueue====== ===============
// task - display the position and flight number of each flight in the
array
// pre - given an array of flight numbers and number of flights in the
array
// post - nothing

void displayQueue (int list[], int lsize)
{ // start function
int w ;
cout << endl << endl << endl ;
if (lsize == 0)
cout << "no flights awaiting landing at this time" << endl ;
else
{ // start listing
cout << "Queue size = " << lsize << endl << endl ;
cout << "Position Flight#" << endl ;
for (w = 0; w < lsize; w++)
cout << setw(5) << w + 1 << setw(12) << list[w] << endl ;
} // end listing
} // end function

//============get Choice========= ===============
// task - obtain a choice code from the user
// pre - nothing
// post - a valid, uppercase code is returned

void getChoice()
{ // start function
char holdCode;
char pick;
if(pick == holdCode)
{
cout << endl << " to search for a flight number, enter S" ;
cout << endl << " to add a flight to the queue, enter A" ;
cout << endl << " to land the next flight, enter L" ;
cout << endl << " to move a flight in the queue, enter M" ;
cout << endl << " to quit the program, enter Q" ;
cout << endl << " enter your choice " ;
cin >> holdCode ;
}
else
{
cout << " not a valid choice "<<endl;
}
holdCode = toupper(holdCod e) ; // convert to upper case
} // end function
//=============== =======addPlane =============== ===== =======
// task - add a new flight to the queue
// pre - given an array of flight numbers and number of flights in
array
// post - size of array is increased, new flight number added to end

void addPlane (int list[], int& lsize)
{ // start function
int newFlight ; // flight number to be added to queue
int w;
char pause ;
cout << endl << "Enter flight number to add : " ;
cin >> newFlight;

if (lsize == MAXQUEUE)
{ // start queue is full
cout << endl << "EMERGENCY! queue is full, cannot add flight" <<
newFlight ;
cout << endl << "press any key to continue" ;
cin.get(pause) ;
return ;
} // end queue is full
//list[lsize] = newFlight ; // add flight to end of list
lsize++ ; // increase list size

for (w = 0; w < lsize; w++)
if (newFlight == list[w])
return ;
} // end function
//=============== ======checkPlan e============== ===== ==========
// task - search the list of flights for a given flight number
// pre - given an array of flight numbers, number in array, and target
flight
// post - return queue position if target is found in array, else
return -1

int checkPlane (int list[], int lsize, int target)
{ // start function
int w ;
for (w = 0; w < lsize; w++)
if (target == list[w])
return w + 1;
return -1 ;
} // end function


any help or pointers would be very appreciated. thanks-

Joyce

Sep 14 '05 #1
1 2199
ae******@gmail. com wrote:
i've been tryin to do the following -
-Add a new flight number to the end of the queue (got it done)
-LAnd the plane at the front of the queue - problems wit it-
-display the queue - got it done
-seach for a specific flight number in queue ( didn't get there yet)
-move a flight number one one position in the queue to another ( didn't
get there yet)

this is what i have so far. it runs but something is wrong and i don't
know what it is.
The code is pretty good for what I presume is your first program. There
is quite a lot that could be improved but you'll learn about that as you
get more experienced.

Some miscellaneous comments follow.

#include <iostream>
#include <iomanip>
using namespace std ;

const int MAXQUEUE = 100 ; // maximum number of flights in the queue

void displayQueue (int[], int) ;
void getChoice ();
getChoice should return the choice (a char) to the main program, so the
above should be

char getChoice ();
void addPlane (int[], int&) ;
int checkPlane (int[], int, int) ;

int main ()
{
int queue[MAXQUEUE] ; // array of incoming flight numbers
int qsize = 0 ; // number of flights in the queue
char choice; // user's choice of next operation

do
{ // start menu loop
displayQueue (queue, qsize) ;
getChoice();
You use the variable choice for the user choice but you never assign to
it. Should be

choice = getChoice();

I guess the concept of a function return value is not something you've
fully grasped yet.
switch (choice)
{ // start switch
case 'S' : cout << "under construction" << endl; break ;
case 'L' : cout << "under construction" << endl; break ;
case 'A' : addPlane (queue, qsize) ; break ;
case 'M' : cout << "under construction" << endl; break ;
case 'Q' : cout << "program ended" << endl ;
} // end switch
} while (choice != 'Q'); // end menu loop

return 1 ;
}

//============dis playQueue====== ===============
// task - display the position and flight number of each flight in the
array
// pre - given an array of flight numbers and number of flights in the
array
// post - nothing

void displayQueue (int list[], int lsize)
{ // start function
int w ;
cout << endl << endl << endl ;
if (lsize == 0)
cout << "no flights awaiting landing at this time" << endl ;
else
{ // start listing
cout << "Queue size = " << lsize << endl << endl ;
cout << "Position Flight#" << endl ;
for (w = 0; w < lsize; w++)
cout << setw(5) << w + 1 << setw(12) << list[w] << endl ;
} // end listing
} // end function
The above looks fine

//============get Choice========= ===============
// task - obtain a choice code from the user
// pre - nothing
// post - a valid, uppercase code is returned
The comment is right, pity it doesn't reflect the code. This is a
serious problem is programming, it's very easy to kid yourself that a
piece of code does what you thought you wrote, not what you actually wrote.

void getChoice()
char getChoice()
{ // start function
char holdCode;
char pick;
if(pick == holdCode)
{
cout << endl << " to search for a flight number, enter S" ;
cout << endl << " to add a flight to the queue, enter A" ;
cout << endl << " to land the next flight, enter L" ;
cout << endl << " to move a flight in the queue, enter M" ;
cout << endl << " to quit the program, enter Q" ;
cout << endl << " enter your choice " ;
cin >> holdCode ;
}
else
{
cout << " not a valid choice "<<endl;
}
holdCode = toupper(holdCod e) ; // convert to upper case
} // end function
HUH??. I don't understand this pick and holdCode business at all. If you
look at pick in the above code is never gets assigned to at all. It is
an uninitialised variable and its value could be absolutely anything.

If you want to deal with bad choices you need a loop, get one input,
test if it is valid and loop round if it isn't, And dont forget the
function must return the choice made.

Some thing like this (this is pseudo code, you fill in the details)

char getChoice()
{
char code;
for (;;)
{
display choices to user
cin >> code;
if (valid code)
break;
cout << "not a valid choice\n";
}
return code; // don't forget this part!!
}


//=============== =======addPlane =============== ===== =======
// task - add a new flight to the queue
// pre - given an array of flight numbers and number of flights in
array
// post - size of array is increased, new flight number added to end

void addPlane (int list[], int& lsize)
{ // start function
int newFlight ; // flight number to be added to queue
int w;
char pause ;
cout << endl << "Enter flight number to add : " ;
cin >> newFlight;

if (lsize == MAXQUEUE)
{ // start queue is full
cout << endl << "EMERGENCY! queue is full, cannot add flight" <<
newFlight ;
cout << endl << "press any key to continue" ;
cin.get(pause) ;
return ;
} // end queue is full
//list[lsize] = newFlight ; // add flight to end of list
Why have you commented the above line out? It looks correct to me.
lsize++ ; // increase list size

for (w = 0; w < lsize; w++)
if (newFlight == list[w])
return ;
I don't understand what the above is doing (actually it is doing
nothing, but maybe it is work in progress).
} // end function
//=============== ======checkPlan e============== ===== ==========
// task - search the list of flights for a given flight number
// pre - given an array of flight numbers, number in array, and target
flight
// post - return queue position if target is found in array, else
return -1

int checkPlane (int list[], int lsize, int target)
{ // start function
int w ;
for (w = 0; w < lsize; w++)
if (target == list[w])
return w + 1;
return -1 ;
} // end function

Above looks fine, except why return w + 1, why not w? w is the position
in the array of the plane, and -1 means not found, that works, no need
for w + 1.

any help or pointers would be very appreciated. thanks-

Joyce


As I said before code seems pretty good. You got to get familiar with
returning values from functions, and you seriously messed up the
getChoice fucntion but apart from that, very good.

john
Sep 14 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
906
by: point | last post by:
Hi there.. I have the folowing array => property = hexonet => property = 2003-12-01 18:46:20.0 => property = hexonet => property = 2003-12-02 02:59:15.0 => property = hexonet => property = 2004-12-01 18:46:20.0
2
1565
by: Antti Nummiaho | last post by:
Consider the following javascript: var temp = new Array(new Array(0)) document.writeln(temp) temp = new Array(new Array(0,1)) document.writeln(temp) One would assume that it would print "0 0" that is the first elements of the arrays, but it prints "undefined 0". Why does temp return
8
2111
by: Gactimus | last post by:
I made the program below. It outputs the smallest number in the array. What I would like to know is how do I output the array location. I am at a loss. For example, since the smallest number in the array is 2, the output should be 2 for the number and 1 for the location. If anyone could help or point me in the right direction that would be great. Thanks. ------------------- #include <iostream>
7
25172
by: ritchie | last post by:
Hi all, I am new to this group and I have question that you may be able to help me with. I am trying to learn C but am currently stuck on this. First of all, I have a function for each sort (Bubble, insertion, selection..). I have an array of int's and am passing them to each sort function.
5
2217
by: ritchie | last post by:
Hi, I am writing to ask if anyone can see why my array is not being sorted correctly? It's an array of 4 elements(ints 1,2,3,4) but after calling the selection sort it comes back sorted as 1,1,2,4. I have narrowed it down to the sort function. I'm almost positive
8
10718
by: intrepid_dw | last post by:
Hello, all. I've created a C# dll that contains, among other things, two functions dealing with byte arrays. The first is a function that returns a byte array, and the other is intended to receive a byte array as one of its parameters. The project is marked for COM interop, and that all proceeds normally. When I reference the type library in the VB6 project, and write the code to call the function that returns the byte array, it works
3
8899
by: George | last post by:
Sub ExcelToListBox() Dim xRange As Object Dim ary Dim xValue As String xRange = oXL.Range("A1:A9") 'has letters A-H ary = xRange.value xValue = ary(3, 1) 'xValue = C Me.ListBoxPerson.Items.AddRange(ary)
3
3738
by: inkexit | last post by:
I need help figuring out what is wrong with my code. I posted here a few weeks ago with some code about creating self similar melodies in music. The coding style I'm being taught is apparently a lot different from what the pros around here use. I really need help with debugging some program errors more than anything, even though my coding style might not be perfect. Anyway here is my code. About the only things that work right are...
23
7416
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these in an array. The application compiles but aborts without giving me any useful information. What I suspect is happening is infinite recursion. Each Directory object creates an array of Subdirectories each of which has an array of...
5
1557
by: fluk | last post by:
Hi Guys, I hope someone can help me with this, because i'm getting crazy to find a good way to do that! This is what I got by querying a db. $arr1 = array("site", "description", "area1" , "activity1"); $arr2 = array("site", "description", "area1" , "activity2"); $arr3 = array("site", "description", "area2" , "activity2"); $arr4 = array("site1", "description1", "area3" , "activity2");
0
8995
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
9331
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9253
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8250
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6798
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3316
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2216
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.