Program help!!!

i have a program that i need to do for an assignment and i am stuck on it.


Here is an exchange sort algorithm written in pseudo code:
Exchange Sort of Array X[]
For I = 0 to (number of array elements - 2), increment by 1
For J = (I + 1) to (number of array elements - 1), increment by 1
If X[I] is greater than X[J] then
temp = X[I]
X[I] = X[J]
X[J] = temp
Next J
Next I
Write a program to define a simple structure for the following information:
last name
sales (float)
Now declare an array of the structure, reserving space for at least 10 elements.
The program should request data from the keyboard to populate (put data in) the
array. You may use "quit" as a last name code value to indicate that data input is
finished. The array should now be sorted so that the element with the highest sales
figure is first and the element with the lowest sales figure is last (adapt the
exchange above). Output the sorted array to the screen.
To compare the entered last name to "quit" do the following:
#include <string.h>
in your program:
if( strcmp( record field, "quit" ) == 0 ) break;
where record field is the name of your last name string
(example: entry[i].lastname


the code that i have is:

#include <iostream>

using namespace std;

int e;

struct record
{
char lastName[15];
float sales;
}
entry[100];

void MakeEntry (int e )
{
cout << "enter last name: ";
cin >> entry[e].lastName;
cout << "enter sales: ";
cin >> entry[e].sales;
}

void SortEntries ( int I )
{
int entry[] = {e};
int J,temp;
int n = 10;

for(I=0; I<n; I++)

{
for(J = 0; J < n - 1; J++)
{
if(entry[J] > entry[J+1])
{
temp=entry[J];
entry[J]=entry[J + 1];
entry[J + 1]=temp;
}
}
}
cout<<"Sorted Entries:\n";

for(I=0; I<n; I++)
{
cout<<entry[I]<<"\n";
}
}

void ListEntries (int t)
{
int r;
for (r = 0; r <= t; r++)
{
cout << entry[r].lastName << endl;
cout << entry[r].sales << endl;
}

}

int main()
{
int t, sel;
t = 0;
sel = 0;
while (sel != 4)
{
cout << "main menu" << endl << endl;
cout << "1. Address Book Entry" << endl;
cout << "2. List Address Book Entries" << endl;
cout << "3. Sorted Entries" << endl;
cout << "4. Exit Program" << endl << endl;
cout << " enter your selection: ";
cin >> sel;

if (sel == 1)
{
MakeEntry (t);
t++;
}

if (sel == 2)
{
ListEntries (t - 1);
}

if (sel == 3)
{
SortEntries(t - 2);
}
}
return 0;
}


I am totally lost!!
Without any input error handling
(we assume that all input is correct and well formed eg. if we ask for a number, the user enters a number etc.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <iomanip>

struct record
{
    static const int NAME_SZ = 15 ;
    char lastName[NAME_SZ];
    double sales;
};

void MakeEntry( record recs[], int pos ) // add an entry at position 'pos' in the array
{
    std::cout << "enter last name (max " << record::NAME_SZ-1 << " chars): ";
    
    // std::setw(record::NAME_SZ): accept a maximum of  NAME_SZ-1 characters 
    // avoid undefined behaviour from out of range access to array 
    std::cin >> std::setw(record::NAME_SZ) >> recs[pos].lastName ;

    std::cout << "enter sales: ";
    std::cin >> recs[pos].sales;
}

void ListEntries( const record recs[], int num_recs )
{
    for( int r = 0; r < num_recs ; ++r )
        std::cout << r << ". " << recs[r].lastName << ' ' << recs[r].sales << '\n' ;
}


void SortEntries( record recs[], int num_recs  )
{
    for( int i = 0 ; i < num_recs ; ++i )
    {
        for( int j = i+1 ; j < num_recs ; ++j )
        {
            if( recs[i].sales > recs[j].sales ) // invariant: i < j
            {
                // sales of recs[i] is greater than sales of recs[j] => interchange their positions
                const record temp = recs[i] ;
                recs[i] = recs[j] ;
                recs[j] = temp ;
            }
        }
    }

    std::cout << "\nSorted Entries:\n";
    ListEntries( recs, num_recs ) ;
}


int main()
{
    const int MAX_RECS = 100 ; // upper limit on number of records
    record recs[MAX_RECS] ;
    int num_recs = 0 ; // number of records actually entered

    int sel = 0;
    while( sel != 4 )
    {
        std::cout << "\n\nmain menu\n\n"
                  << "1. Address Book Entry\n"
                  << "2. List Address Book Entries\n"
                  << "3. Sort Entries\n"
                  << "4. Exit Program\n\n"
                  << " enter your selection: ";
        std::cin >> sel;

        if( sel == 1 )
        {
            if( num_recs < MAX_RECS )
            {
                MakeEntry( recs, num_recs ) ;
                ++num_recs ;
            }
            else std::cout << "the list is full\n" ;
        }

        else if( sel == 2 ) ListEntries( recs, num_recs ) ;

        else if (sel == 3) SortEntries( recs, num_recs ) ;

        else if( sel != 4 ) std::cout << "invalid selection\n" ;

        // else sel==4 ; do nothing
        // (the loop condition is false; so it will exit on its own)
    }
}
I think the only headers i can use are #include <iostream> and #include <string.h>
I took what you put and tried to make it in the way that they want it. is there anyway to get this to work. I am getting an error of (invalid converstion from 'record' to 'int')

#include <iostream>

using namespace std;


struct record
{
static const int Name_size = 15;
char lastName[Name_size];
double sales;
};

void MakeEntry (int e, record entry[] )
{
cout << "enter last name: ";
cin >> entry[e].lastName;
cout << "enter sales: ";
cin >> entry[e].sales;
}

void ListEntries (int t, const record entry[])
{
int r;
for (r = 0; r <= t; r++)
{
cout << entry[r].lastName << endl;
cout << entry[r].sales << endl;
}

}

void SortEntries ( record entry[], int I )
{
for( int i = 0 ; i < I ; ++i )
{
for( int j = i+1 ; j < I ; ++j )
{
if( entry[i].sales > entry[j].sales )
{

const record temp = entry[i] ;
entry[i] = entry[j] ;
entry[j] = temp ;
}
}
}

cout<<"Sorted Entries:\n";
ListEntries (I, entry);

}


int main()
{
const int MAX_RECS = 100 ;
record entry[MAX_RECS] ;
int b = 0 ;

int sel = 0;
while( sel != 4 )
{
std::cout << "\n\nmain menu\n\n"
<< "1. Address Book Entry\n"
<< "2. List Address Book Entries\n"
<< "3. Sort Entries\n"
<< "4. Exit Program\n\n"
<< " enter your selection: ";
std::cin >> sel;

if( sel == 1 )
{
if( b < MAX_RECS )
{
MakeEntry( entry, b ) ;
++b ;
}
else
{
cout << "the list is full\n" ;
}
}

else if( sel == 2 )
{
ListEntries( entry, b ) ;
}

else if (sel == 3)
{
SortEntries( entry, b ) ;
}

else if( sel != 4 )
{
cout << "invalid selection\n" ;
}

}
}
listentries appears to be called with its parameters reversed?
Will switching them help with the program or will it still be the same?
Thank you all so so much. Switching those did make the program work. I was looking at the program and finally got to switching them and it came back with no error.
they have to be in the order you gave it when you wrote the code. you simply cannot swap them, that was the cause of it not working!!

Keep this in mind, because if you get the order wrong on some types, it will "work" but give the wrong output because you scrambled its data.

for example,

double compute(double x, int y)
{
return x/y;
}

...

int a,b;
double d;
a = 1; b = 2; d = 3.14;

compute (a,d); fine, returns a/d
compute (d,a); fine, returns d/a
compute (a,b); also fine.

it would be easy to mangle your code if you needed d/a and got a/d though!

Topic archived. No new replies allowed.