0

As the title says, I'm searching for a way to ask the user for input again if they dont provide an expected answer. I've searched around for some time, but all I've found was stuff for python and age stuff... I compare strings, couldn't tailor it to what I wanted.

I used goto in C# if the user did not give a wanted input (e.g. "1","2","3","4","debug","test"). Here's an example:

label:
string userinput == Console.ReadLine();
if (userinput == "test")
{
 // stuff here
}

else if (userinput == "debug")
{
// more code here
}

else
{
goto label:
}

Here in C:

char userinput[20];
label:
printf("> ");
scanf("%s", userinput);
if (strcmp(userinput,"debug") == 0)
{
// stuff here
}
else if (strcmp(userinput,"test") == 0)
{
// more stuff here
}
else if (strcmp(userinput,"1") == 0)
{
// you get the idea
}
else 
{
goto label;
}

I could maybe use goto in C as well, but upon reading more, I see that many are against using goto, so I decided not to, as it was really getting hard to keep up with it as well. How can I loop this? I'm a beginner in C, been thinking about this for a week now. Decided to ask here at last, sorry if this is a duplicate, any help appreciated!

steadybit
  • 1
  • 1
  • 1
    Put the code in a `while(1)` loop. Break out of the loop when you get valid input. – Barmar Sep 15 '21 at 17:53
  • There are [some cases](https://stackoverflow.com/a/24476/12149471) where using `goto` is ok. Personally, I also consider `goto` acceptable in this case, but most people will prefer to use an infinite loop. If you use `goto`, you should give the label a descriptive name, such as `bad_input` or `repeat_input`. – Andreas Wenzel Sep 15 '21 at 18:34
  • 1
    Note that nothing keeps the user from entering more than 20 characters and sending your program into buffer overrun country. For user input, `scanf()` is generally the wrong tool. Have a look at `fgets()` instead. – DevSolar Sep 15 '21 at 18:52

1 Answers1

1

You can use a do-while loop & set a flag/boolean variable to & run the loop prompting the user to re-enter the option till he selects a valid option. Here I have set a int variable - "isInvalidSelection" & set it to true, then have set up a do-while loop & ran the user input code inside the do-while loop, finally the test condition for the do-while loop is the predicate/boolean which was initially set to true & as long as it is true the loop will continue.

Note : Once the user makes a valid selection, the value of isInvalidSelection is made false, & the do-while loops terminates when a falsy value is encountered at the last iteration.

Note : In C, Any Non-Zero integer is Truthy, & 0 is Falsy.

Code :

#include <string.h>
#include <stdio.h>

int main(){
    
    char userInput[24];
    int isInvalidSelection = 1;
    
    do {
       // Prompts the user to make a selection & retrieve the input selection.
        printf("\n User Selection :\t");
        scanf("%s",  userInput);
        
       // Checks the user's selection against the valid selections
        if(strcmp(userInput,"debug") == 0) {
            printf("\n User Entered debug");
            isInvalidSelection = 0; // do while loop test condition is set to false
        }
        else if(strcmp(userInput,"test") == 0) {
            printf("\n User Entered test");
            isInvalidSelection = 0;. // do-while loop test condition is set to false
            
        }else {
            // For an invalid selection, displays a message informing the user & the loop continues
            printf("\nInvalid Selection! Try Again!!\n");
        }
    }while(isInvalidSelection);
    return 0;
}

Suggestion : You could use strcmpi() function to compare the strings ignoring case-sensitive inputs (Make sure that this method is available for your compiler first, or otherwise if you compiler doesn't support the method another way to implement strcmpi() like functionality would be to convert the userSelection to lowercase or uppercase & then use strcmp() with the respective comparison)

Danvin
  • 61
  • 4
  • In my opinion, an infinite loop with an explicit `break` statement is better, because that way, you don't need the additional variable `isInvalidSelection`. – Andreas Wenzel Sep 15 '21 at 19:05
  • That's true! There is no need for an extra variable actually, having a while loop with test condition set to a Truthy value & breaking out of it upon meeting certain criteria will also work & save some memory, – Danvin Sep 15 '21 at 19:11
  • There is no function `strcmpi` in ISO C. You are referring to a compiler-specific function (probably of the Microsoft compiler). Therefore, that function will probably only work on that compiler and not on any other compilers. – Andreas Wenzel Sep 15 '21 at 19:15
  • I didn't say it will be available everywhere, there are many useful functions but not standard! But it can be useful so if he has it he could check it! – Danvin Sep 15 '21 at 19:56
  • In general, there is nothing wrong with recommending compiler-specific functions, even when the question is not about a specific compiler. However, when making such a recommendation, you should generally make clear that the function you are recommending is compiler-specific and may not work on other compilers. In your answer, you wrote: `"You could use strcmpi() function [...]"`. This statement is incorrect if the reader is not using the same compiler as you are. – Andreas Wenzel Sep 15 '21 at 20:07
  • Note that you can [edit] your answer to improve it. – Andreas Wenzel Sep 15 '21 at 20:14
  • Yes, I'll edit it! – Danvin Sep 15 '21 at 21:05
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/237154/discussion-between-danvin-and-andreas-wenzel). – Danvin Sep 15 '21 at 21:10