Logical Errors with Arrays.

This program is now fixed for syntax errors.
Now I am having issues with the arrays.
When the program starts, all the slots for the inventory are filled with 0s.
When I go to add item, it won't show.
I hate arrays right now haha.
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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/************************************************
Array Sample
*************************************************/

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

//******* Global Parallel Arrays *********
const int carArraySize = 5;

string carMakeArray[carArraySize];
string carModelArray[carArraySize];
int carYearArray[carArraySize];
double carPriceArray[carArraySize];

//******* Function Prototypes ***********
void initArrays();
void displayArrays();
void addToArrays();
void removeFromArrays(int);

int main()
{
	int choice = 0;
	int arraySubscript = 0;

	initArrays();

	do
	{
		system("cls");
		displayArrays();
		cout << endl << endl;
		cout << "==== Car Inventory Options ====" << endl << endl;
		cout << "1. Add to Inventory" << endl;
		cout << "2. Remove from Inventory" << endl;
		cout << "3. End Program" << endl << endl;
		cout << "Please make a selection: ";
		cin >> choice;

		switch(choice)
		{
		case 1:
			addToArrays();
			break;
		case 2:
			system("cls");
			displayArrays();

			cout << endl << endl;
			cout << "Please enter the Index number of the car to remove: ";
			cin >> arraySubscript;
			
			removeFromArrays(arraySubscript);
			break;
		case 3:
			return 0;
		default:
			cout << "Invalid Choice!" << endl;
			system("pause");
		}
	}while(true);

}

/**********************************************
Function: initArrays
Input:
Output:

Description:
Initializes the global parallel arrays
**********************************************/

void initArrays()
{
	for(int i = 0; i < carArraySize; i++)
	{
		carMakeArray[i] = "";
        carModelArray[i] = "";
		carYearArray[i] = 0;
		carPriceArray[i] = 0.0;
	}
	return;
}

/**********************************************
Function: displayArrays
Input:
Output:

Description:
Displays the global parallel arrays and allows basic operations.
**********************************************/

void displayArrays()
{
	system("cls");
	cout << "              ================= Car Inventory ====================" << endl << endl;
	cout << " Index     Year        Make                    Model                    Price" << endl;
	cout << "-----------------------------------------------------------------------------" << endl;
		
	for(int i = 0; i < carArraySize; i++)
	{
		if(carMakeArray[i] == "")
			cout << " " << i << ". " << "\t" << carYearArray[i] << "\t" << carMakeArray[i] << "\t" << carModelArray[i] << "\t" << setw(10) << carPriceArray[10] << endl;
	}

	return;
}

/**********************************************
Function: addToArrays
Input:
Output:

Description:
Allows additions to the global parallel arrays
**********************************************/

void addToArrays()
{	
	system("cls");

	for(int i = 0; i < carArraySize; i++)
	{
		if(carMakeArray[i] == "")
		{
			cout << "==== Add to Car Inventory ====" << endl << endl;
			cout << "Car Year: ";
			cin >> carYearArray[i];
			cout << "Car Make: ";
			cin >> carMakeArray[i];
			cout << "Car Model: ";
			cin >> carModelArray[i];
			cout << "Car Price: ";
			cin >> carPriceArray[i];
			return;
		}
		else
		{
			cout << "Not Enough Array Space!" << endl;
			system("pause");
			return;
		
	}
}
}

/**********************************************
Function: removeFromArrays
Input:
Output:

Description:
Allows removal from the global parallel arrays.
**********************************************/

void removeFromArrays(int subscriptFromMain)
{
	carMakeArray[subscriptFromMain] = "";
    carModelArray[subscriptFromMain] = "";
	carYearArray[subscriptFromMain] = 0;
	carPriceArray[subscriptFromMain] = 0.0;

	for(int i = 0; i < carArraySize - 1; i++)
	{
		if(carMakeArray[i] != "")
		{
			carMakeArray[i] = carMakeArray[i + 1];
			carModelArray[i] = carModelArray[i + 1];
			carYearArray[i] = carYearArray[i + 1];
			carPriceArray[i] = carPriceArray[i + 1];

			carMakeArray[i + 1] = "";
			carModelArray[i + 1] = "";
			carYearArray[i + 1] = 0;
			carPriceArray[i + 1] = 0.0;
		}
	}
}
Last edited on
Lines 127-130 hide the global arrays.
Thanks... Have no idea why it was there.
Still puts out 0's though. =[
Last edited on
See line 108.
Helios, what should I do...
I get it, I know whats wrong but I just can't think of how to fix it for some reason.
If you can't figure out how to fix it, it's because you don't get it and you don't know what's wrong. Look very carefully at the line I pointed out, see what it's doing in its context.
I've already done too much by doing half the debugging for you. If I did more, you wouldn't learn anything.
hehehehe... :D
ah. a guessing game. i won't ruin it for the two of you.

here is another clue:
there is no problem in line 136, but if you comment-out (remove) line 136, then numbers show up. try it:
 
// cin >> carMakeArray[i]; 

except, line 136 isn't the problem. line 136 is a good line. restore line 136, then fix the real problem in line 108.


there are more problems after you fix this display-only-0 thing. this game may take a while. i don't think helios is going to let you play outside today.
Topic archived. No new replies allowed.