Hi All
Can someone pls help me on this one. Under nested 'if' conditions inside 'case', i have 2 sections. If i choose to go to section 1 or 2, when i am done, how can i return back to the primary 'if'. Any help will be highly appreciated.

This is the general structure of my program.

int main()
{
    //variables initialized here
    int option;
    char sectionLoop;
    
    
    cout << "Statements.........;
    cout << "Choose Options. (1,2, 3 or 4)\n"; cin >> option";
    do
    {
       	//statements
	//statements
	}
	while ((option <1) || (option >4));

        switch (option)
	{
        case 1:
            {
              //Statements
	      //Nested if's
                {
                 if (............) // Option was 1, assign data to multiple variables
                 //statements
                 //statements
                 //statements
                 cout << "Do you want to proceed to [SECTION A] or [SECTION B] (A/B)? : "; 
                 cin >> sectionLoop;
		}
					
// QUESTION : IF I CHOOSE TO GO TO [SECTION A OR B BELOW,], HOW CAN I RETURN BACK TO PRIMARY NESTED IF UNDER CASE 1?
	
			// [SECTION A]
			if ((sectionLoop == "a") || (sectionLoop == "A"))
                        {
                        // statements
                        // statements
                        // statements   
                        break;
                        }
                  
                        // [SECTION B]
                        else if ((sectionLoop == "b") || (sectionLoop == "B"))
                        {
                        // statements
                        // statements
                        // statements   
                        // statements
                        // statements
                        // statements   
                        break;
    
	case 2:
            {
		// Similar to case 1)
	    }
	case 3:
            {
		// Similar to case 1)
	    }
	case 4:
            {
		// Similar to case 1)
	    }
}

Edited 6 Years Ago by newbie_to_cpp: n/a

Use a loop, your code already has at least one in it, so you must be at least familiar with them.

Edited 6 Years Ago by Fbody: n/a

Just use a break and loop like Fbody suggested.

e.g.,

int answer = 1;
while (answer >= 1 && <= 4)
{
	switch (answer)
	{
		case 1:
		{
			cout << "what do you want to do?\n";
			cin << answer;
			break;
		}
		case 2:
		{
			//do stuff
			
			cout << "what do you want to do now?\n";
			cin << answer;
			break;
		}
		case 3:
		{
			//do stuff
			
			cout << "what do you want to do now?\n";
			cin << answer;
			break;
		}
		case 4:
		{
			//do stuff
			
			cout << "what do you want to do now?\n";
			cin << answer;
			break;
		}
	}
}

Thanks Duki. But what i needed was a way to go back to the main body of case (in my case primary 'if' condition) from inside the multiple nested 'if's under it. Pls see the code structure i posted and refer the comments in upper case for clear idea. I tried using loop. But it didnt work out. I need more patience and debugging skills i guess.

Just use a break and loop like Fbody suggested.

e.g.,

int answer = 1;
while (answer >= 1 && <= 4)
{
	switch (answer)
	{
		case 1:
		{
			cout << "what do you want to do?\n";
			cin << answer;
			break;
		}
		case 2:
		{
			//do stuff
			
			cout << "what do you want to do now?\n";
			cin << answer;
			break;
		}
		case 3:
		{
			//do stuff
			
			cout << "what do you want to do now?\n";
			cin << answer;
			break;
		}
		case 4:
		{
			//do stuff
			
			cout << "what do you want to do now?\n";
			cin << answer;
			break;
		}
	}
}

So nest a loop within your case instead, like I said earlier. It's just structure nesting, it's not that difficult...

char primaryChoice = '\0';

cout << "Make your primary choice, user: ";
cin >> userChoice;

switch (primaryChoice) {
 case 'a':
   char secondaryChoice = '\0';
   do {
     cout << "Make your secondary choice: ";
     cin >> secondaryChoice;

     if (secondaryChoice == someValue) {
       //do something
       if (anotherChoice == somethingElse)
         //blah
       else
         //else blah

     } else {
       //do something else
     } //end if

   } while (someCondition == true);  //end do
   break;

 case 'b':
   //case 'b' actions

 default:
   //default actions
} //end switch

Edited 6 Years Ago by Fbody: n/a

Thank you. I Will modify my program structure to match yours and see how it works out.

So nest a loop within your case instead, like I said earlier. It's just structure nesting, it's not that difficult...

char primaryChoice = '\0';

cout << "Make your primary choice, user: ";
cin >> userChoice;

switch (primaryChoice) {
 case 'a':
   char secondaryChoice = '\0';
   do {
     cout << "Make your secondary choice: ";
     cin >> secondaryChoice;

     if (secondaryChoice == someValue) {
       //do something
       if (anotherChoice == somethingElse)
         //blah
       else
         //else blah

     } else {
       //do something else
     } //end if

   } while (someCondition == true);  //end do
   break;

 case 'b':
   //case 'b' actions

 default:
   //default actions
} //end switch
This question has already been answered. Start a new discussion instead.