jeudi 22 novembre 2018

How do I return from an int to int main after an if-else statement?

How do I return from an int to int main after an if-else statement?

using namespace std;
    char menu;
    int i,q[10],rot,t[5];
    int pu[10],qu[10];
    int item[10];
    int prodq;
    int num;
    float bank, TS, p[10];
    string prodname[10]; 

so the idea of this is the user has to input numbers from 1-5, I tried to achieve this by hmmm.. detecting the numbers from 1-5 respectively

    int additems () {
        system ("cls");
        slots:

        cout << "SLOT 1: " << prodname[1] << "| x" << q[1] << " | " <<  p[1] << " PHP\n";
        cout << "SLOT 2: " << prodname[2] << "| x" << q[2] << " | " <<  p[2] << " PHP\n";
    cout << "Select slot to manage (1-5): ";
    cin >> num;         


    if(num>6 || num<0){
        system ("cls");
        cout << "Invalid number, please enter numbers exclusively from 1-5 \n";
        goto slots;
    } 

I wanted 6 to be able to go back to mainmenu; or int main; but I've yet to find a way

    //INSERT & UPDATE! this section will allow the program to detect if a slot has been used before.


     //switch(menu){
          //case '1' :
          else if(num==1){
            system ("cls");
            cout << "SLOT 1: " << prodname[1] << "| x" << q[1] << " | " <<  p[1] << " PHP\n";
            cout <<"Enter product name for item slot NO.1: ";
            cin >> prodname[1];
            cout <<"Enter quantity of " << prodname[1] << " : ";
            cin >> q[1];
            cout <<"Enter price " << prodname[1] << " : ";
            cin >> p[1];
            system ("cls");
            goto slots;
            return 0;
        }

so what happens after this is the console just says Process exited after xx seconds with return value 0, what my intention is for it to go back to mainmenu(); if it does miraculously work it only goes back to slots;

            // break;
         // case '2' :
         else if(num==2){

            system ("cls");
            cout << "SLOT 2: " << prodname[2] << "| x" << q[2] << " | " <<  p[2] << " PHP\n";
            cout <<"Enter product name for item slot NO.2: ";
            cin >> prodname[2];
            cout <<"Enter quantity of " << prodname[2] << " : ";
            cin >> q[2];
            cout <<"Enter price " << prodname[2] << " : ";
            cin >> p[2];
            return 0;
       }

       else if(num==3){

         // case'3' :

            system ("cls");
            cout << "SLOT 3: " << prodname[3] << "| x" << q[3] << " | " <<  p[3] << " PHP\n";
            cout <<"Enter product name for item slot NO.3: ";
            cin >> prodname[3];
            cout <<"Enter quantity of " << prodname[3] << " : ";
            cin >> q[3];
            cout <<"Enter price " << prodname[3] << " : ";
            cin >> p[3];
            return 0;
          //   break;
          }
         // case '4' :
         else if(num==4){
     cout <<"empty";
    }

        else if(num==5){

        //  case '5' :
             cout <<"empty";

        /*else if(num==x){

        //  case '5' :
        cout <<"empty";
        //  break;
        */

        return 0;
    }


    }




    int mainmenu(){
        mainmenu:
        { 
            cout << "INVENTORY SYSTEM";
            //I've put some choices here
       cin >> menu;
        }
       switch(menu){
        case '1' :
             system ("cls");
             additems();
             break;
        case '2' :
            system ("cls");
             //placeholder();
       }
        system("cls");


    }



    int main() {

        int choice;
        int choice2;
        int total;
        int itemq;
        char ch;
        int ln;
        int un;
        int l=0;
        int counter=1;

        cout << "L O A D I N G \n\n";

      for (l=0; l<60; l++) { 
      system("Color 0b");
        Sleep(counter);  
        cout << ". "; 
        Sleep(counter);  
      system("Color 0c");
      } 
        //ca

        mainmenu();
        /*
        mainmenu:
        { 
       cout <<" SALES AND INVENTORY SYSTEM \n\n";
       cout << "[1] MANAGE ITEMS IN INVENTORY \n[2] VIEW ITEMS IN INVENTORY\n";
       cout << "\nEnter choice: ";
       cin >> menu;
        }
    //za
       switch(menu){
          case '1' :
             system ("cls");
             cout << "MANAGE ITEMS \n" << endl; 
             additems();
             break;
          case '2' :
             cout << "VIEW ALL ITEMS \n!" << endl; 
       }


    }else
    {
        cout<<"Access denied";
        exit(0);
    }
    //fa


       return 0;
    }

Aucun commentaire:

Enregistrer un commentaire