0

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 by newbie_to_cpp: n/a

3
Contributors
5
Replies
6
Views
6 Years
Discussion Span
Last Post by newbie_to_cpp
Featured Replies
  • 2
    Duki 552   6 Years Ago

    Just use a break and loop like Fbody suggested. e.g., [code=c++] 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 … Read More

  • 1

    So nest a loop within your case instead, like I said earlier. It's just structure nesting, it's not that difficult... [CODE]char primaryChoice = '\0'; cout << "Make your primary choice, user: "; cin >> userChoice; switch (primaryChoice) { case 'a': char secondaryChoice = '\0'; do { cout << "Make your … Read More

0

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

Edited by Fbody: n/a

2

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;
		}
	}
}
0

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;
		}
	}
}
1

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 by Fbody: n/a

0

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.
Be sure to adhere to our posting rules.