Pointer Arrays

This is the pseudocode for the project:

Declare the following prototypes :

int sumSales(int list[], int size);
int winnerIndex(int list[], int size);

B. In the mainline :
Declare an int variable to store the number of salespersons.

Declare an int variable to store the total number of vehicle sales.

Declare a string pointer to be used to point to the array of salesperson names.

Declare an int pointer to be used to point to the array of salesperson sales.

Set manipulators so the sales percentages are outputted with 2 dec. positions

Prompt the user for number of salespersons and store the answer.

Set the pointers to contain the addresses of dynamic arrays(one array for the
salesperson names and the other to contain the sales for each salesperson)

Prompt the use to enter salesperson's name and the sales received by the
salesperson

Create a loop to accept(input) the user’s responses and store them in the
arrays

Call the sumSales function and store returned value

Output the heading line of the report

Output the detail lines of the report by creating a loop that loops through the
two arrays

Output the total line

Output the ‘winner’ line (see output below)

End program.



C. After the mainline :
Define the functions called within the mainline.
1. sumSales function :

Loop through the array containing the sales and add them up (See Example 8-6 in the textbook)

2. winnerIndex function :

Loop through the array containing the votes and determine the index of the element with the largest value (See Example 8-6 in the textbook)

This is what I have so far but I'm stuck and I'm sure what I have is wrong.

#include <iostream>
#include <iomanip>

using namespace std;

int sumSales(int list[], int size);
int winnerIndex(int list[], int size);

int main()
{
int people, vehicle;
string names[people];
int sales[people];
int* s;
string* n;

cout << fixed << setprecision(2);

cout << "How many sales persons are there? ";
cin >> people;
cout << endl;

for (int i = 0; i < people; i++)
{
cout << "Enter each sales person's name: " << endl;
cin >> names[i];
}

return 0;
}

you need to apply code tags.

immediate issues spotted without formatting and without a detailed question:
- people appears to be uninitialized. how many do you think you get for names[people] there?
- names[people] is illegal: variable sized arrays are not allowed in c++ though many compilers support them as an extension, its a bad practice.
- same for sales.
where is the dynamic memory allocation?
Are you aware of 'new' and 'delete' c++ keywords? You need to use them here.
in the raw that might look like
string * names = new string[people]; //this would come AFTER people has a value from user!
... code...
delete[] names;
though you may want to learn about smart pointers ASAP the raw ones will do for now.
Last edited on
My bad about the lack of info, but i appreciate the feedback. I've seen the 'new' and 'delete' keywords but not sure how to use them.

These are the main parts im stuck on.

Create a loop to accept(input) the user’s responses and store them in the
arrays

Declare a string pointer to be used to point to the array of salesperson names.

Declare an int pointer to be used to point to the array of salesperson sales.


I initialized the variables i missed. Where do i go from here. Im also not sure how the formatting works


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
#include <iostream>
#include <iomanip>

using namespace std;

int sumSales(int list[], int size);
int winnerIndex(int list[], int size);

int main()
{
    int people, vehicle;
    string names[people];
    int sales[people];
    int people;
    int sales;
    int* s;
    string* n;

    cout << fixed << setprecision(2);

    cout << "How many sales persons are there? ";
    cin >> people;
    cout << endl;
    
    string* names = new string[people];

    for (int i = 0; i < people; i++)
    {
        cout << "Enter each sales person's name: " << endl;
        cin >> names[i];
    }

    return 0;
}

int sumSales(int list[], int size)
{
    
}

int winnerIndex(int list[], int size)
{
    
}

Create a loop to accept(input) the user’s responses and store them in the
arrays

you had a perfect example of this:
1
2
3
4
5
for (int i = 0; i < people; i++)
{
cout << "Enter each sales person's name: " << endl;
cin >> names[i];
}

pointers .. you can use the [index] syntax from arrays just like above is fine.

----------------
Declare a string pointer to be used to point to the array of salesperson names.

also in good shape. you did this: is line 25 above.

-----------------------
Declare an int pointer to be used to point to the array of salesperson sales.
your turn. its just like the string pointer, you need to use the 'new' statement.
and every new requires a delete -- for now put these at the end of main before you return 0 and call it good enough.

find an online resource if you don't get new and delete.
the 2 cent version.. these keywords ask the OS for permission to use a section of ram (new!), and when done with it, tell the OS to take it back so some other program can use it (delete!).
pointers can be a little funky, but think of them as an array index into the giant array we call 'ram' and that may help you to make sense of it.
Last edited on
So I declared the int pointer to be used to point to the int array like i did for the string array and pointer. But now with the code i have, i get an error of declaring the same pointer twice but i dont know how else to write it. The lines where i wrote:

1
2
    s = sales;
    n = names;


I wrote these to contain the dynamic arrays but im pretty sure its part of the problem. Another issue is that the loop stops after i only enter one name. This is the full code again. after changing it a bit more from the last reply.

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
#include <iostream>
#include <iomanip>

using namespace std;

typedef int* intPtr;
typedef string* strPtr;

int sumSales(int list[], int size);
int winnerIndex(int list[], int size);

int main()
{
    int people, vehicle;
    string names[people];
    int sales[people];
    intPtr s;
    strPtr n;

    cout << fixed << setprecision(2);

    n = new string[people];
    s = new int[people];

    cout << "How many sales persons are there? ";
    cin >> people;
    cout << endl;

    s = sales;
    n = names;

    for (int i = 0; i < people; i++)
    {
        cout << "Enter each sales person's name: " << endl;
        cin >> names[i];
    }

    return 0;
}

int sumSales(int list[], int size)
{

}

int winnerIndex(int list[], int size)
{

}
Last edited on
you didnt read what I said... you are still reading in people after using it.
line 22 and 23 still use people before you read it in, which is at line 26!

here, I fixed it so you can see what I mean.
please do not make typedefs for simple things. if you want to do that, just use auto, eg:
auto foo = new int[100]; The reason ... imagine you wrote thousands of pages of code.
now someone wants to know what N is. N is a whoopitydo. Damn, that didn't help, what the heck is a whoopitydo? ... search again, oh, its really just an int pointer. Wastes time and effort etc for no good reason. Auto is also aggravating; I prefer you just call a thing what it really is unless there is a really good reason not to do that. If the * syntax bothers you, the good news is that c++ does not use pointers a lot anymore, so you won't have to deal with them a LOT.


int sumSales(int list[], int size);
int winnerIndex(int list[], int size);

int main()
{
    int people, vehicle;
    int *s{};
    string *n{};

    cout << fixed << setprecision(2);
	
	cout << "How many sales persons are there? ";
    cin >> people;
    cout << endl;

    n = new string[people];
    s = new int[people];   
    
    for (int i = 0; i < people; i++)
    {
        cout << "Enter each sales person's name: " << endl;
        cin >> n[i];
    }

for (int i = 0; i < people; i++)
	cout << n[i] << "\n";
    return 0;
}

int sumSales(int list[], int size)
{

}

int winnerIndex(int list[], int size)
{

}
Last edited on
So i have the whole code down i think but my only issue is that the parameters for the function dont match so do i change the parameters or do i need to change something else.

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
#include <iostream>
#include <iomanip>

using namespace std;

int sumSales(int list[], int size);
int winnerIndex(int list[], int size);

int main()
{
    int people, vehicle;
    int* s{};
    string* n{};
    int sum;
    int winner;

    cout << fixed << setprecision(2);

    cout << "How many sales persons are there? ";
    cin >> people;
    cout << endl;

    n = new string[people];
    s = new int[people];

    cout << "Enter each sales person's name and sales made: " << endl;
    for (int i = 0; i < people; i++)
    {
        cin >> n[i] >> s[i];
    }

    //sumSales(s[people], people);

    cout << "Salesperson   Sales Made" << endl;
    for (int i = 0; i < people; i++)
    {
        cout << left << setw(8) << n[i] << "         " << s[i] << endl;
    }
    cout << endl;
    cout << "Total              " << sum << endl;

    //winnerIndex(s[people], people);

    cout << "The winner of the monthly reward is " << winner;
    return 0;
}

int sumSales(int list[], int size)
{
    int sum;

    for (int i = 0; i < size; i++)
    {
        sum = sum + list[i];
    }
    return sum;
}

int winnerIndex(int list[], int size)
{
    int winner;

    for (int i = 0; i < size; i++)
    {
        if(list[i] > list[i + 1])
        {
            winner = list[i];
        }
    }
    return winner;
}


i commented out the functions in main because they dont work
Last edited on
they may work. its hard to say. you throw away the returned values!

//winnerIndex(s[people], people);

cout << "The winner of the monthly reward is " << winnerIndex(s, people);

or maybe
winner = winnerIndex(s, people);
s[people] is 1) an integer and 2) out of bounds (10 items in an array are 0-9, 0,1,2,3,4,5,6,7,8,9 how many numbers are in that list??)
the function does not want an integer, it wants an array. s is an array.

Topic archived. No new replies allowed.