storing array

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

using namespace std;

struct Calculation
{string itemName;
 double price,tax,sum;
};
void input(Calculation total[], int index, double sum);
void showPrice (Calculation total[], int index,double sum);

int main()
{	int index=0;
    double sum=0.0;
	const int MAX=10;
	Calculation total[MAX];
	int choice;

		do
		{
			cout<<"1. Input item name, price, and tax"<<endl;
			cout<<"2. show total"<<endl;
			cout<<"3. exit"<<endl;
			cout<<"you choose:";
			cin>>choice;

			switch (choice)
			{
			case 1:
				input(total,index,sum);
				break;
			case 2:
				showPrice(total,index,sum);
				break;
			case 3:

					cout<<"you exit the program";
				break;
			}

	}	while (choice!=3);


}


void input(Calculation total[], int index,double sum)
{
	cout<<"which item";
	cin.ignore();
	getline(cin,total[index].itemName);
	cout<<"how much is it";
	cin>>total[index].price;
	cout<<"how much is the tax";
	cin>>total[index].tax;


}


void showPrice (Calculation total[], int index,double sum)
{
	sum=total[index].price+total[index].tax;
	cout<<"item"<<"            "<<"total price"<<endl;
cout<<total[index].itemName<<"           "<<sum<<endl;

}



This is my code. My goal is to input item name and price and tax or to display the total price of that item. for(index=0;index<size;index++) will not work, because I need to able to input, then show it on the display, then go back to input again.
But whenever I choose option 1 and input another value in the array, the result replaces the pervious item.I am not sure how to fix it.
Last edited on
Hello zxkun,

In both functions and in main "index" is never changed. My first thought is to increase "index" in the case statements.

Hope that helps,

Andy
Hey Andy

When I try to put index++;
1
2
3
4
case 1:
 				input(total,index,sum);
                                //index++; here
				break;

the sum displays a extremely huge number.

And When I try to put index++; in the last lineof my void input(Calculation total[], int index,double sum) function, and use int&index in the parameter, the output is still a huge number.
Hello zxkun,

According to your original code you initialized "index", so the index++; in case 1 should not be a problem. At least it worked for me.

Second thing I did was to initialize all the variables of the struct because any unused variable had a very large negative number:

1
2
3
4
5
struct Calculation
{
	std::string itemName{ "" };
	double price{ 0.0 }, tax{ 0.0 }, sum{ 0.0 };
};


I changed the function "showPrice" to use a for loop and also changed the output. Unless your idea was to use "showPrice" after each entry. In which case I would have to undo my changes and do some testing.

1
2
3
4
case 1:
	input(total,index,sum);
        //index++; here
        break;


index++; should work. I hope the word "here" is commented or tho there or you will have a problem. If this gives you an error message post the message.

Next "sum" is defined in main and passed by value to the function. In the function you only are using a copy of "sum", so any changes in the function will not be reflected in main. Ad when the function ends "sum" is destroyed. If yo really want to leave "sum" in main and want the value to change then pass the variable by reference:
void input(Calculation total[], int index, double & sum). Now any change to "sum" in the function will be seen in main, but there is no real need for "sum" to be defined in main or passed to the functions. Unless the idea is to keep a running total of your input.

the sum displays a extremely huge number.
I do not see how because the variable is initialized to zero and never changes.

When I first added the "index++" to case 1 the input worked, but the showPrice" function did not because "index" would have the wrong value.

If you are still having problems post the whole code for main.

Hope that helps,

Andy
Hey Andy,

I made some progress, but there is some small problems.

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

using namespace std;
const int MAX=10;
struct Calculation
{
	string itemName= {"   "};
	double price={0.0},
			tax={0.0},
			sum={0.0};
};
void input(Calculation total[], int index);
void showPrice (Calculation total[], int index,double&sum);

int main()
{	int index=0;
    double sum=0.0;

	Calculation total[MAX];
	int choice;

		do
		{
			cout<<"1. Input item name, price, and tax"<<endl;
			cout<<"2. show total"<<endl;
			cout<<"3. exit"<<endl;
			cout<<"you choose:";
			cin>>choice;

			switch (choice)
			{
			case 1:

				input(total,index);
				index++;

				break;
			case 2:
				cout<<"index"<<index;
				showPrice(total,index,sum);
				break;
			case 3:

					cout<<"you exit the program";
				break;
			}

	}	while (choice!=3);


}


void input(Calculation total[], int index)
{
	cout<<"which item";
	cin.ignore();
	getline(cin,total[index].itemName);
	cout<<"how much is it";
	cin>>total[index].price;
	cout<<"how much is the tax";
	cin>>total[index].tax;
index++;

}


void showPrice (Calculation total[], int index,double&sum)
{
	cout<<"item"<<"            "<<"total price"<<endl;
for(index=0;index<MAX;index++)
{sum=total[index].price+total[index].tax;

cout<<total[index].itemName<<"               "<<sum<<endl;
}

}


My option 2 is able to display like

item 1 price
item 2 price

but the ones I didn't assign, are all zeros.
Last edited on
Hello zxkun,

All variables in the struck are initialized, so when Calculation total[MAX]; defines the array each element of the array is a struck and each struct will initialize each variable of the struct.

When option 2 goes to print it will print all 10 elements of the array even if there is nothing in them. That is where your zeros are coming from. You need to find a different way to limit how many elements of the array to print.

You could use a counter variable to count how many times you call the function"input" or use a "std::vector", but the whole program would have to change. The advantages to a vector are that you only store what you need and in a for loop you can use "vectorName.size()" to get how many elements are in the vector unlike an array which has a fixed size.

With what you have you could add an if statement to the for loop to check if "total[index] is equal to zero and skip to the next record like this:

1
2
3
4
5
6
7
8
for (index = 0; index<MAX; index++)
{
	if (total[index].price == 0) continue;

	sum = total[index].price + total[index].tax;

	std::cout << total[index].itemName << "               " << sum << std::endl;
}


Another thing I noticed is that you start your output in the switch and finish in the function. All output should be done in the function.

Hope that helps,

Andy
Topic archived. No new replies allowed.