Having a problem with arrays.

Hey all.

My program calls a funtion which adds 10% tax onto a number that is given by the user. The function returns that value and then it gets printed thats easy.

However I need the user to be able to then enter another price that gets sent to the function and returned. Then I need the program to add all of the values up and print them out.

I am assuming I need to use an array for this. But im not sure where to put the array. Here is the relivant code.

This is the call to my funtion
1
2
3
4
5
6
7
8
9
10
11
12
13
14
else
{
	cout << "";		
	cout << "Please Enter Pre-tax Value: ";
	cout << "";
	cin >> pre_tax_value;
	cout << "" << endl;

	complete_tax_value = tax_Calculator (pre_tax_value); // Call my Tax Calculator Function

	cout << "Item: " << item_Description << " | Pre-Tax Price: $" << pre_tax_value << " | After-Tax Price: $" << complete_tax_value << endl;
	cout << "********************************************************************************" << endl;
	cout << "" << endl;
}


This is my function
1
2
3
4
5
6
7
8
9
double tax_Calculator (double pre_tax_value)
{
	double tax;
	double complete_Value;
	tax = pre_tax_value * 10 / 100;
	complete_Value = tax + pre_tax_value;

	return (complete_Value);
}


Any help is much appreciated thankyou

u just need a while dont you? it will check if the number u entered is the one you want your program to stop with.

double eachValue;
double sum;
double pre_tax_value;
sum=0;
cin>>pre_tax_value;

while(pre_tax_value!="the number you want your program to stop with") // for example -1
{
eachValue=tax_Calculator(pre_tax_value);
sum+=eachValue;
cout<<"give another number to calculate its tax"; cin>>pre_tax_value;
} //while


in that case though, you have to check the number you enter with an if.
for example: in this case the number (you want to calculate its tax) cant be a negative number as u cant have taxes from negative numbers.. its a logic fault. so "-1" isnt that corrent in this case. You could use instead a big number for example 9999. and declare you variable as unsigned double (unsigned variables can only be >=0)

while (pre_tax_value!=9999) {....
Last edited on
I wasn clear. I dont want the program to stop when a certain number is entered. The program is a Retail Shop program. You enter and Item give it a value then enter another item. I need it to add all those items up.
You dont know how many of those numbers u have to enter to get their tax?
The program has to add up all the entered After Tax Values. There are infinite number of Entries. So i need my tax function to return individual After Tax Values and store them in an array. So I can then add up all the different values in my array.

Does that make more sense? Sorry im trying to be as descriptive as i can.
the method i posted above does that..
sum variable has the total cost you're talking about.
you can pick a number for your while that will never be one of those numbers you enter and your while will never stop.
you can print your sum with a cout<<"sum is:"<<sum<<endl; inside your while everytime u insert another number and thats all you can do if you dont know the excact number of your entries or if your program never stops.

i dont think you can use an array as you dont know the number of the values you will insert, as u said they will be infinite. i might be wrong though.

whats the code before that else?
Last edited on
Sorry for taking so long to 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

	while (item_Description != "end" || item_Description != "End")
	{
		cout << "***************************" << endl;
		cout << "Please Enter Item Description: ";
		cout << "";
		cin >> item_Description;

		if (item_Description == "end" || item_Description == "End")
		{
			char main_Menu;

			cout << "***************************" << endl;
			cout << "Number of items bought: " << endl; // Need to put this in
			cout << "Total Pre-Tax: " << endl; //Need to put this in
			cout << "Total Paid Tax: " << endl; //Need to put this in
			cout << "Grand Total: " << endl; //Need to put this in
			cout << "***************************" << endl;
			cout << "Would you like to return to the Main Menu? Y or N: " << endl;
			cin >> main_Menu;

			if( main_Menu == 'Y' || main_Menu == 'y')
			{
				menu_Repeat ();
			}

			else
			{
			}
		}

		else
		{
			cout << "";		
			cout << "Please Enter Pre-tax Value: ";
			cout << "";
			cin >> pre_tax_value;
			cout << "" << endl;

			complete_tax_value = tax_Calculator (pre_tax_value); // Call my Tax Calculator Function

			cout << "Item: " << item_Description << " | Pre-Tax Price: $" << pre_tax_value << " | After-Tax Price: $" << complete_tax_value << endl;
			cout << "********************************************************************************" << endl;
			cout << "" << endl;
		}
Last edited on
Topic archived. No new replies allowed.