Parallel Arrays

Hi everyone, I have this problem that I need help with and I feel like I should know how to do it as the instructions were super clear, I just dont think im sure about where to start and if im even doing this right. After hours of googling and trying to understand I still dont understand how to break down the arrays and to check whether the input for the letter code is correct. Im still fairly new to all of this and these are new concepts to me

"You are to write a program which inputs the base price of a car. Then, the program will input in a 2-letter code (a string) which corresponds to an option package.

All the 2 letter codes will be in an array (string), along with the corresponding cost of the package in a parallel array (doubles), and there is also another array with the name of the package (string).

Here is the data for the 3 parallel arrays:

OptionPackageCodeArray PackageCostArray PackageNameArray
BB 1500.00 Base
SP 3250.00 Sport
NP 4575.00 Intermediate
HE 7500.00 Luxury
UC 5220.00 User specified



Your program first has to determine if the 2-letter code input by the user is valid. You are required to use a loop to check the input against each item in the optionPackageCodeArray. Begin with the first item then "walk" down through the array. If the code does NOT exist on the list, display an error message and stop. If the code is valid, do some calculation and displaying. First, add the base price to the package cost from the array and get a subtotal. Then, calculate 15% of the subtotal for taxes and fees and add that to the subtotal for a final price. Display the final price and the full name of the package from the last array, then stop.

This question was previously asked on the forum but I also did not understand that
I know I have to do this so far:

string OptionPackageCodeArray [5 ] { “BB”, “SP”. “NP”, “HE”, “UC”}
string PackageNameArray [5 ] {“Base”, “Sport”, “Intermediate”, “Luxury”, “User specified”}
double PackageCostArray [5 ] { 1500.00, 3250.00, 4575.00, 7500.00, 5220.00}

for (i=0, i<5, i++)


The next parts are kind of where im stuck
Last edited on
You'll be able to do the "calculations and displaying" since my linguistic skills are kind of lacking in this sphere. :D

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

int main()
{
	std::string optionPackageCodeArray[5] = { "BB", "SP", "NP", "HE", "UC" };
	std::string packageNameArray[5] = { "Base", "Sport", "Intermediate", "Luxury", "User specified" };
	double packageCostArray[5] = { 1500.00, 3250.00, 4575.00, 7500.00, 5220.00 };

	std::string userInput;

	std::cout << "Enter the 2-letter code: ";
	std::getline(std::cin, userInput);

	bool isValid = false;

	for (int i = 0; i < 5; i++)
	{
		if (userInput.compare(optionPackageCodeArray[i]) == 0)
		{
			isValid = true;
			//Your calculations go here. The current "i" represents the same position 
			//in all the arrays. So "BB" goes together with "Base" which goes together with "1500.0"
			//You can access them by doing "packageCostArray[i] = ..." 
		}
	}

	if (isValid == false)
	{
		std::cout << "Bad input!";
	}

	std::cin.get();
	return 0;
}
Last edited on
Thank you first of all for replying!! :)
I have this so far but I have errors and I know i'm still lacking

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

int main()
{
double basePrice = 0.0;
double subTotal = 0.0;
double finalPrice = 0.0;
std::string optionPackageCodeArray[5] = { "BB", "SP", "NP", "HE", "UC" };
std::string packageNameArray[5] = { "Base", "Sport", "Intermediate", "Luxury", "User specified" };
double packageCostArray[5] = { 1500.00, 3250.00, 4575.00, 7500.00, 5220.00 };


cout << "Enter Base Price for Car:"
cin >> basePrice

std::string userInput;

std::cout << "Enter the 2-letter code: ";
std::getline(std::cin, userInput);

bool isValid = false;

for (int i = 0; i < 5; i++)
{
if (userInput == optionPackageCodeArray[i])
{
isValid = true;

subTotal = basePrice + packageCostArray [i];
finalPrice = subTotal * .015;
//Your calculations go here. The current "i" represents the same position
//in all the arrays. So "BB" goes together with "Base" which goes together with "1500.0"
//You can access them by doing "packageCostArray[i] = ..."

//And would my final displayed result go here as well?

}
else
{
isValid = false;
}
}

if (isValid == false)
{
std::cout << "Bad input!";
}

system ("pause");
std::cin.get();
return 0;}

I debugged what I had before putting the base price input and it worked up to the point where it did not recognize the Package code, if I tried any of them it just told me I had a bad input
And how do you reply in code?
Last edited on
You were pretty much almost done. You have to double check that you've put all the semi-colons (;) where they should be. Those were most of your errors. Also, there must NOT be an "else" which sets isValid to false. isValid is initialized to false when it is declared. So once it becomes true it mustn't become false again... which is what was happening.

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
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

int main()
{
	double basePrice = 0.0;
	double subTotal = 0.0;
	double finalPrice = 0.0;
	string optionPackageCodeArray[5] = { "BB", "SP", "NP", "HE", "UC" };
	string packageNameArray[5] = { "Base", "Sport", "Intermediate", "Luxury", "User specified" };
	double packageCostArray[5] = { 1500.00, 3250.00, 4575.00, 7500.00, 5220.00 };


	cout << "Enter Base Price for Car: ";
	cin >> basePrice;
	cin.ignore(); //this removes what you just entered from the stream, because otherwise it would go 
//into the getline(cin, userInput); and it would cause unwanted results.

	string userInput;

	cout << "Enter the 2-letter code: ";
	getline(cin, userInput);

	bool isValid = false;

	for (int i = 0; i < 5; i++)
	{
		if (userInput == optionPackageCodeArray[i])
		{
			isValid = true;

			subTotal = basePrice + packageCostArray[i];
			finalPrice = subTotal * .015;

			cout << "Package: " << packageNameArray[i] << endl << "Final price: " << finalPrice;
			//All done i think

		}
	}

	if (isValid == false)
	{
		cout << "Bad input!";
	}

	//cin.ignore(); Don't use system("pause"); it's not good, use those two lines instead
	cin.get();
	return 0;
}
Last edited on
Whoops. Did not see this reply. That looks a lot more simple than what I ended up with
Topic archived. No new replies allowed.