How do you pass the value that was inputted after each while-loop iteration?

Heya, I'm still quite inexperienced with programming since I'm just a freshman here, I just wanna know if there is a way to pass values that was inputted witihn a while-loop to outside of it since I need the values to be listed on another part of the program which is the list of shop items. I'll provide my current code below for your reference:


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
 
int choice, price, i=1;
char item[10], choice2;	
	
	main:
	cout << "Welcome to my toy store!" << endl; 
	
	cout << "What can we do for you today?" << endl;
	
	cout << "[1] Enter Shop Items" << endl; 
	
	cout << "[2] List of Shop Items" << endl; 
	
	cout << "[3] Buy Items" << endl; 
	
	cout << "[4] Exit Program" << endl; 
	
	cout << "Please enter your choice" << endl; 
	
	cin >> choice; 
	
	if (choice == 1){
		
	shopmenu:
				
	cout << "Enter shop items and price\n";
	cin >> item;
	cin >> price;
	cout << "Item and price is: " << item << ": " << price << "\n";
	cout << "Enter again? [Y/N]?" << endl; 
	cin >> choice2;
	
	switch(choice2){
		
	case 'Y': case 'y':
	
	goto shopmenu;
	
	while (i<=5){	
	++i;
	
	if (i=5){
	goto main;
	}
	
    }
		
	case 'N': case 'n':
	
	goto main;	
Well the first thing to get out of doing ASAP is using goto all over the place.
Backward jumping goto's are best implemented as while loops.

1
2
3
4
5
6
7
8
9
10
if (choice == 1){
    do {
	cout << "Enter shop items and price\n";
	cin >> item;
	cin >> price;
	cout << "Item and price is: " << item << ": " << price << "\n";
	cout << "Enter again? [Y/N]?" << endl; 
	cin >> choice2;
   while ( choice2 == 'Y' );
}


If you find your code getting too complicated, then make some functions.
1
2
3
if (choice == 1){
    enterShopItems(/*parameters go here*/);
}

Each function should have a clear statement of intent that can be summarised in a single sentence.

Like
"This function enters items into the shop"

As salem c recommended, get rid of your goto statements.

Line 2: price should be an array. You need to store the price of every item.
int price[10];

Line 2: i should start at 0.
int i=0;

Line 3: item should be a two dimension array.
char]item[10][20];

Line 5: Every program must have a
int main () and an opening brace.
main is a poor choice for a label.

lines 27,28: You need to read item and price into specific elements of the array.
cin >> item[i] >> price[i];

Line 29: You need to reference specific elements of item and price:
cout << "Item and price is: " << item[i] << ": " << price[i] << "\n";

Line 51: Your program needs a return 0; and a closing brace.

Keep in mind that if you sell an item, it is now longer in inventory.



Last edited on
I fixed my program and codes now although unfortunately when I made the third function which is the int list (int c) and whenever I call it on the program it keeps showing that it can't be used as a function while the other two functions work just fine, is there a way to fix it somehow??

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

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

//functionzz//

	
char loop(int a){
char  choice2;
int choice, itemloop, b, price[10];
	
		
	cout << "Welcome to my toy store!" << endl; 
	
	cout << "What can we do for you today?" << endl;
	
	cout << "[1] Enter Shop Items" << endl; 
	
	cout << "[2] List Shop Items" << endl; 
	
	cout << "[3] Buy Items" << endl; 
	
	cout << "[4] Exit Program" << endl; 
	
	cout << "Please enter your choice" << endl; 

    
    
}



//shop item//

int itemloop(int b){
char item[10][20], choice2, a;
int counter=1, choice, i=0, price[10], list, c;


    do {
	cout << "Enter description\n";
	cin >> item [i];
	cout << "Enter price\n";
	cin  >> price [i];
	cout << "Enter again? [Y/N]?" << endl; 
	cin >> choice2;
	++i;
  if (i==5){
  	cout << "error, max items reached\n";
   	cout << loop(a);
   }
}
   while ( choice2 == 'y' && counter !=5 );


   cout << loop(a);
   cin >> choice; 
   if (choice == 1)
   cout << itemloop(b);
   
   	if (choice == 2){
	cout << list(c); 
    
    }
}




int list(int c){
char item[10][20], choice2, a, choice3;
int price[10], list;


	if (i==0){
	cout << "No items yet\n" << loop(a);
	}
	
	if (i==1){
	cout << "Item and price is: " << item[0] << " " << price[0]<< "\n";
	}
	if (i==2){
	
	cout << "Item and price is: " << item[1] << " " << price[1]<< "\n";
    }
	if (i==3){
	cout << "Item and price is: " << item[2] << " " << price[2]<< "\n";
    }
	if (i==4){
	cout << "Item and price is: " << item[3] << " " << price[3]<< "\n";
    }
	if (i==5){
	cout << "Item and price is: " << item[4] << " " << price[4]<< "\n";	
    }
    
    cout << "Go back to main menu? [Y/N]";
    
    cin >> choice3;
    
    if (choice3 == 'Y'|| choice3 == 'y'){
    	
    cout << loop(a);	
	}
	
	else if (choice3 == 'N' || choice3 == 'n'){
		cout << list(c);
    }
}


int main(){

int choice, b, c, a;

cout << loop(a);
	
	cin >> choice; 
	
	if (choice == 1)
	
	cout << itemloop(b);
	
	else if (choice == 2)
	
	cout << list(c);
	
	
	
}


Last edited on
One issue is that a function has to be defined/declared before it can be used, You're using list() in itemloop() before it is defined.

However, the whole logic seems wrong. main() should display the menu, get an option and then call an appropriate function based upon the entered option. The called functions should do the required processing and then return. The functions don't call themselves.

Line 9: Why is this called loop()? It doesn't loop. A better name is menu().

Lines 9-11: Your argument and your local variables are not used in the function. Why are they there?

Line 28: You should do the following:
1
2
    cin >> choice2;
    return choice2;


Line 37,38: item, price and i are local variables. Their values are lost when you exit the function.

Line 40: Why are you testing for 5? Your arrays can hold 10 items. The best way to ensure these are consistent is to use a constexpr to define the size of your array. If you want to change the maximum size of the inventory, you only need to make the change in one place.
1
2
3
4
5
6
constexpr MaxInventory = 10;
...
char item[MaxInventory][20];
int price[MaxInventory];
...
    if (i == MaxInventory)


Line 57, 77, 107: You output the return value of loop(), but loop() has no return statement.

Line 60: You call itemloop() recusrsively. This is a poor dsesign. You should use a loop.

Line 101: If choice3 is Y, simply return;

Line 107: You call list() recursively. Again, a poor design. Use a loop.

Lines 76,80,83,87,90,93: You reference the value of i, but i is not defined in your function.

Lines 76-95: You should use a loop here. At this point, i contains the number of elements in the inventory.
1
2
    for (int j=0; j<i; j++)
        cout << "Item and price is: " << item[j] << " " << price[j]<< "\n";

Line 122: You output the return value of itemloop(), but itemloop() has no return value.

Lines 120, 124: This is a perfect place for a switch statement.
1
2
3
4
5
6
7
    choice = menu();
    switch (choice)
    {
    case 1:  itemloop(); break; 
    case 2:  list (); break;
    default:  // do something
    }



Last edited on
Topic archived. No new replies allowed.