Putting a struct array.quantity into a new int array

Part of my assignment is to sort the remaining quantities of sodas from highest to lowest and display. I can print out tables no problem, but I'm having issues transferring from my sodaMenu[].quantity values into a new int array quantityArray[]. int maxValue was previously found in an earlier function and it is the basis for finding the highest value, then the next highest, then the next highest... until I have an array sorted the way I need it and then I can print it to the user. Seemed simple enough but I get C2059 errors all over it. I googled some answers and checked these forums but coding is very new to me. I have zero experience but I have a degree in Biology so I'm not uneducated just inexperienced. My TAs said I'm doing this the hard way but won't offer a simpler method. If I need to I'll post the whole code, but I think that was in the rules as a no no. Any help is appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  void sortQuantityArray(sodaMenu[], int quantityArray[], int maxValue)
{ /* function sortQuantityArray */
	int i; // counter variable for this for loop
	for (i = 0; i < maxValue; i++)				// this series of loops will add to the array in order of greatest to least
	if (sodaMenu[i].quantity == maxValue)
		quantityArray[0] = sodaMenu[i].quantity;
	for (i = 0; i < quantityArray[0]; i++)
	if (sodaMenu[i].quantity <= quantityArray[0])
		quantityArray[1] = sodaMenu[i].quantity;
	for (i = 0; i < quantityArray[1]; i++)
	if (sodaMenu[i].quantity <= quantityArray[1])
		quantityArray[2] = sodaMenu[i].quantity;
	for (i = 0; i < quantityArray[2]; i++)
	if (sodaMenu[i].quantity <= quantityArray[1])
		quantityArray[3] = sodaMenu[i].quantity;
	for (i = 0; i < quantityArray[3]; i++)
	if (sodaMenu[i].quantity <= quantityArray[1])
		quantityArray[4] = sodaMenu[i].quantity;
	for (i = 0; i < quantityArray[4]; i++)
	if (sodaMenu[i].quantity <= quantityArray[1])
		quantityArray[5] = sodaMenu[5].quantity;
} /* function sortQuantityArray*/
MSDN wrote:
C2059: syntax error

To determine the cause of the error, examine not only the line that's listed in the error message, but also the lines above it.

You did not tell the line nor erroneous token, but a function parameter that lacks type is the first error.

You should stare at the indices of the quantityArray too. Nested loops could avoid such logical errors.


Now, your function writes to 6 elements of the quantityArray. How do we know that the array has that many elements? This function cannot be called with smaller arrays and that severely decreases genericity.

Parameter maxValue seems to be the size of the sodaMenu array in the first loop. However, let me show a simple array:
int amount[] = { 42, 7 };
Two elements and the maxValue is 42. Your function would not handle input of that type. Then again, does it have to?


You apparently have to fill the quantityArray with something. With what? Quantities? What happens, if you simply copy the values without sorting? Can you sort a simple integer array? Dividing the task into smaller and simpler subtasks could avoid some errors too.
I'll have to paste in pieces I guess. I have a const int of 6 as that is the max number of types of sodas in this vending Machine example. The maximum quantity is 20 and is also a const int. I'm trying to fill quantityArray with the highest quantity found in sodaMenu[].quantity (which is an array of structs) in descending order <as that is defined in our project assignment>. I have a function to find the highest quantity value in the struct array which becomes int maxValue which will be used to find the next highest and so on. It seems like a simple problem but I have no answers. I'm posting my total code in pieces so you can see this:

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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/* This program will simulate a vending machine program in a semi-real world application.  It is to read from a text file "machine.txt" and store the information
into an array of structures <structs>.  The program will enter a loop and interactively display the names and prices of the drinks as well as an option to quit.
The user will select a drink or quit.  If any invalid input is detected it must be alerted to the user and wait until proper input is recieved.  All input must be
echoprinted.  If a drink is selected, ask the user to "insert" $1.00 and display the "change" as well as decrement the quantity of the soda selected.  If the
quantity was zero, display SOLD OUT and allow the user to try again.  When the quit option is chosen, display the final values of all structs showing name, price,
and quantity.  Also show the profit as total cans sold multiplied by $0.25. Finally, an array must be displayed showing the sodas organized by highest to lowest in
quantity of cans.*/

#include <iostream>		// Header Files
#include <fstream>		
#include <string>
#include <iomanip>
using namespace std;	// Name space

struct sodaInfo			// the qualities of each soda
{
	string name;
	double price;
	int quantity;
};

const int NUM_SODAS = 6; // number of types of soda
const double DOLLAR = 1.00; // amount of cash inserted every time
const double PROFIT = 0.25; // amount of profit per can sold
const int MAX_CANS = 20; // maximum number of cans that can be held per machine

typedef sodaInfo sodaMenu[NUM_SODAS];		// defining the array sodaMenusodaMenu[NUM_SODAS];	// declaring the struct sodaMenu
int quantityArray[NUM_SODAS];				// defining the array for sorting quantities

void storeData(sodaMenu, ifstream &);	// function will read the file and put data into an array of structs
void displayMenu(sodaMenu); // function will enter a do while loop to display names and prices of sodas from the struct array and await user input
int userChoice(sodaMenu, int); // function will displayMenu for the user to interactively decide sodas 1-6 or 0 which quits and prints a final report
void showProfit(int); // function will calculate the total profit with cansSold * PROFIT
void buildQuantityArray(sodaMenu, int quantityArray[], int); // function will use sodaMenu  to build new array of just quantities to be sorted
int findMaxValue(int quantityArray[NUM_SODAS]);  // function will find the maximum number to sort the new array
void sortQuantityArray(sodaMenu, int quantityArray[], int); // sorts the array of quantities into highest to lowest.
void finalStatus(sodaMenu); // this function shows the names, prices, and quantities of the machine at the end of the iteration.
int findMax(sodaMenu);	     // one dimensional array sorting from highest to lowest in quantity of the sodas sold, and then a final table showing
							// the soda name, quantity, and price.


int main()
{
	ifstream sodaList;		// declaring the list of names, prices, and quantities of sodas.  Naming it SodaList.open("machine.txt");		
	sodaList.open("C:\\Users\\Dustin\\Documents\\Programming Proj ects\\vendingMachine\\vendingMachineP6"); // hard copying file location
    sodaMenu machineList; // defining the array as machineList
	int userInput;	// the integer showing the choice of the user
	int cansSold = 0;	// the running quantity that will grow with each soda purchased to be used in calculating the total profit.
	int maxValue = 0; // final maxvalue
	if (sodaList)	// checking to see if file loaded properly
	{
		cout << "File successfully open\n";	// let the user know the file opened properly
		storeData(machineList, sodaList);   // put the data into the array of structs
		do
		{
			displayMenu(machineList);			// print the initial data of the vending machine in a do while loop and await user input
			userInput = userChoice(machineList, cansSold);	// switch that lets the user interactively choose a soda, display change, change quantities, and quit.
		} while (userInput != 0);
		if (userInput == 0)
			quantityArray[NUM_SODAS];
			showProfit(cansSold);				// calling function to show profit
			buildQuantityArray(machineList, quantityArray, cansSold);		// calling function to transfer quantity info from sodaList to new array quantityArray[]
			findMaxValue(quantityArray);			// calling function to find the maximum value
			sortQuantityArray(machineList, quantityArray, maxValue);		// calling function to sort the quantityArray[] into highest to lowest
			finalStatus(machineList);			// finally display a table showing the final results of names, prices, and quantities in machine
		sodaList.close();				  // close the file
		cout << "Terminating Program...";
		cin.get();
	}
	else
	{
		cout << "\nError opening file!\n";  // 
	}
	return 0;
}

void storeData(sodaMenu sodaMenu, ifstream &sodaList)
{ /* function storeData */
	for (int i = 0; i < NUM_SODAS; i++)
	{
		getline(sodaList, sodaMenu[i].name);
		std::setprecision(3);
		sodaList >> sodaMenu[i].price;
		sodaList >> sodaMenu[i].quantity;
		sodaList.ignore(10, '\n');
	}
} /* function storeData */

void displayMenu(sodaMenu sodaList)
{ /* function displayMenu */
	int i;			// counter to allow additional sodas to be added if needed.  It will print line by line the name and price of each soda.
	cout << endl;
	cout << setfill('*') << setw(25);
	cout << "Thirsty?  Try your refreshing soda today!\n";
	cout << setw(25) << setfill(' ');
	for (i = 0; i < NUM_SODAS; i++)  // enter loop to print NUM_SODAS names and prices for the user to select.
	{
		cout << "[" << (i + 1) << "] " << sodaList[i].name << setw(5) << sodaList[i].price << endl;
	}
	cout << "[0] Quit";
} /* function displayMenu */

int userChoice(sodaMenu sodaList, int cansSold) // sending the array and the cansSold which starts at 0.
{ /* function userChoice */
	int userInput = -1; // initializing the user choice as NOT 0, or 1-6
	cin >> userInput;	// user inputs their choice
	if (userInput == 0)
		cout << "You have chosen 0!  Quitting vending operations...";
	else if (userInput >= 1 && userInput <= 6)
	{
		if (sodaList[userInput - 1].quantity > 0) // checking to make sure the quantity is there
		{
			cout << "Please insert $1.00 -=-=-=-=- $$$ Dollar inserted";  // pretending the user is inserting $1.00
			cout << "You have chosen 1! " << sodaList[userInput-1].name << "!\n";   // echoprinting the user's decision
			cout << "Your change is " << std::setprecision(3) << (DOLLAR - sodaList[userInput-1].price) << "!";  // calculating the change.
			sodaList[userInput-1].quantity--; // removing one from the quantity of sodas in the appropriate array
			cansSold++;		// add one to number of cans sold
		}
		else
			cout << "\nSorry! We are SOLD OUT! Please select another choice!\n"; // letting user know the machine is sold out of this soda
	}
		cout << "\nInvalid Entry, please enter a value 0-6.";		// value was not an integer 0-6 so it is invalid
		return userInput;		// letting main function know what soda was selected
} /* function userChoice */

void showProfit(int cansSold)
{ /* function showProfit */
	int totalCansSold = cansSold;
	cout << "\n The total profit for this business day is " << totalCansSold * PROFIT << ".\n";
} /* function showProfit */

void buildQuantityArray(sodaMenu machineList, int quantityArray[])
{ /* function buildQuantityArray */
	int i;
	for (i = 0; i < NUM_SODAS; i++)
		quantityArray[i] = machineList[i].quantity;
} /* function buildQuantityArray */
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
int findMaxValue(int quantityArray[NUM_SODAS])
{ /* function findMaxValue */
	int maxValue;
	int highestValueSoFar = 0; // temporary variable to hold largest value seen so far
	int i;	// simple counter
	for (i = 0; i < NUM_SODAS; i++)
	{
		if (highestValueSoFar < quantityArray[i])
			highestValueSoFar = quantityArray[i];
	}
	maxValue = highestValueSoFar;
		return maxValue;
} /* function findMaxValue */

void sortQuantityArray(machineList, int quantityArray[], int maxValue)
{ /* function sortQuantityArray */
	int i; // counter variable for this for loop
	for (i = 0; i < maxValue; i++)				// this series of loops will add to the array in order of greatest to least
	if (machineList[i].quantity == maxValue)
		quantityArray[0] = sodaMenu[i].quantity;
	for (i = 0; i < quantityArray[0]; i++)
	if (sodaMenu[i].quantity <= quantityArray[0])
		quantityArray[1] = sodaMenu[i].quantity;
	for (i = 0; i < quantityArray[1]; i++)
	if (sodaMenu[i].quantity <= quantityArray[1])
		quantityArray[2] = sodaMenu[i].quantity;
	for (i = 0; i < quantityArray[2]; i++)
	if (sodaMenu[i].quantity <= quantityArray[1])
		quantityArray[3] = sodaMenu[i].quantity;
	for (i = 0; i < quantityArray[3]; i++)
	if (sodaMenu[i].quantity <= quantityArray[1])
		quantityArray[4] = sodaMenu[i].quantity;
	for (i = 0; i < quantityArray[4]; i++)
	if (sodaMenu[i].quantity <= quantityArray[1])
		quantityArray[5] = sodaMenu[5].quantity;
} /* function sortQuantityArray*/

void finalStatus(sodaMenu sodaList, int quantityArray[])
{ /* function finalStatus */
	int i;			// counter for soda types
	cout << endl;
	cout << setfill('*') << setw(25);
	cout << "END OF BUSINESS TRANSACTIONS.  SUMMARY FOLLOWS:\n";
	cout << setw(25) << setfill(' ');
	for (i = 0; i < NUM_SODAS; i++) // loop to print NUM_SODAS name, prices, and quantities of the final array
	{
		cout << endl << sodaList[i].name << setw(3) << sodaList[i].price << setw(3) << sodaList[i].quantity << endl;
	}
} /* function finalStatus */
Topic archived. No new replies allowed.