Can functions be applied in this scenario?

Hello! Is there any way to make one small function out of all of the cases inside case 2 to make the code less redundant. Also, how can I make the "Would you like to do it again" a function, I tried replacing the break; with return; but I wasn't able to get the desired result.

I'm new to programming so please don't be too harsh lol

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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
              case 2:{
                do{
                    table();
                    cout<<endl<<"Input the item code of your choice: ";
                    cin>>itcd;
                    itcd = toupper(itcd);
                    switch (itcd){
                        case 'A':{
                            if (aq<=0){
                                cout<<"OUT OF STOCK!"<<endl;
                                cout<<"Would you like to try again? Press Y for Yes and N for No: ";
                                cin>>input;
                                input=toupper(input);
                                if (input=='Y'){

                                }
                                else if (input=='N'){
                                    break;
                                }
                                break;
                            }
                            else {
                                qtty();
                                if (qty>aq){
                                    no();
                                    cout<<endl;
                                    break;
                                }
                                else if (aq>=qty){
                                    price=qty*8;
                                    pay();
                                    if (pymnt>=price){
                                        aq=aq-qty;
                                    }
                                    break;
                                }
                            }
                        }
                        case 'B':{
                            if (bq<=0){
                                cout<<"OUT OF STOCK!"<<endl;
                                cout<<"Would you like to try again? Press Y for Yes and N for No: ";
                                cin>>input;
                                input=toupper(input);
                                if (input=='Y'){

                                }
                                else if (input=='N'){
                                    break;
                                }
                                break;
                            }
                            else {
                                qtty();
                                if (qty>bq){
                                    no();
                                    cout<<endl;
                                    break;
                                }
                                else if (bq>=qty){
                                    price=qty*12;
                                    pay();
                                    if (pymnt>=price){
                                        bq=bq-qty;
                                    }
                                    break;
                                }
                            }
                        }
                        case 'C':{
                            if (cq<=0){
                                cout<<"OUT OF STOCK!"<<endl;
                                cout<<"Would you like to try again? Press Y for Yes and N for No: ";
                                cin>>input;
                                input=toupper(input);
                                if (input=='Y'){

                                }
                                else if (input=='N'){
                                    break;
                                }
                                break;
                            }
                            else {
                                qtty();
                                if (qty>cq){
                                    no();
                                    cout<<endl;
                                    break;
                                }
                                else if (cq>=qty){
                                    price=qty*15;
                                    pay();
                                    if (pymnt>=price){
                                        cq=cq-qty;
                                    }
                                    break;
                                }
                            }
                        }
                        case 'D':{
                            if (dq<=0){
                                cout<<"OUT OF STOCK!"<<endl;
                                cout<<"Would you like to try again? Press Y for Yes and N for No: ";
                                cin>>input;
                                input=toupper(input);
                                if (input=='Y'){

                                }
                                else if (input=='N'){
                                    break;
                                }
                                break;
                            }
                            else {
                                qtty();
                                if (qty>dq){
                                    no();
                                    cout<<endl;
                                    break;
                                }
                                else if (dq>=qty){
                                    price=qty*10;
                                    pay();
                                    if (pymnt>=price){
                                        dq=dq-qty;
                                    }
                                    break;
                                }
                            }
                        }
                        case 'E':{
                            if (eq<=0){
                                cout<<"OUT OF STOCK!"<<endl;
                                cout<<"Would you like to try again? Press Y for Yes and N for No: ";
                                cin>>input;
                                input=toupper(input);
                                if (input=='Y'){

                                }
                                else if (input=='N'){
                                    break;
                                }
                                break;
                            }
                            else {
                                qtty();
                                if (qty>eq){
                                    no();
                                    cout<<endl;
                                    break;
                                }
                                else if (eq>=qty){
                                    price=qty*2;
                                    pay();
                                    if (pymnt>=price){
                                        eq=eq-qty;
                                    }
                                    break;
                                }
                            }
                        }
                        default:{
                            cout<<"Please input one from the following item codes: A, B, C, D, E."<<endl;
                            cout<<"Would you like to try again? Press Y for Yes and N for No: ";
                            cin>>input;
                            input=toupper(input);
                            if (input=='Y'){

                            }
                            else if (input=='N'){
                                break;
                            }
                        }
                    }
                if (input=='N'){
                    break;
                }
                else if (pymnt>0){
                    pymnt=pymnt-pymnt;
                    cout<<pymnt;
                    break;
                }
                }
                while (x!=0);
            break;
            }
Lets look at a smaller example:
1
2
3
4
5
6
7
8
9
10
11
12
char itcd;
bool again = true;
do {
  cin >> itcd;
  switch ( itcd ) {
    case 'A':
      again = false;
      break;
    default:
  }

} while ( again );

This loop continues while again remains true. Yes, you could move code into functions:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
char itcd;
bool again = true;
do {
  cin >> itcd;
  switch ( itcd ) {
    case 'A':
      if ( eq <= 0 ) {
        again = outofstock();
      }
      break;
    default:
  }
} while ( again );


// with
bool outofstock() {
  out<<"OUT OF STOCK!"<<endl;
  cout<<"Would you like to try again? Press Y for Yes and N for No: ";
  char input;
  cin>>input;
  input=toupper(input);
  return input != 'N';
}


You have (at least) nested
switch
  loop
    switch

You do have a lot of breaks. Most of them seem to merely jump to the end of the inner switch.

Some end the loop. Which asks a question, which shows intent better
1
2
3
4
  if ( x ) break;
} while ( y );
// OR
} while ( y && !x );


The outer switch could indeed be simpler, if you can move nested bits into functions:
1
2
3
4
5
6
7
8
switch ( x ) {
  // cases
  case 2:
    result = casetwo(/*data*/);
    break;
  //cases
}
// use the result 

The only question is, what data to pass in and out.
Thank you for the quick reply!

Do you mind me asking what this does? I've read about return; but I'm not really sure how it works
 
return input != 'N';


Also, what makes the second option better?
1
2
3
4
 if ( x ) break;
} while ( y );
// OR
} while ( y && !x );


Thank you again for replying :)
Last edited on
Some functions always return a value. Other functions return no value (void).
See http://www.cplusplus.com/doc/tutorial/functions/

1
2
3
4
5
bool outofstock() // promise to return a bool value
{
  // code
  return foo; // foo is a bool, or converts to bool
}



What is better, depends,

On one there is a loop that repeat as long as the condition is true:
1
2
3
do {
  // things to repeat
} while ( condition );

To repeat or not is all based on one expression on one line.

On the other one still has some condition, but that is not the only point of exit:
1
2
3
4
5
6
7
do {
  // things to repeat
    break;
  // things to repeat in some cases
    break;
  // things to repeat, unless
} while ( condition );


"Correct" and "easy to understand" are things to favor. The latter is of course subjective. Complex, hard to read code is much more difficult to make correct and keep correct.
1
2
3
4
5
6
do {
  // code
  if ( x ) break; // loop will end, IF x==true
} while ( y ); // loop will end, IF y!=true
// summary:
// loop ends, IF (x==true OR y==false) 

1
2
3
4
5
do {
  // code
} while ( !x && y );
// summary:
// loop continues, IF (x==false AND y==true) 


loop continues == loop does not end
(x==false AND y==true) == NOT (x==true OR y==false) Boolean logic.
Topic archived. No new replies allowed.