Been Having a hard time with arrays

Hi guys just want to ask how can I transfer the input in my array in choice 1 to the choice 2 I've been trying it and it is not showing


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
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
	char choice2;
	int choice;
	int price[10];
	int i=0;
	char item[10][20];
	
	cout<<"Welcome to my Gadget Shop!!"<<endl;
	cout<<"[1] Enter Shop Items"<<endl;
	cout<<"[2] List Shop Items"<<endl;
	cout<<"[3] Buy Items"<<endl;
	cout<<"[4] Exit Program"<<endl;
	cout<<"Enter choice: ";
	cin>>choice;
	switch (choice){
		case 1:
			do {
	cout << "Enter shop items and price\n";
	cin >> item[i];
	cin >> price[i];
	cout << "Item and price is: " << item[i] << ": " << price[i] << "\n";
	cout << "Enter again? [Y/N]?" << endl; 
	cin >> choice2;
	choice2 = tolower(choice2);
	if (choice2=='n'){
		main();
	}if (i==5){
		cout<<"Maximum Item Reached!!"<<endl;
		cout<<"Go Back To Main Menu? [Y|N]";
		cin>>choice2;
		
	}
   }while ( choice2 == 'y' );

			case 2:
			if (i==0){
				cout<<"There Is No Item Yet"<<endl;
				system("PAUSE");
				system("CLS");
				main();
			}
			cout << "Item No." << "      Description" << "          Price\n";
    		for (int j=0; j<i; j++)
        cout << (j+1) << item[j] << "      " << price[j]<< "\n";
    }}
Last edited on
Please format your code.
https://www.cplusplus.com/articles/jEywvCM9/

First obvious mistake is calling main() recursively instead of using a loop.
Not only is this forbidden by the standard, you're getting fresh uninitialised arrays every time.
Oh okay okay sorry for that I'm just new in coding
Perhaps as a first pass over the code, consider:

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

constexpr size_t MAXITMS {10};
constexpr size_t MAXSTR {20};

int main() {
	int price[MAXITMS] {};
	char item[MAXITMS][MAXSTR] {};
	size_t noitms {};
	bool again {true};

	cout << "Welcome to my Gadget Shop!!\n";

	while (again) {

		cout << "\n[1] Enter Shop Items\n";
		cout << "[2] List Shop Items\n";
		cout << "[3] Buy Items\n";
		cout << "[4] Exit Program\n";
		cout << "\nEnter choice: ";

		int choice {};
		char choice2 {};

		cin >> choice;

		switch (choice) {
			case 1:
				do {
					if (noitms == MAXITMS) {
						cout << "Maximum Item Reached!!\n";
						choice2 = 'n';
					} else {
						cout << "Enter item name: ";
						cin >> ws;
						cin.getline(item[noitms], MAXSTR);

						cout << "Enter price: ";
						cin >> price[noitms];

						cout << "Item and price are: " << item[noitms] << ": " << price[noitms] << '\n';
						++noitms;

						cout << "Enter again? [Y/N]? ";
						cin >> choice2;

						choice2 = static_cast<char>(tolower(choice2));
					}
				} while (choice2 == 'y');
				break;

			case 2:
				if (noitms == 0)
					cout << "There Is No Item Yet\n";
				else {
					cout << "Item No." << " Description" << " Price\n";

					for (int j = 0; j < noitms; ++j)
						cout << (j + 1) << ' ' << item[j] << ' ' << price[j] << '\n';
				}
				break;

			case 3:
				cout << "To be done\n";
				break;

			case 4:
				again = false;
				break;

			default:
				cout << "Invalid choice\n";
				break;
		}
	}
}


@FMan
Another sweep through what you have reveals a few ways to simplify your code and avoid the complexity of the the several choices. eg If you enter an item and want to continue, instead of asking the question the way you have, selecting the existing menu item does exactly the same at no extra cost.

Having the menu as a function makes the code easier to handle/debug (instead of the illegal call to main()'), but it's not mandatory so just copy the lines back in to the while loop.

There are lots of things to do like validating user input to avoid crashes but that can be done later.

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

using namespace std;

char menu();

int main()
{
    char choice{'0'};

    const int MAX_NO_OF_ITEMS{10};

    int price[MAX_NO_OF_ITEMS];
    char item[MAX_NO_OF_ITEMS][20];

    int item_count{0};

    do{
        choice = menu();

        switch (choice)
        {
            case '1':
                cout << "Enter shop items and price\n";
                cin >> item[item_count];
                cin >> price[item_count];
                cout
                << "Item and price is: " << item[item_count] << ": "
                << price[item_count] << "\n";
                item_count++;
                break;

            case '2':
                if (item_count == 0)
                {
                    cout << "There Is No Item Yet\n";
                }
                cout << "Item No." << " Description" << " Price\n";
                for (int j=0; j<item_count; j++)
                cout << (j+1) << ' ' << item[j] << " " << price[j]<< "\n";
                break;

            case '3':
                break;

            case '4':
                cout << "Bye, bye\n";
                break;

            default:
                break;
        }
    }while (choice != '4');
}

char menu()
{
    char choice;

    cout
    << '\n'
    << "Welcome to my Gadget Shop!!\n"
    << "[1] Enter Shop Items\n"
    << "[2] List Shop Items\n"
    << "[3] Buy Items\n"
    << "[4] Exit Program\n"
    << "Enter choice: ";
    cin >> choice;
    cout << "*** You have selected: " << choice << '\n';

    return choice;
}



Welcome to my Gadget Shop!!
[1] Enter Shop Items
[2] List Shop Items
[3] Buy Items
[4] Exit Program
Enter choice: 2
*** You have selected: 2
There Is No Item Yet
Item No. Description Price

Welcome to my Gadget Shop!!
[1] Enter Shop Items
[2] List Shop Items
[3] Buy Items
[4] Exit Program
Enter choice: 1
*** You have selected: 1
Enter shop items and price
p
9
Item and price is: p: 9

Welcome to my Gadget Shop!!
[1] Enter Shop Items
[2] List Shop Items
[3] Buy Items
[4] Exit Program
Enter choice: 1
*** You have selected: 1
Enter shop items and price
y
9
Item and price is: y: 9

Welcome to my Gadget Shop!!
[1] Enter Shop Items
[2] List Shop Items
[3] Buy Items
[4] Exit Program
Enter choice: 2
*** You have selected: 2
Item No. Description Price
1 p 9
2 y 9

Welcome to my Gadget Shop!!
[1] Enter Shop Items
[2] List Shop Items
[3] Buy Items
[4] Exit Program
Enter choice: 4
*** You have selected: 4
Bye, bye
Program ended with exit code: 0
Hi! kinda thankful for you all help but can I ask for one last and how is the computation of the choice 3 is somewhat wrong when I entered the same item no it will only count as one?


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

using namespace std;

char menu();

int main()
{
    char choice{'0'};
bool keep_going = true;
char ans;
char answer;
float change;
    const int MAX_NO_OF_ITEMS{10};
float total = 0;
int item_quant;
int i=0;
    int price[MAX_NO_OF_ITEMS];
    char item[MAX_NO_OF_ITEMS][20];
    int item_no;

    int item_count{0};

    do{
        choice = menu();

        switch (choice)
        {
            case '1':
            	while(!(item_count==5)&&(keep_going)){
                cout << "Enter shop items and price\n";
                cin >> item[item_count];
                cin >> price[item_count];
                cout
                << "Item and price is: " << item[item_count] << ": "
                << price[item_count] << "\n";
                item_count++;cout<<"Enter Again? [Y|N] ";
                cin>>ans;
				if(ans=='n'||ans=='N'){
					keep_going=false;
				}
				}
                
                
                if (item_count==5){
                	cout<<"Maximum Item Reached!!"<<endl;
				}
                system("PAUSE");
                system("CLS");
                break;
            case '2':
                if (item_count == 0)
                {
                    cout << "There Is No Item Yet\n";
                }
                cout << "Item No." << " Description" << " Price\n";
                for (int j=0; j<item_count; j++)
                cout << (j+1) << ' ' << item[j] << " " << price[j]<< "\n";
                system("PAUSE");
                system("CLS");
                break;

            case '3':
            	 do{
				 
			cout << "Item No." << " Description" << " Price\n";
                for (int j=0; j<item_count; j++)
                cout << (j+1) << ' ' << item[j] << " " << price[j]<< "\n";
                	cout << "Enter Item no: ";
        			cin >> item_no;
        			cout << "Enter Quantity: ";
        			cin >> item_quant;
        			cout<<"Buy Another Item? [Y|N] ";
        			cin>>answer;
        			}
					while (answer=='Y'||answer=='y');
			

   					 for (int j = 0; j < item_count && item_quant; ++j) {
        			total +=  price[j]* item_quant;
    				}

    				cout << "Total Price: " << total << "\n";

    				cout << "Enter Payment: ";


   					 int payment;

    				cin >> payment;
    				change = payment - total;
    				while (payment <total){
    					cout<<"Payment not enough, Please Enter another payment"<<endl;
    					cout << "Enter Payment: ";
    					cin>>payment;
					}
					cout<<"Your change is "<<change<<endl;
					cout<<"Come Back again!!, Going back to main menu"<<endl;
					system("PAUSE");
					system("CLS");
					
            	
                break;

            case '4':
                cout << "Bye, bye\n";
                break;

            default:
                break;
        }
    }while (choice != '4');
}

char menu()
{
    char choice;

    cout
    << '\n'
    << "Welcome to my Gadget Shop!!\n"
    << "[1] Enter Shop Items\n"
    << "[2] List Shop Items\n"
    << "[3] Buy Items\n"
    << "[4] Exit Program\n"
    << "Enter choice: ";
    cin >> choice;
    cout << "*** You have selected: " << choice << '\n';

    return choice;
}
thankful for you all help


but just ignore it anyhow...

1
2
3
4
cout << "Enter Item no: ";
cin >> item_no;
cout << "Enter Quantity: ";
cin >> item_quant;


The total price for that item is the price for that item * quantity.

The total purchase price is the sum of the totals for all the purchased items.
Topic archived. No new replies allowed.