vendredi 3 décembre 2021

How do I loop through the program properly? [closed]

I just wanna ask something about my program. Here in my program, I want to be able to have a "return" option where you can go back to a previous menu. For example, choosing 1 to enter the formalscience menu then choosing mathematics and pressing 4 to return to formalscience menu, then returning back to the main menu. For some reasons, when I press 3 to return to the main menu from the formalscience menu, it loops to the formalscience menu but pressing 3 again will lead me to the main menu which is what I want. How do I fix this issue? I know I am doing something wrong with my loops so excuse me for that. I am a complete beginner to c++.

#include <iostream>
#include <string>

using namespace std;

class sciences
{
public:
    
    string formaloptions ()
    {
        int choice;
        
        cout << "You chose Formal science!\nFormal Science has two categories:\n";
        cout << "1. Mathematics\n2. Logic\n3. Back";
        cout << "\nWhat will you choose?\n";
        cin >> choice ; 
        
        switch (choice)
        {
            case 1:
            return "MATHEMATICS";
            break;
            case 2:
            return "LOGIC";
            break;
            case 3:
            return "BACK";
            break;
        }
    }
    
    
  };

  class subject 
  {
public:
    
    int mathematics()
    {
        int choice;
        
        cout << "You chose Mathematics!\n";
        cout << "Press 4 to return\n";
        cin >> choice ;
        
        switch (choice)
        {
            case 4:
                return 4;
                break;
        }
    }
    };

int main()
{
sciences sciencesobject;
subject subjectobject;

int choice;
int keytoformal;
int keytomath = 0;
int repeatertothemainmenu = 0;

do
{
cout << "BRANCHES OF SCIENCE\n\n";
cout << "Choose what do you want to tackle\n";
cout << "1. Formal\n2. Natural\n3. Humanistic\n";
cin >> choice;

switch (choice)
{
    case 1:
    keytoformal = 1;
    break;
}

while(keytoformal == 1)
{
if (sciencesobject.formaloptions() == "MATHEMATICS")
{
    keytomath = 1;
}
else if (sciencesobject.formaloptions() == "BACK")
{
    keytomath = 0;
    repeatertothemainmenu = 1;
    keytoformal = 0;
}
while (keytomath == 1)
{   
    switch (subjectobject.mathematics())
    {
        case 4:
        keytoformal = 1;
        keytomath = 0;
        break;
    }
}

}

 }while (repeatertothemainmenu == 1);
 }

Aucun commentaire:

Enregistrer un commentaire