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
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'){
}
elseif (input=='N'){
break;
}
break;
}
else {
qtty();
if (qty>aq){
no();
cout<<endl;
break;
}
elseif (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'){
}
elseif (input=='N'){
break;
}
break;
}
else {
qtty();
if (qty>bq){
no();
cout<<endl;
break;
}
elseif (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'){
}
elseif (input=='N'){
break;
}
break;
}
else {
qtty();
if (qty>cq){
no();
cout<<endl;
break;
}
elseif (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'){
}
elseif (input=='N'){
break;
}
break;
}
else {
qtty();
if (qty>dq){
no();
cout<<endl;
break;
}
elseif (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'){
}
elseif (input=='N'){
break;
}
break;
}
else {
qtty();
if (qty>eq){
no();
cout<<endl;
break;
}
elseif (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'){
}
elseif (input=='N'){
break;
}
}
}
if (input=='N'){
break;
}
elseif (pymnt>0){
pymnt=pymnt-pymnt;
cout<<pymnt;
break;
}
}
while (x!=0);
break;
}
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.
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.