-2

At the end of the switch statement, I want to prompt the user to enter another input that will be checked again in the switch statement. How can I reloop the new user input to the switch statement?

    switch(choice){
        case 'l' :
            ct.Start();
            count = LinearSearch(new_books, requested_books); 
            ct.Stop();
            break;
        case 'b' :
            ct.Start(); 
            Sort(new_books, requested_books); 
            count = BinarySearch(new_books, requested_books); 
            ct.Stop();
            break;
        case 'r' :
            ct.Start();
            Sort(new_books, requested_books); 
            count = RecursiveBinarySearch(new_books, requested_books); 
            ct.Stop();
            break;
        default : 
            std::cout << "Incorrect choice\n";
            std::cout << "Choice of search method ([l]inear, [b]inary, [r]recursiveBinary)?";
            std::cin >> choice;  //do I need to run some sort of while loop here?
    }

I imagine I have to wrap the switch statement in a while loop, but I have no idea how it would work

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Jason Ma
  • 1
  • 2

1 Answers1

-1

You are correct that a loop is needed. Try something like this:

bool promptAgain;

do {
    std::cout << "Choice of search method ([l]inear, [b]inary, [r]recursiveBinary)?";
    std::cin >> choice;

    promptAgain = false;

    switch (choice){
        case 'l' :
            ct.Start();
            count = LinearSearch(new_books, requested_books); 
            ct.Stop();
            break;
        case 'b' :
            ct.Start(); 
            Sort(new_books, requested_books); 
            count = BinarySearch(new_books, requested_books); 
            ct.Stop();
            break;
        case 'r' :
            ct.Start();
            Sort(new_books, requested_books); 
            count = RecursiveBinarySearch(new_books, requested_books); 
            ct.Stop();
            break;
        default : 
            std::cout << "Incorrect choice\n";
            promptAgain = true;
            break;
    }
}
while (promptAgain);
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770