473,386 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 ;
}

//============displayQueue=====================
// 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

//============getChoice========================
// 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(holdCode) ; // 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
//=====================checkPlane=================== ==========
// 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 2179
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 ;
}

//============displayQueue=====================
// 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

//============getChoice========================
// 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(holdCode) ; // 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
//=====================checkPlane=================== ==========
// 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
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 =>...
2
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...
8
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...
7
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...
5
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...
8
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...
3
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...
3
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...
23
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...
5
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" ,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.